Skip to content

Commit edfa536

Browse files
jloveridgenodkz
authored andcommitted
feat: native TypeScript support (#80) thanks @jloveridge
* chore(types): add TypeScript declarations, ported from Flow * chore(tsconfig, tslint): add TypeScript and TSLint, and configure * chore(package): add commands to lint TypeScript files, type-check, and export declaration files to package. * docs(readme): add a badge to signal TypeScript compatibility. * chore(package): reference the declaration files entry point
1 parent 2ca96f5 commit edfa536

File tree

11 files changed

+249
-3
lines changed

11 files changed

+249
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![Downloads stat](https://img.shields.io/npm/dt/mongodb-memory-server.svg)](http://www.npmtrends.com/mongodb-memory-server)
66
[![Travis](https://img.shields.io/travis/nodkz/mongodb-memory-server.svg?maxAge=2592000)](https://travis-ci.org/nodkz/mongodb-memory-server)
77
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
8+
![TypeScript compatible](https://img.shields.io/badge/typescript-compatible-brightgreen.svg)
89

910
This package spins up a actual/real MongoDB Server programmatically from node for testing or mocking during development. By default it holds the data in memory. Fresh spinned up `mongod` process takes about 7Mb of memory. The server will allow you to connect your favorite ODM or client library to the MongoDB Server and run integration tests isolated from each other.
1011

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0-development",
44
"description": "In-memory MongoDB Server. Designed with testing in mind, the server will allow you to connect your favourite ODM or client library to the MongoDB Server and run integration tests isolated from each other.",
55
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
67
"repository": "https://github.com/nodkz/mongodb-memory-server",
78
"author": "Nodkz ([email protected])",
89
"files": [
@@ -32,6 +33,8 @@
3233
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
3334
"@babel/preset-env": "^7.0.0",
3435
"@babel/preset-flow": "^7.0.0",
36+
"@types/getos": "^3.0.0",
37+
"@types/node": "^10.11.1",
3538
"babel-core": "^7.0.0-bridge.0",
3639
"babel-eslint": "^9.0.0",
3740
"babel-jest": "^23.6.0",
@@ -48,7 +51,9 @@
4851
"npm-run-all": "^4.1.3",
4952
"prettier": "^1.14.2",
5053
"rimraf": "^2.6.2",
51-
"semantic-release": "^15.9.15"
54+
"semantic-release": "^15.9.15",
55+
"tslint": "^5.11.0",
56+
"typescript": "^3.1.1"
5257
},
5358
"dependencies": {
5459
"@babel/runtime": "^7.0.0",
@@ -68,11 +73,14 @@
6873
"build": "npm-run-all build:*",
6974
"build:cjs": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
7075
"build:flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
76+
"build:ts": "find ./src -name '*.d.ts' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`; done",
7177
"watch": "jest --watch",
7278
"coverage": "jest --coverage",
73-
"lint": "eslint --ext .js ./src",
79+
"lint": "eslint --ext .js ./src && npm run tslint",
7480
"flow": "./node_modules/.bin/flow",
75-
"test": "npm run coverage && npm run lint && npm run flow",
81+
"test": "npm run coverage && npm run lint && npm run flow && npm run tscheck",
82+
"tscheck": "tsc",
83+
"tslint": "tslint -p . \"src/**/*.d.ts\"",
7684
"semantic-release": "semantic-release",
7785
"postinstall": "node ./postinstall.js"
7886
},

src/MongoMemoryServer.d.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/// <reference types='node' />
2+
3+
import { ChildProcess } from 'child_process';
4+
import MongoInstance from './util/MongoInstance';
5+
6+
export interface MongoMemoryServerOptsT {
7+
instance: {
8+
port?: number;
9+
dbPath?: string;
10+
dbName?: string;
11+
storageEngine?: string;
12+
debug?: boolean | ((...args: any[]) => any);
13+
};
14+
binary: {
15+
version?: string;
16+
downloadDir?: string;
17+
platform?: string;
18+
arch?: string;
19+
debug?: boolean | ((...args: any[]) => any);
20+
};
21+
debug?: boolean;
22+
spawn: any;
23+
autoStart?: boolean;
24+
}
25+
26+
export interface MongoInstanceDataT {
27+
port: number;
28+
dbPath: string;
29+
dbName: string;
30+
uri: string;
31+
storageEngine: string;
32+
instance: MongoInstance;
33+
childProcess: ChildProcess;
34+
tmpDir?: {
35+
name: string;
36+
removeCallback: ((...args: any[]) => any);
37+
};
38+
}
39+
40+
export default class MongoMemoryServer {
41+
isRunning: boolean;
42+
runningInstance: Promise<MongoInstanceDataT> | undefined;
43+
opts: MongoMemoryServerOptsT;
44+
debug: ((...args: any[]) => any);
45+
46+
constructor(opts?: Partial<MongoMemoryServerOptsT>);
47+
48+
start(): Promise<boolean>;
49+
stop(): Promise<boolean>;
50+
getInstanceData(): Promise<MongoInstanceDataT>;
51+
getUri(otherDbName?: string | boolean): Promise<string>;
52+
getConnectionString(otherDbName?: string | boolean): Promise<string>;
53+
getPort(): Promise<number>;
54+
getDbPath(): Promise<string>;
55+
getDbName(): Promise<string>;
56+
57+
protected _startUpInstance(): Promise<MongoInstanceDataT>;
58+
}

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import MongoBinary from './util/MongoBinary';
2+
import MongoInstance from './util/MongoInstance';
3+
import MongoMemoryServer from './MongoMemoryServer';
4+
5+
export default MongoMemoryServer;
6+
export { MongoBinary, MongoInstance, MongoMemoryServer };

src/util/MongoBinary.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export interface MongoBinaryCache {
2+
[version: string]: string;
3+
}
4+
5+
export interface MongoBinaryOpts {
6+
version?: string;
7+
downloadDir?: string;
8+
platform?: string;
9+
arch?: string;
10+
debug?: boolean | ((...args: any[]) => any);
11+
}
12+
13+
// disable error for a class with all static functions,
14+
// so the TypeScript declaration would map the implementation with flow types for easier support.
15+
// tslint:disable-next-line:no-unnecessary-class
16+
export default class MongoBinary {
17+
static cache: MongoBinaryCache;
18+
19+
static getPath(opts?: MongoBinaryOpts): Promise<string>;
20+
static hasValidBinPath(files: string[]): boolean;
21+
}

src/util/MongoBinaryDownload.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export interface MongoBinaryDownloadOpts {
2+
version: string;
3+
downloadDir: string;
4+
platform: string;
5+
arch: string;
6+
debug?: boolean | Function;
7+
}
8+
9+
interface dlProgress { // tslint:disable-line
10+
current: number;
11+
length: number;
12+
totalMb: number;
13+
lastPrintedAt: number;
14+
}
15+
16+
export default class MongoBinaryDownload {
17+
debug: Function;
18+
dlProgress: dlProgress;
19+
20+
downloadDir: string;
21+
arch: string;
22+
version: string;
23+
platform: string;
24+
25+
constructor(opts: MongoBinaryDownloadOpts);
26+
getMongodPath(): Promise<string>;
27+
startDownload(): Promise<string>;
28+
checkMd5(mongoDBArchiveMd5: string, mongoDBArchive: string): Promise<void>;
29+
download(downloadUrl: string): Promise<string>;
30+
extract(mongoDBArchive: string): Promise<string>;
31+
httpDownload(
32+
httpOptions: any, // tslint:disable-line:no-any
33+
downloadLocation: string,
34+
tempDownloadLocation: string
35+
): Promise<string>;
36+
printDownloadProgress(chunk: any): void; // tslint:disable-line:no-any
37+
locationExists(location: string): boolean;
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// <reference types='getos' />
2+
3+
import {Os as OS} from 'getos';
4+
5+
export interface MongoBinaryDownloadUrlOpts {
6+
version: string;
7+
platform: string;
8+
arch: string;
9+
os?: OS; // getos() result
10+
}
11+
12+
export default class MongoBinaryDownloadUrl {
13+
constructor(opts: MongoBinaryDownloadUrlOpts);
14+
getDownloadUrl(): Promise<string>;
15+
getArchiveName(): Promise<string>;
16+
getArchiveNameWin(): Promise<string>;
17+
getArchiveNameOsx(): Promise<string>;
18+
getArchiveNameLinux(): Promise<string>;
19+
getos(): Promise<OS>;
20+
getLinuxOSVersionString(os: OS): string;
21+
getDebianVersionString(os: OS): string;
22+
getFedoraVersionString(os: OS): string;
23+
getRhelVersionString(os: OS): string;
24+
getElementaryOSVersionString(os: OS): string;
25+
getLegacyVersionString(os: OS): string;
26+
getSuseVersionString(os: any): string;
27+
getUbuntuVersionString(os: OS): string;
28+
translatePlatform(platform: string): string;
29+
translateArch(arch: string, mongoPlatform: string): string;
30+
}

src/util/MongoInstance.d.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// <reference types='node' />
2+
3+
import { ChildProcess } from 'child_process';
4+
import { MongoBinaryOpts } from './MongoBinary';
5+
6+
export interface MongodOps {
7+
// instance options
8+
instance: {
9+
port: number;
10+
storageEngine?: string;
11+
dbPath: string;
12+
debug?: boolean | ((...args: any[]) => any);
13+
};
14+
15+
// mongo binary options
16+
binary?: MongoBinaryOpts;
17+
18+
// child process spawn options
19+
spawn?: {
20+
cwd?: string;
21+
env?: object;
22+
argv0?: string;
23+
stdio?: string | any[];
24+
detached?: boolean;
25+
uid?: number;
26+
gid?: number;
27+
shell?: boolean | string;
28+
};
29+
30+
debug?: boolean | ((...args: any[]) => any);
31+
}
32+
33+
export default class MongodbInstance {
34+
static childProcessList: ChildProcess[];
35+
36+
opts: MongodOps;
37+
debug: ((...args: any[]) => any);
38+
childProcess: ChildProcess;
39+
killerProcess: ChildProcess;
40+
instanceReady: ((...args: any[]) => any);
41+
instanceFailed: ((...args: any[]) => any);
42+
43+
constructor(opts: MongodOps);
44+
45+
static run(opts: MongodOps): Promise<MongodbInstance>;
46+
prepareCommandArgs(): string[];
47+
run(): Promise<MongodbInstance>;
48+
kill(): Promise<MongodbInstance>;
49+
getPid(): number | undefined;
50+
errorHandler(err: string): void;
51+
closeHandler(code: number): void;
52+
stderrHandler(message: string | Buffer): void;
53+
stdoutHandler(message: string | Buffer): void;
54+
55+
private _launchMongod(mongoBin: string): ChildProcess;
56+
private _launchKiller(parentPid: number, childPid: number): ChildProcess;
57+
}

src/util/mongo_killer.d.ts

Whitespace-only changes.

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"strict": true,
6+
"rootDir": ".",
7+
"declaration": true,
8+
"lib": ["es2017", "esnext.asynciterable"]
9+
},
10+
"include": [ "src/**/*.d.ts" ]
11+
}

0 commit comments

Comments
 (0)