diff --git a/types/mongodb-memory-server/index.d.ts b/types/mongodb-memory-server/index.d.ts new file mode 100644 index 0000000000..bfb14ed97d --- /dev/null +++ b/types/mongodb-memory-server/index.d.ts @@ -0,0 +1,153 @@ +// Type definitions for mongodb-memory-server 1.8 +// Project: https://github.com/nodkz/mongodb-memory-server +// Definitions by: Dmitry Rogozhny +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +/// + +import { ChildProcess } from "child_process"; + +import MongoMemoryServer = _MongoMemoryServer.MongoMemoryServer; +import MongoInstance = _MongoInstance.MongodbInstance; +import MongoBinary = _MongoBinary.MongoBinary; + +export default MongoMemoryServer; +export { MongoMemoryServer, MongoInstance, MongoBinary }; + +// exports from the ./util/MongoBinary.js.flow file +declare namespace _MongoBinary { + interface MongoBinaryCache { + [version: string]: string; + } + + interface MongoBinaryOpts { + version?: string; + downloadDir?: string; + platform?: string; + arch?: string; + debug?: boolean | ((...args: any[]) => any); + } + + // disable error for a class with all static functions, + // so the TypeScript declaration would map the implementation with flow types for easier support. + // tslint:disable-next-line:no-unnecessary-class + class MongoBinary { + static cache: MongoBinaryCache; + + static getPath(opts?: MongoBinaryOpts): Promise; + static hasValidBinPath(files: string[]): boolean; + } +} + +// exports from the ./util/MongoInstance.js.flow file +declare namespace _MongoInstance { + interface MongodOps { + // instance options + instance: { + port: number, + storageEngine?: string, + dbPath: string, + debug?: boolean | ((...args: any[]) => any), + }; + + // mongo binary options + binary?: _MongoBinary.MongoBinaryOpts; + + // child process spawn options + spawn?: { + cwd?: string, + env?: object, + argv0?: string, + stdio?: string | any[], + detached?: boolean, + uid?: number, + gid?: number, + shell?: boolean | string, + }; + + debug?: boolean | ((...args: any[]) => any); + } + + class MongodbInstance { + static childProcessList: ChildProcess[]; + + opts: MongodOps; + debug: ((...args: any[]) => any); + childProcess: ChildProcess; + killerProcess: ChildProcess; + instanceReady: ((...args: any[]) => any); + instanceFailed: ((...args: any[]) => any); + + static run(opts: MongodOps): Promise; + + constructor(opts: MongodOps); + + prepareCommandArgs(): string[]; + run(): Promise; + kill(): Promise; + getPid(): number | undefined; + _launchMongod(mongoBin: string): ChildProcess; + _launchKiller(parentPid: number, childPid: number): ChildProcess; + errorHandler(err: string): void; + closeHandler(code: number): void; + stderrHandler(message: string | Buffer): void; + stdoutHandler(message: string | Buffer): void; + } +} + +// exports from the MongoMemoryServer.js.flow file +declare namespace _MongoMemoryServer { + interface MongoMemoryServerOptsT { + instance: { + port?: number, + dbPath?: string, + dbName?: string, + storageEngine?: string, + debug?: boolean | ((...args: any[]) => any), + }; + binary: { + version?: string, + downloadDir?: string, + platform?: string, + arch?: string, + debug?: boolean | ((...args: any[]) => any), + }; + debug?: boolean; + spawn: any; + autoStart?: boolean; + } + + interface MongoInstanceDataT { + port: number; + dbPath: string; + dbName: string; + uri: string; + storageEngine: string; + instance: _MongoInstance.MongodbInstance; + childProcess: ChildProcess; + tmpDir?: { + name: string, + removeCallback: ((...args: any[]) => any), + }; + } + + class MongoMemoryServer { + isRunning: boolean; + runningInstance: Promise | undefined; + opts: MongoMemoryServerOptsT; + debug: ((...args: any[]) => any); + + constructor(opts?: Partial); + + start(): Promise; + _startUpInstance(): Promise; + stop(): Promise; + getInstanceData(): Promise; + getUri(otherDbName?: string | boolean): Promise; + getConnectionString(otherDbName?: string | boolean): Promise; + getPort(): Promise; + getDbPath(): Promise; + getDbName(): Promise; + } +} diff --git a/types/mongodb-memory-server/mongodb-memory-server-tests.ts b/types/mongodb-memory-server/mongodb-memory-server-tests.ts new file mode 100644 index 0000000000..62abb2651b --- /dev/null +++ b/types/mongodb-memory-server/mongodb-memory-server-tests.ts @@ -0,0 +1,15 @@ +import MongodbMemoryServer from 'mongodb-memory-server'; + +// Simple server start example from https://www.npmjs.com/package/mongodb-memory-server +async function simpleServerStart() { + const mongod = new MongodbMemoryServer(); + + const uri = await mongod.getConnectionString(); + const port = await mongod.getPort(); + const dbPath = await mongod.getDbPath(); + const dbName = await mongod.getDbName(); + + // you may stop mongod manually + mongod.stop(); + // or it will be stopped automatically when you exit from script +} diff --git a/types/mongodb-memory-server/tsconfig.json b/types/mongodb-memory-server/tsconfig.json new file mode 100644 index 0000000000..29046ccb01 --- /dev/null +++ b/types/mongodb-memory-server/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mongodb-memory-server-tests.ts" + ] +} diff --git a/types/mongodb-memory-server/tslint.json b/types/mongodb-memory-server/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mongodb-memory-server/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }