mongodb-memory-server-core
Advanced tools
Comparing version 6.1.1 to 6.2.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [6.2.0](https://github.com/nodkz/mongodb-memory-server/compare/v6.1.1...v6.2.0) (2019-12-26) | ||
### Features | ||
* add static async `MongoMemoryServer.create()`; improve Example code & add TSDoc (tnx [@hasezoey](https://github.com/hasezoey)) ([a2b0fc4](https://github.com/nodkz/mongodb-memory-server/commit/a2b0fc4)), closes [#211](https://github.com/nodkz/mongodb-memory-server/issues/211) [/github.com/nodkz/mongodb-memory-server/issues/184#issuecomment-568782496](https://github.com//github.com/nodkz/mongodb-memory-server/issues/184/issues/issuecomment-568782496) | ||
## [6.1.1](https://github.com/nodkz/mongodb-memory-server/compare/v6.1.0...v6.1.1) (2019-12-20) | ||
@@ -8,0 +19,0 @@ |
@@ -120,3 +120,3 @@ "use strict"; | ||
mongoServer = new MongoMemoryServer({ autoStart: false }); | ||
return [4 /*yield*/, expect(mongoServer.ensureInstance()).rejects.toThrow('Database instance is not running. You should start database by calling start() method. BTW it should start automatically if opts.autoStart!=false. Also you may provide opts.debug=true for more info.')]; | ||
return [4 /*yield*/, expect(mongoServer.ensureInstance()).rejects.toThrow('Ensure-Instance failed to start an instance!')]; | ||
case 1: | ||
@@ -161,3 +161,32 @@ _a.sent(); | ||
}); | ||
describe('create()', function () { | ||
// before each for sanity (overwrite protection) | ||
beforeEach(function () { | ||
// de-duplicate code | ||
MongoMemoryServer.prototype.start = jest.fn(function () { return Promise.resolve(true); }); | ||
}); | ||
it('should create an instance but not autostart', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, MongoMemoryServer.create()]; | ||
case 1: | ||
_a.sent(); | ||
expect(MongoMemoryServer.prototype.start).toHaveBeenCalledTimes(0); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
it('should autostart and be awaitable', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, MongoMemoryServer.create({ autoStart: true })]; | ||
case 1: | ||
_a.sent(); | ||
expect(MongoMemoryServer.prototype.start).toHaveBeenCalledTimes(1); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); | ||
}); | ||
//# sourceMappingURL=MongoMemoryServer-test.js.map |
@@ -33,15 +33,68 @@ /// <reference types="node" /> | ||
debug: DebugFn; | ||
/** | ||
* Create an Mongo-Memory-Sever Instance | ||
* | ||
* Note: because of JavaScript limitations, autoStart cannot be awaited here, use ".create" for async/await ability | ||
* @param opts Mongo-Memory-Sever Options | ||
*/ | ||
constructor(opts?: MongoMemoryServerOptsT); | ||
/** | ||
* Create an Mongo-Memory-Sever Instance that can be awaited | ||
* @param opts Mongo-Memory-Sever Options | ||
*/ | ||
static create(opts?: MongoMemoryServerOptsT): Promise<MongoMemoryServer>; | ||
/** | ||
* Start the in-memory Instance | ||
* (when options.autoStart is true, this already got called) | ||
*/ | ||
start(): Promise<boolean>; | ||
/** | ||
* Internal Function to start an instance | ||
* @private | ||
*/ | ||
_startUpInstance(): Promise<MongoInstanceDataT>; | ||
/** | ||
* Stop the current In-Memory Instance | ||
*/ | ||
stop(): Promise<boolean>; | ||
/** | ||
* Get Information about the currently running instance, if it is not running it returns "false" | ||
*/ | ||
getInstanceInfo(): MongoInstanceDataT | false; | ||
/** | ||
* Ensure that the instance is running | ||
* -> throws if instance cannot be started | ||
*/ | ||
ensureInstance(): Promise<MongoInstanceDataT>; | ||
/** | ||
* Basic MongoDB Connection string | ||
* @private | ||
*/ | ||
_getUriBase(host: string, port: number, dbName: string): string; | ||
/** | ||
* Get a mongodb-URI for a different DataBase | ||
* @param otherDbName Set this to "true" to generate a random DataBase name, otherwise a string to specify a DataBase name | ||
*/ | ||
getUri(otherDbName?: string | boolean): Promise<string>; | ||
/** | ||
* Get a mongodb-URI for a different DataBase | ||
* @param otherDbName Set this to "true" to generate a random DataBase name, otherwise a string to specify a DataBase name | ||
*/ | ||
getConnectionString(otherDbName?: string | boolean): Promise<string>; | ||
/** | ||
* Get the Port of the currently running Instance | ||
* Note: calls "ensureInstance" | ||
*/ | ||
getPort(): Promise<number>; | ||
/** | ||
* Get the DB-Path of the currently running Instance | ||
* Note: calls "ensureInstance" | ||
*/ | ||
getDbPath(): Promise<string>; | ||
/** | ||
* Get the DB-Name of the currently running Instance | ||
* Note: calls "ensureInstance" | ||
*/ | ||
getDbName(): Promise<string>; | ||
} | ||
//# sourceMappingURL=MongoMemoryServer.d.ts.map |
@@ -57,7 +57,14 @@ "use strict"; | ||
var MongoInstance_1 = __importDefault(require("./util/MongoInstance")); | ||
// import { deprecate } from './util/deprecate'; | ||
var util_1 = require("util"); | ||
tmp_1.default.setGracefulCleanup(); | ||
var MongoMemoryServer = /** @class */ (function () { | ||
/** | ||
* Create an Mongo-Memory-Sever Instance | ||
* | ||
* Note: because of JavaScript limitations, autoStart cannot be awaited here, use ".create" for async/await ability | ||
* @param opts Mongo-Memory-Sever Options | ||
*/ | ||
function MongoMemoryServer(opts) { | ||
var _this = this; | ||
var _a; | ||
this.runningInstance = null; | ||
@@ -71,3 +78,3 @@ this.instanceInfoSync = null; | ||
}; | ||
if (!(opts && opts.autoStart === false)) { | ||
if (((_a = opts) === null || _a === void 0 ? void 0 : _a.autoStart) === true) { | ||
this.debug('Autostarting MongoDB instance...'); | ||
@@ -77,2 +84,28 @@ this.start(); | ||
} | ||
/** | ||
* Create an Mongo-Memory-Sever Instance that can be awaited | ||
* @param opts Mongo-Memory-Sever Options | ||
*/ | ||
MongoMemoryServer.create = function (opts) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var instance; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
instance = new MongoMemoryServer(__assign(__assign({}, opts), { autoStart: false })); | ||
if (!((_a = opts) === null || _a === void 0 ? void 0 : _a.autoStart)) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, instance.start()]; | ||
case 1: | ||
_b.sent(); | ||
_b.label = 2; | ||
case 2: return [2 /*return*/, instance]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Start the in-memory Instance | ||
* (when options.autoStart is true, this already got called) | ||
*/ | ||
MongoMemoryServer.prototype.start = function () { | ||
@@ -111,2 +144,6 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
/** | ||
* Internal Function to start an instance | ||
* @private | ||
*/ | ||
MongoMemoryServer.prototype._startUpInstance = function () { | ||
@@ -169,2 +206,5 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
/** | ||
* Stop the current In-Memory Instance | ||
*/ | ||
MongoMemoryServer.prototype.stop = function () { | ||
@@ -177,2 +217,6 @@ return __awaiter(this, void 0, void 0, function () { | ||
this.debug('Called MongoMemoryServer.stop() method'); | ||
// just return "true" if the instance is already running / defined | ||
if (util_1.isNullOrUndefined(this.runningInstance)) { | ||
return [2 /*return*/, true]; | ||
} | ||
return [4 /*yield*/, this.ensureInstance()]; | ||
@@ -196,5 +240,13 @@ case 1: | ||
}; | ||
/** | ||
* Get Information about the currently running instance, if it is not running it returns "false" | ||
*/ | ||
MongoMemoryServer.prototype.getInstanceInfo = function () { | ||
return this.instanceInfoSync || false; | ||
var _a; | ||
return _a = this.instanceInfoSync, (_a !== null && _a !== void 0 ? _a : false); | ||
}; | ||
/** | ||
* Ensure that the instance is running | ||
* -> throws if instance cannot be started | ||
*/ | ||
MongoMemoryServer.prototype.ensureInstance = function () { | ||
@@ -206,14 +258,15 @@ return __awaiter(this, void 0, void 0, function () { | ||
this.debug('Called MongoMemoryServer.ensureInstance() method:'); | ||
if (!!this.runningInstance) return [3 /*break*/, 2]; | ||
if (!this.runningInstance) return [3 /*break*/, 1]; | ||
return [2 /*return*/, this.runningInstance]; | ||
case 1: | ||
this.debug(' - no running instance, call `start()` command'); | ||
return [4 /*yield*/, this.start()]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
this.debug(' - `start()` command was succesfully resolved'); | ||
_a.label = 2; | ||
case 2: | ||
if (this.runningInstance) { | ||
return [2 /*return*/, this.runningInstance]; | ||
// check again for 1. Typescript-type reasons and 2. if .start failed to throw an error | ||
if (!this.runningInstance) { | ||
throw new Error('Ensure-Instance failed to start an instance!'); | ||
} | ||
throw new Error('Database instance is not running. You should start database by calling start() method. BTW it should start automatically if opts.autoStart!=false. Also you may provide opts.debug=true for more info.'); | ||
return [2 /*return*/, this.runningInstance]; | ||
} | ||
@@ -223,5 +276,13 @@ }); | ||
}; | ||
/** | ||
* Basic MongoDB Connection string | ||
* @private | ||
*/ | ||
MongoMemoryServer.prototype._getUriBase = function (host, port, dbName) { | ||
return "mongodb://" + host + ":" + port + "/" + dbName + "?"; | ||
}; | ||
/** | ||
* Get a mongodb-URI for a different DataBase | ||
* @param otherDbName Set this to "true" to generate a random DataBase name, otherwise a string to specify a DataBase name | ||
*/ | ||
MongoMemoryServer.prototype.getUri = function (otherDbName) { | ||
@@ -250,2 +311,6 @@ if (otherDbName === void 0) { otherDbName = false; } | ||
}; | ||
/** | ||
* Get a mongodb-URI for a different DataBase | ||
* @param otherDbName Set this to "true" to generate a random DataBase name, otherwise a string to specify a DataBase name | ||
*/ | ||
MongoMemoryServer.prototype.getConnectionString = function (otherDbName) { | ||
@@ -255,2 +320,3 @@ if (otherDbName === void 0) { otherDbName = false; } | ||
return __generator(this, function (_a) { | ||
// should this function be marked deprecated? because it is just a pass-through to getUri | ||
return [2 /*return*/, this.getUri(otherDbName)]; | ||
@@ -260,2 +326,6 @@ }); | ||
}; | ||
/** | ||
* Get the Port of the currently running Instance | ||
* Note: calls "ensureInstance" | ||
*/ | ||
MongoMemoryServer.prototype.getPort = function () { | ||
@@ -274,2 +344,6 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
/** | ||
* Get the DB-Path of the currently running Instance | ||
* Note: calls "ensureInstance" | ||
*/ | ||
MongoMemoryServer.prototype.getDbPath = function () { | ||
@@ -288,2 +362,6 @@ return __awaiter(this, void 0, void 0, function () { | ||
}; | ||
/** | ||
* Get the DB-Name of the currently running Instance | ||
* Note: calls "ensureInstance" | ||
*/ | ||
MongoMemoryServer.prototype.getDbName = function () { | ||
@@ -290,0 +368,0 @@ return __awaiter(this, void 0, void 0, function () { |
@@ -38,2 +38,5 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -44,2 +47,4 @@ var fs_1 = require("fs"); | ||
var util_1 = require("util"); | ||
var path_1 = require("path"); | ||
var resolve_config_1 = __importDefault(require("../resolve-config")); | ||
/** Collection of Regexes for "lsb_release -a" parsing */ | ||
@@ -85,30 +90,122 @@ var LSBRegex = { | ||
function getLinuxInfomation() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var lsb, err_1, os, err_2, file, os; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
var lsbOut, osOut, releaseOut; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_b.trys.push([0, 2, , 12]); | ||
if (!!util_1.isNullOrUndefined(resolve_config_1.default('USE_LINUX_LSB_RELEASE'))) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, tryLSBRelease()]; | ||
case 1: return [2 /*return*/, (_a.sent())]; | ||
case 2: | ||
if (!!util_1.isNullOrUndefined(resolve_config_1.default('USE_LINUX_OS_RELEASE'))) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, tryOSRelease()]; | ||
case 3: return [2 /*return*/, (_a.sent())]; | ||
case 4: | ||
if (!!util_1.isNullOrUndefined(resolve_config_1.default('USE_LINUX_ANYFILE_RELEASE'))) return [3 /*break*/, 6]; | ||
return [4 /*yield*/, tryFirstReleaseFile()]; | ||
case 5: return [2 /*return*/, (_a.sent())]; | ||
case 6: return [4 /*yield*/, tryLSBRelease()]; | ||
case 7: | ||
lsbOut = _a.sent(); | ||
if (!util_1.isNullOrUndefined(lsbOut)) { | ||
return [2 /*return*/, lsbOut]; | ||
} | ||
return [4 /*yield*/, tryOSRelease()]; | ||
case 8: | ||
osOut = _a.sent(); | ||
if (!util_1.isNullOrUndefined(osOut)) { | ||
return [2 /*return*/, osOut]; | ||
} | ||
return [4 /*yield*/, tryFirstReleaseFile()]; | ||
case 9: | ||
releaseOut = _a.sent(); | ||
if (!util_1.isNullOrUndefined(releaseOut)) { | ||
return [2 /*return*/, releaseOut]; | ||
} | ||
// if none has worked, return unkown | ||
return [2 /*return*/, { | ||
os: 'linux', | ||
dist: 'unkown', | ||
release: '', | ||
}]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Try the "lsb_release" command, and if it works, parse it | ||
*/ | ||
function tryLSBRelease() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var lsb, err_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, util_1.promisify(child_process_1.exec)('lsb_release -a')]; | ||
case 1: | ||
lsb = _b.sent(); | ||
lsb = _a.sent(); | ||
return [2 /*return*/, parseLSB(lsb.stdout)]; | ||
case 2: | ||
err_1 = _b.sent(); | ||
if (!err_1.message.match(/: not found/im)) return [3 /*break*/, 10]; | ||
_b.label = 3; | ||
case 3: | ||
_b.trys.push([3, 5, , 9]); | ||
err_1 = _a.sent(); | ||
// check if "USE_LINUX_LSB_RELEASE" is unset, when yes - just return to start the next try | ||
if (util_1.isNullOrUndefined(resolve_config_1.default('USE_LINUX_LSB_RELEASE'))) { | ||
return [2 /*return*/]; | ||
} | ||
// otherwise throw the error | ||
throw err_1; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Try to read the /etc/os-release file, and if it works, parse it | ||
*/ | ||
function tryOSRelease() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var os, err_2; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, util_1.promisify(fs_1.readFile)('/etc/os-release')]; | ||
case 4: | ||
case 1: | ||
os = _b.sent(); | ||
return [2 /*return*/, parseOS(os.toString())]; | ||
case 5: | ||
case 2: | ||
err_2 = _b.sent(); | ||
if (!(((_a = err_2) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT')) return [3 /*break*/, 8]; | ||
// check if the error is an "ENOENT" OR "SKIP_OS_RELEASE" is set | ||
// AND "USE_LINUX_OS_RELEASE" is unset | ||
// and just return | ||
if ((((_a = err_2) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT' || !util_1.isNullOrUndefined(resolve_config_1.default('SKIP_OS_RELEASE'))) && | ||
util_1.isNullOrUndefined(resolve_config_1.default('USE_LINUX_OS_RELEASE'))) { | ||
return [2 /*return*/]; | ||
} | ||
// otherwise throw the error | ||
throw err_2; | ||
case 3: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Try to read any /etc/*-release file, take the first, and if it works, parse it | ||
*/ | ||
function tryFirstReleaseFile() { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var file, os, err_3; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 3, , 4]); | ||
return [4 /*yield*/, util_1.promisify(fs_1.readdir)('/etc')]; | ||
case 6: | ||
case 1: | ||
file = (_b.sent()).filter(function (v) { | ||
return v.match(/.*-release$/im); | ||
// match if file ends with "-release" | ||
return v.match(/.*-release$/im) && | ||
// check if the file does NOT contain "lsb" | ||
!v.match(/lsb/im); | ||
})[0]; | ||
@@ -118,13 +215,17 @@ if (util_1.isNullOrUndefined(file) || file.length <= 0) { | ||
} | ||
return [4 /*yield*/, util_1.promisify(child_process_1.exec)('cat /etc/' + file)]; | ||
case 7: | ||
return [4 /*yield*/, util_1.promisify(fs_1.readFile)(path_1.join('/etc/', file))]; | ||
case 2: | ||
os = _b.sent(); | ||
return [2 /*return*/, parseOS(os.stdout)]; | ||
case 8: throw err_2; | ||
case 9: return [3 /*break*/, 11]; | ||
case 10: | ||
// throw the error in case it is not a "not found" error | ||
throw err_1; | ||
case 11: return [3 /*break*/, 12]; | ||
case 12: return [2 /*return*/]; | ||
return [2 /*return*/, parseOS(os.toString())]; | ||
case 3: | ||
err_3 = _b.sent(); | ||
// check if the error is an "ENOENT" OR "SKIP_RELEASE" is set | ||
// AND "USE_LINUX_RELEASE" is unset | ||
// and just return | ||
if (((_a = err_3) === null || _a === void 0 ? void 0 : _a.code) === 'ENOENT' && util_1.isNullOrUndefined(resolve_config_1.default('USE_LINUX_ANYFILE_RELEASE'))) { | ||
return [2 /*return*/]; | ||
} | ||
// otherwise throw the error | ||
throw err_3; | ||
case 4: return [2 /*return*/]; | ||
} | ||
@@ -131,0 +232,0 @@ }); |
@@ -138,3 +138,2 @@ "use strict"; | ||
_a.os = _b.sent(); | ||
console.log('got back:', this.os); | ||
_b.label = 2; | ||
@@ -141,0 +140,0 @@ case 2: |
{ | ||
"name": "mongodb-memory-server-core", | ||
"version": "6.1.1", | ||
"version": "6.2.0", | ||
"description": "MongoDB Server for testing (core package, without autodownload). The server will allow you to connect your favourite ODM or client library to the MongoDB Server and run parallel integration tests isolated from each other.", | ||
@@ -12,2 +12,5 @@ "main": "lib/index", | ||
}, | ||
"engines": { | ||
"node": ">=8.10.0" | ||
}, | ||
"files": [ | ||
@@ -71,8 +74,8 @@ "lib", | ||
"coverage": "cross-env MONGOMS_DOWNLOAD_DIR=./tmp jest --coverage", | ||
"lint": "yarn eslint && yarn tscheck", | ||
"eslint": "eslint 'src/**/*.{js,ts}'", | ||
"test": "npm run lint && npm run tscheck && npm run coverage", | ||
"lint": "npm run eslint && npm run tscheck", | ||
"eslint": "eslint -c ../../.eslintrc.js 'src/**/*.{js,ts}'", | ||
"test": "npm run lint && npm run coverage", | ||
"tscheck": "tsc --noEmit" | ||
}, | ||
"gitHead": "26526630e3c2a8e00acad496c42931aba2284ba1" | ||
"gitHead": "38124fea1cb263f3ab94e75a5f14e51e524c5a1c" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
318819
4477