Skip to content

Commit

Permalink
feat: native TypeScript support (#80) thanks @jloveridge
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jloveridge authored and nodkz committed Sep 27, 2018
1 parent 2ca96f5 commit edfa536
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Downloads stat](https://img.shields.io/npm/dt/mongodb-memory-server.svg)](http://www.npmtrends.com/mongodb-memory-server)
[![Travis](https://img.shields.io/travis/nodkz/mongodb-memory-server.svg?maxAge=2592000)](https://travis-ci.org/nodkz/mongodb-memory-server)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
![TypeScript compatible](https://img.shields.io/badge/typescript-compatible-brightgreen.svg)

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.

Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0-development",
"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.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": "https://github.com/nodkz/mongodb-memory-server",
"author": "Nodkz (pavel.chertorogov@gmail.com)",
"files": [
Expand Down Expand Up @@ -32,6 +33,8 @@
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@types/getos": "^3.0.0",
"@types/node": "^10.11.1",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.6.0",
Expand All @@ -48,7 +51,9 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"semantic-release": "^15.9.15"
"semantic-release": "^15.9.15",
"tslint": "^5.11.0",
"typescript": "^3.1.1"
},
"dependencies": {
"@babel/runtime": "^7.0.0",
Expand All @@ -68,11 +73,14 @@
"build": "npm-run-all build:*",
"build:cjs": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
"build:flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
"build:ts": "find ./src -name '*.d.ts' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`; done",
"watch": "jest --watch",
"coverage": "jest --coverage",
"lint": "eslint --ext .js ./src",
"lint": "eslint --ext .js ./src && npm run tslint",
"flow": "./node_modules/.bin/flow",
"test": "npm run coverage && npm run lint && npm run flow",
"test": "npm run coverage && npm run lint && npm run flow && npm run tscheck",
"tscheck": "tsc",
"tslint": "tslint -p . \"src/**/*.d.ts\"",
"semantic-release": "semantic-release",
"postinstall": "node ./postinstall.js"
},
Expand Down
58 changes: 58 additions & 0 deletions src/MongoMemoryServer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// <reference types='node' />

import { ChildProcess } from 'child_process';
import MongoInstance from './util/MongoInstance';

export 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;
}

export interface MongoInstanceDataT {
port: number;
dbPath: string;
dbName: string;
uri: string;
storageEngine: string;
instance: MongoInstance;
childProcess: ChildProcess;
tmpDir?: {
name: string;
removeCallback: ((...args: any[]) => any);
};
}

export default class MongoMemoryServer {
isRunning: boolean;
runningInstance: Promise<MongoInstanceDataT> | undefined;
opts: MongoMemoryServerOptsT;
debug: ((...args: any[]) => any);

constructor(opts?: Partial<MongoMemoryServerOptsT>);

start(): Promise<boolean>;
stop(): Promise<boolean>;
getInstanceData(): Promise<MongoInstanceDataT>;
getUri(otherDbName?: string | boolean): Promise<string>;
getConnectionString(otherDbName?: string | boolean): Promise<string>;
getPort(): Promise<number>;
getDbPath(): Promise<string>;
getDbName(): Promise<string>;

protected _startUpInstance(): Promise<MongoInstanceDataT>;
}
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import MongoBinary from './util/MongoBinary';
import MongoInstance from './util/MongoInstance';
import MongoMemoryServer from './MongoMemoryServer';

export default MongoMemoryServer;
export { MongoBinary, MongoInstance, MongoMemoryServer };
21 changes: 21 additions & 0 deletions src/util/MongoBinary.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface MongoBinaryCache {
[version: string]: string;
}

export 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
export default class MongoBinary {
static cache: MongoBinaryCache;

static getPath(opts?: MongoBinaryOpts): Promise<string>;
static hasValidBinPath(files: string[]): boolean;
}
38 changes: 38 additions & 0 deletions src/util/MongoBinaryDownload.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export interface MongoBinaryDownloadOpts {
version: string;
downloadDir: string;
platform: string;
arch: string;
debug?: boolean | Function;
}

interface dlProgress { // tslint:disable-line
current: number;
length: number;
totalMb: number;
lastPrintedAt: number;
}

export default class MongoBinaryDownload {
debug: Function;
dlProgress: dlProgress;

downloadDir: string;
arch: string;
version: string;
platform: string;

constructor(opts: MongoBinaryDownloadOpts);
getMongodPath(): Promise<string>;
startDownload(): Promise<string>;
checkMd5(mongoDBArchiveMd5: string, mongoDBArchive: string): Promise<void>;
download(downloadUrl: string): Promise<string>;
extract(mongoDBArchive: string): Promise<string>;
httpDownload(
httpOptions: any, // tslint:disable-line:no-any
downloadLocation: string,
tempDownloadLocation: string
): Promise<string>;
printDownloadProgress(chunk: any): void; // tslint:disable-line:no-any
locationExists(location: string): boolean;
}
30 changes: 30 additions & 0 deletions src/util/MongoBinaryDownloadUrl.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference types='getos' />

import {Os as OS} from 'getos';

export interface MongoBinaryDownloadUrlOpts {
version: string;
platform: string;
arch: string;
os?: OS; // getos() result
}

export default class MongoBinaryDownloadUrl {
constructor(opts: MongoBinaryDownloadUrlOpts);
getDownloadUrl(): Promise<string>;
getArchiveName(): Promise<string>;
getArchiveNameWin(): Promise<string>;
getArchiveNameOsx(): Promise<string>;
getArchiveNameLinux(): Promise<string>;
getos(): Promise<OS>;
getLinuxOSVersionString(os: OS): string;
getDebianVersionString(os: OS): string;
getFedoraVersionString(os: OS): string;
getRhelVersionString(os: OS): string;
getElementaryOSVersionString(os: OS): string;
getLegacyVersionString(os: OS): string;
getSuseVersionString(os: any): string;
getUbuntuVersionString(os: OS): string;
translatePlatform(platform: string): string;
translateArch(arch: string, mongoPlatform: string): string;
}
57 changes: 57 additions & 0 deletions src/util/MongoInstance.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// <reference types='node' />

import { ChildProcess } from 'child_process';
import { MongoBinaryOpts } from './MongoBinary';

export interface MongodOps {
// instance options
instance: {
port: number;
storageEngine?: string;
dbPath: string;
debug?: boolean | ((...args: any[]) => any);
};

// mongo binary options
binary?: 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);
}

export default class MongodbInstance {
static childProcessList: ChildProcess[];

opts: MongodOps;
debug: ((...args: any[]) => any);
childProcess: ChildProcess;
killerProcess: ChildProcess;
instanceReady: ((...args: any[]) => any);
instanceFailed: ((...args: any[]) => any);

constructor(opts: MongodOps);

static run(opts: MongodOps): Promise<MongodbInstance>;
prepareCommandArgs(): string[];
run(): Promise<MongodbInstance>;
kill(): Promise<MongodbInstance>;
getPid(): number | undefined;
errorHandler(err: string): void;
closeHandler(code: number): void;
stderrHandler(message: string | Buffer): void;
stdoutHandler(message: string | Buffer): void;

private _launchMongod(mongoBin: string): ChildProcess;
private _launchKiller(parentPid: number, childPid: number): ChildProcess;
}
Empty file added src/util/mongo_killer.d.ts
Empty file.
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"rootDir": ".",
"declaration": true,
"lib": ["es2017", "esnext.asynciterable"]
},
"include": [ "src/**/*.d.ts" ]
}
16 changes: 16 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"defaultSeverity": "error",
"extends": "tslint:recommended",
"jsRules": {},
"rules": {
"ban-types": [true, ["Object", "Use {} instead."], ["String"]],
"quotemark": [true, "single"],
"trailing-comma": [false],
"ordered-imports": false,
"member-access": false,
"member-ordering": [true, { "order": "fields-first" }],
"variable-name": false,
"interface-name": false,
"no-reference-import": false
}
}

0 comments on commit edfa536

Please sign in to comment.