mongodb-memory-server 2.1.0 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -2
- package/lib/MongoMemoryReplSet.d.ts +57 -0
- package/lib/MongoMemoryReplSet.js +307 -0
- package/lib/MongoMemoryReplSet.js.flow +252 -0
- package/lib/MongoMemoryServer.d.ts +47 -0
- package/lib/MongoMemoryServer.js +76 -78
- package/lib/MongoMemoryServer.js.flow +22 -26
- package/lib/__tests__/multipleDB-test.js +68 -0
- package/lib/__tests__/replset-test.js +87 -0
- package/lib/__tests__/singleDB-test.js +43 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +32 -14
- package/lib/index.js.flow +2 -1
- package/lib/types.d.ts +49 -0
- package/lib/types.js +1 -0
- package/lib/types.js.flow +60 -0
- package/lib/util/MongoBinary.d.ts +23 -0
- package/lib/util/MongoBinary.js +46 -42
- package/lib/util/MongoBinary.js.flow +13 -6
- package/lib/util/MongoBinaryDownload.d.ts +33 -0
- package/lib/util/MongoBinaryDownload.js +84 -73
- package/lib/util/MongoBinaryDownload.js.flow +5 -11
- package/lib/util/MongoBinaryDownloadUrl.d.ts +35 -0
- package/lib/util/MongoBinaryDownloadUrl.js +68 -31
- package/lib/util/MongoBinaryDownloadUrl.js.flow +12 -17
- package/lib/util/MongoInstance.d.ts +49 -0
- package/lib/util/MongoInstance.js +50 -32
- package/lib/util/MongoInstance.js.flow +12 -17
- package/lib/util/__tests__/MongoBinary-test.js +52 -0
- package/lib/util/__tests__/MongoBinaryDownload-test.js +42 -0
- package/lib/util/__tests__/MongoBinaryDownloadUrl-test.js +179 -0
- package/lib/util/__tests__/MongoInstance-test.js +133 -0
- package/lib/util/db_util.d.ts +20 -0
- package/lib/util/db_util.js +56 -0
- package/lib/util/db_util.js.flow +29 -0
- package/lib/util/mongo_killer.d.ts +0 -0
- package/lib/util/mongo_killer.js +5 -5
- package/package.json +36 -23
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
[](http://www.npmtrends.com/mongodb-memory-server)
|
|
6
6
|
[](https://travis-ci.org/nodkz/mongodb-memory-server)
|
|
7
7
|
[](http://commitizen.github.io/cz-cli/)
|
|
8
|
+

|
|
8
9
|
|
|
9
10
|
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.
|
|
10
11
|
|
|
@@ -47,11 +48,14 @@ All options are optional.
|
|
|
47
48
|
const mongod = new MongodbMemoryServer({
|
|
48
49
|
instance: {
|
|
49
50
|
port?: ?number, // by default choose any free port
|
|
50
|
-
ip?: string, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`,
|
|
51
|
+
ip?: string, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`,
|
|
51
52
|
dbName?: string, // by default generate random dbName
|
|
52
53
|
dbPath?: string, // by default create in temp directory
|
|
53
|
-
storageEngine?: string, // by default `ephemeralForTest
|
|
54
|
+
storageEngine?: string, // by default `ephemeralForTest`, available engines: [ 'devnull', 'ephemeralForTest', 'mmapv1', 'wiredTiger' ]
|
|
54
55
|
debug?: boolean, // by default false
|
|
56
|
+
replSet?: string, // by default no replica set, replica set name
|
|
57
|
+
auth?: boolean, // by default `mongod` is started with '--noauth', start `mongod` with '--auth'
|
|
58
|
+
args?: string[], // by default no additional arguments, any additional command line arguments for `mongod` `mongod` (ex. ['--notablescan'])
|
|
55
59
|
},
|
|
56
60
|
binary: {
|
|
57
61
|
version?: string, // by default 'latest'
|
|
@@ -64,6 +68,64 @@ const mongod = new MongodbMemoryServer({
|
|
|
64
68
|
autoStart?: boolean, // by default true
|
|
65
69
|
});
|
|
66
70
|
```
|
|
71
|
+
Also you can use the environment variables for configure installation process
|
|
72
|
+
```
|
|
73
|
+
MONGOMS_DOWNLOAD_DIR=/path/to/mongodb/binaries
|
|
74
|
+
MONGOMS_PLATFORM=linux
|
|
75
|
+
MONGOMS_ARCH=x64
|
|
76
|
+
MONGOMS_VERSION=3
|
|
77
|
+
MONGOMS_DEBUG=1 # also available case-insensitive values: "on" "yes" "true"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Replica Set start:
|
|
81
|
+
```js
|
|
82
|
+
import { MongoMemoryReplSet } from 'mongodb-memory-server';
|
|
83
|
+
|
|
84
|
+
const replSet = new MongoMemoryReplSet();
|
|
85
|
+
await replSet.waitUntilRunning();
|
|
86
|
+
const uri = await mongod.getConnectionString();
|
|
87
|
+
const port = await mongod.getPort();
|
|
88
|
+
const dbPath = await mongod.getDbPath();
|
|
89
|
+
const dbName = await mongod.getDbName();
|
|
90
|
+
|
|
91
|
+
// some code
|
|
92
|
+
|
|
93
|
+
// stop replica set manually
|
|
94
|
+
replSet.stop();
|
|
95
|
+
// or it should be stopped automatically when you exit from script
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Available options
|
|
99
|
+
All options are optional.
|
|
100
|
+
```js
|
|
101
|
+
const replSet = new MongoMemoryReplSet({
|
|
102
|
+
autoStart, // same as for MongoMemoryServer
|
|
103
|
+
binary: binaryOpts, // same as for MongoMemoryServer
|
|
104
|
+
debug, // same as for MongoMemoryServer
|
|
105
|
+
instanceOpts: [
|
|
106
|
+
{
|
|
107
|
+
args, // any additional instance specific args
|
|
108
|
+
port, // port number for the instance
|
|
109
|
+
dbPath, // path to database files for this instance
|
|
110
|
+
storageEngine, // same storage engine options
|
|
111
|
+
},
|
|
112
|
+
// each entry will result in a MongoMemoryServer
|
|
113
|
+
],
|
|
114
|
+
// unless otherwise noted below these values will be in common with all instances spawned.
|
|
115
|
+
replSet: {
|
|
116
|
+
name, // replica set name (default: 'testset')
|
|
117
|
+
auth, // enable auth support? (default: false)
|
|
118
|
+
args, // any args specified here will be combined with any per instance args from `instanceOpts`
|
|
119
|
+
count, // number of `mongod` processes to start; (default: 1)
|
|
120
|
+
dbName, // default database for db URI strings. (default: uuid.v4())
|
|
121
|
+
ip, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`
|
|
122
|
+
oplogSize, // size (in MB) for the oplog; (default: 1)
|
|
123
|
+
spawn, // spawn options when creating the child processes
|
|
124
|
+
storageEngine, // default storage engine for instance. (Can be overridden per instance)
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
67
129
|
|
|
68
130
|
### Simple test with MongoClient
|
|
69
131
|
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/// <reference types='node' />
|
|
2
|
+
import { SpawnOptions } from 'child_process';
|
|
3
|
+
import * as events from 'events';
|
|
4
|
+
import {
|
|
5
|
+
DebugFn,
|
|
6
|
+
MongoMemoryInstancePropT,
|
|
7
|
+
MongoMemoryInstancePropBaseT,
|
|
8
|
+
StorageEngineT,
|
|
9
|
+
} from './types';
|
|
10
|
+
import MongoMemoryServer from './MongoMemoryServer';
|
|
11
|
+
import { MongoBinaryOpts } from './util/MongoBinary';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Replica set specific options.
|
|
15
|
+
*
|
|
16
|
+
* @property {boolean} auth enable auth; (default: false)
|
|
17
|
+
* @property {string[]} args additional command line args passed to `mongod`
|
|
18
|
+
* @property {number} count number of `mongod` servers to start (default: 1)
|
|
19
|
+
* @property {string} dbName database name used in connection string
|
|
20
|
+
* @property {string} ip bind to all IP addresses specify `::,0.0.0.0`; (default '127.0.0.1')
|
|
21
|
+
* @property {string} name replSet name (default: 'testset')
|
|
22
|
+
* @property {number} oplogSize oplog size (in MB); (default: 1)
|
|
23
|
+
* @property {StorageEngineT} storageEngine `mongod` storage engine type; (default: 'ephemeralForTest')
|
|
24
|
+
*/
|
|
25
|
+
export interface ReplSetOpts {
|
|
26
|
+
auth: boolean;
|
|
27
|
+
args: string[];
|
|
28
|
+
count: number;
|
|
29
|
+
dbName: string;
|
|
30
|
+
ip: string;
|
|
31
|
+
name: string;
|
|
32
|
+
oplogSize: number;
|
|
33
|
+
spawn: SpawnOptions;
|
|
34
|
+
storageEngine: StorageEngineT;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface MongoMemoryReplSetOptsT {
|
|
38
|
+
instanceOpts: MongoMemoryInstancePropBaseT[];
|
|
39
|
+
binary: MongoBinaryOpts;
|
|
40
|
+
replSet: ReplSetOpts;
|
|
41
|
+
autoStart?: boolean;
|
|
42
|
+
debug?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default class MongoMemoryReplSet extends events.EventEmitter {
|
|
46
|
+
servers: MongoMemoryServer[];
|
|
47
|
+
opts: MongoMemoryReplSetOptsT;
|
|
48
|
+
debug: DebugFn;
|
|
49
|
+
_state: 'init' | 'running' | 'stopped';
|
|
50
|
+
getConnectionString(otherDb?: string | boolean): Promise<string>;
|
|
51
|
+
getDbName(): Promise<string>;
|
|
52
|
+
getInstanceOpts(baseOpts: MongoMemoryInstancePropBaseT): MongoMemoryInstancePropT;
|
|
53
|
+
getUri(otherDb?: string | boolean): Promise<string>;
|
|
54
|
+
start(): Promise<void>;
|
|
55
|
+
stop(): Promise<boolean>;
|
|
56
|
+
waitUntilRunning(): Promise<void>;
|
|
57
|
+
}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _events = _interopRequireDefault(require("events"));
|
|
9
|
+
|
|
10
|
+
var _mongodb = require("mongodb");
|
|
11
|
+
|
|
12
|
+
var _MongoMemoryServer = _interopRequireDefault(require("./MongoMemoryServer"));
|
|
13
|
+
|
|
14
|
+
var _db_util = require("./util/db_util");
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
19
|
+
|
|
20
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
21
|
+
|
|
22
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
23
|
+
|
|
24
|
+
class MongoMemoryReplSet extends _events.default.EventEmitter {
|
|
25
|
+
constructor(opts = {}) {
|
|
26
|
+
super();
|
|
27
|
+
|
|
28
|
+
_defineProperty(this, "servers", void 0);
|
|
29
|
+
|
|
30
|
+
_defineProperty(this, "opts", void 0);
|
|
31
|
+
|
|
32
|
+
_defineProperty(this, "debug", void 0);
|
|
33
|
+
|
|
34
|
+
_defineProperty(this, "_state", void 0);
|
|
35
|
+
|
|
36
|
+
const replSetDefaults = {
|
|
37
|
+
auth: false,
|
|
38
|
+
args: [],
|
|
39
|
+
name: 'testset',
|
|
40
|
+
count: 1,
|
|
41
|
+
dbName: (0, _db_util.generateDbName)(),
|
|
42
|
+
ip: '127.0.0.1',
|
|
43
|
+
oplogSize: 1,
|
|
44
|
+
spawn: {},
|
|
45
|
+
storageEngine: 'ephemeralForTest'
|
|
46
|
+
};
|
|
47
|
+
this._state = 'stopped';
|
|
48
|
+
this.opts = {
|
|
49
|
+
binary: opts.binary || {},
|
|
50
|
+
debug: !!opts.debug,
|
|
51
|
+
instanceOpts: opts.instanceOpts || [],
|
|
52
|
+
replSet: Object.assign(replSetDefaults, opts.replSet)
|
|
53
|
+
};
|
|
54
|
+
this.opts.replSet.args.push('--oplogSize', `${this.opts.replSet.oplogSize}`);
|
|
55
|
+
|
|
56
|
+
this.debug = (...args) => {
|
|
57
|
+
if (!this.opts.debug) return;
|
|
58
|
+
console.log(...args);
|
|
59
|
+
}; // auto start by default
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
if (opts.autoStart || !('autoStart' in opts)) {
|
|
63
|
+
this.debug('Autostarting MongoMemoryReplSet.');
|
|
64
|
+
setTimeout(() => this.start(), 0);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
process.on('beforeExit', () => this.stop());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getConnectionString(otherDb) {
|
|
71
|
+
var _this = this;
|
|
72
|
+
|
|
73
|
+
return _asyncToGenerator(function* () {
|
|
74
|
+
return _this.getUri(otherDb);
|
|
75
|
+
})();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns database name.
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
getDbName() {
|
|
83
|
+
var _this2 = this;
|
|
84
|
+
|
|
85
|
+
return _asyncToGenerator(function* () {
|
|
86
|
+
// this function is only async for consistency with MongoMemoryServer
|
|
87
|
+
// I don't see much point to either of them being async but don't
|
|
88
|
+
// care enough to change it and introduce a breaking change.
|
|
89
|
+
return _this2.opts.replSet.dbName;
|
|
90
|
+
})();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns instance options suitable for a MongoMemoryServer.
|
|
94
|
+
* @param {MongoMemoryInstancePropBaseT} baseOpts
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
getInstanceOpts(baseOpts = {}) {
|
|
99
|
+
const rsOpts = this.opts.replSet;
|
|
100
|
+
const opts = {
|
|
101
|
+
auth: !!rsOpts.auth,
|
|
102
|
+
args: rsOpts.args,
|
|
103
|
+
dbName: rsOpts.dbName,
|
|
104
|
+
ip: rsOpts.ip,
|
|
105
|
+
replSet: rsOpts.name
|
|
106
|
+
};
|
|
107
|
+
if (baseOpts.args) opts.args = rsOpts.args.concat(baseOpts.args);
|
|
108
|
+
if (baseOpts.port) opts.port = baseOpts.port;
|
|
109
|
+
if (baseOpts.dbPath) opts.dbPath = baseOpts.dbPath;
|
|
110
|
+
if (baseOpts.storageEngine) opts.storageEngine = baseOpts.storageEngine;
|
|
111
|
+
this.debug(' instance opts:', opts);
|
|
112
|
+
return opts;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Returns a mongodb: URI to connect to a given database.
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
getUri(otherDb) {
|
|
120
|
+
var _this3 = this;
|
|
121
|
+
|
|
122
|
+
return _asyncToGenerator(function* () {
|
|
123
|
+
if (_this3._state === 'init') {
|
|
124
|
+
yield _this3._waitForPrimary();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (_this3._state !== 'running') {
|
|
128
|
+
throw new Error('Replica Set is not running. Use opts.debug for more info.');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let dbName;
|
|
132
|
+
|
|
133
|
+
if (otherDb) {
|
|
134
|
+
dbName = typeof otherDb === 'string' ? otherDb : (0, _db_util.generateDbName)();
|
|
135
|
+
} else {
|
|
136
|
+
dbName = _this3.opts.replSet.dbName;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const ports = yield Promise.all(_this3.servers.map(s => s.getPort()));
|
|
140
|
+
const hosts = ports.map(port => `127.0.0.1:${port}`).join(',');
|
|
141
|
+
return `mongodb://${hosts}/${dbName}`;
|
|
142
|
+
})();
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Start underlying `mongod` instances.
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
start() {
|
|
150
|
+
var _this4 = this;
|
|
151
|
+
|
|
152
|
+
return _asyncToGenerator(function* () {
|
|
153
|
+
_this4.debug('start');
|
|
154
|
+
|
|
155
|
+
if (_this4._state !== 'stopped') {
|
|
156
|
+
throw new Error(`Already in 'init' or 'running' state. Use opts.debug = true for more info.`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_this4.emit(_this4._state = 'init');
|
|
160
|
+
|
|
161
|
+
_this4.debug('init'); // Any servers defined within `opts.instanceOpts` should be started first as
|
|
162
|
+
// the user could have specified a `dbPath` in which case we would want to perform
|
|
163
|
+
// the `replSetInitiate` command against that server.
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
const servers = _this4.opts.instanceOpts.map(opts => {
|
|
167
|
+
_this4.debug(' starting server from instanceOpts:', opts, '...');
|
|
168
|
+
|
|
169
|
+
return _this4._startServer(_this4.getInstanceOpts(opts));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
while (servers.length < _this4.opts.replSet.count) {
|
|
173
|
+
_this4.debug(' starting a server due to count...');
|
|
174
|
+
|
|
175
|
+
const server = _this4._startServer(_this4.getInstanceOpts({}));
|
|
176
|
+
|
|
177
|
+
servers.push(server);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
_this4.servers = servers; // Brief delay to wait for servers to start up.
|
|
181
|
+
|
|
182
|
+
yield new Promise(resolve => setTimeout(resolve, 1000));
|
|
183
|
+
yield _this4._initReplSet();
|
|
184
|
+
})();
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Stop the underlying `mongod` instance(s).
|
|
188
|
+
*/
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
stop() {
|
|
192
|
+
var _this5 = this;
|
|
193
|
+
|
|
194
|
+
return _asyncToGenerator(function* () {
|
|
195
|
+
if (_this5._state === 'stopped') return false;
|
|
196
|
+
const servers = _this5.servers;
|
|
197
|
+
_this5.servers = [];
|
|
198
|
+
return Promise.all(servers.map(s => s.stop())).then(() => {
|
|
199
|
+
_this5.emit(_this5._state = 'stopped');
|
|
200
|
+
|
|
201
|
+
return true;
|
|
202
|
+
}).catch(err => {
|
|
203
|
+
_this5.debug(err);
|
|
204
|
+
|
|
205
|
+
_this5.emit(_this5._state = 'stopped', err);
|
|
206
|
+
|
|
207
|
+
return false;
|
|
208
|
+
});
|
|
209
|
+
})();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
waitUntilRunning() {
|
|
213
|
+
var _this6 = this;
|
|
214
|
+
|
|
215
|
+
return _asyncToGenerator(function* () {
|
|
216
|
+
if (_this6._state === 'running') return;
|
|
217
|
+
yield new Promise(resolve => _this6.once('running', () => resolve()));
|
|
218
|
+
})();
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Connects to the first server from the list of servers and issues the `replSetInitiate`
|
|
222
|
+
* command passing in a new replica set configuration object.
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
_initReplSet() {
|
|
227
|
+
var _this7 = this;
|
|
228
|
+
|
|
229
|
+
return _asyncToGenerator(function* () {
|
|
230
|
+
if (_this7._state !== 'init') {
|
|
231
|
+
throw new Error('Not in init phase.');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
_this7.debug('Initializing replica set.');
|
|
235
|
+
|
|
236
|
+
if (!_this7.servers.length) {
|
|
237
|
+
throw new Error('One or more server is required.');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const uris = yield Promise.all(_this7.servers.map(server => server.getUri()));
|
|
241
|
+
const conn = yield _mongodb.MongoClient.connect(uris[0], {
|
|
242
|
+
useNewUrlParser: true
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
const db = yield conn.db(_this7.opts.replSet.dbName);
|
|
247
|
+
const admin = db.admin();
|
|
248
|
+
const members = uris.map((uri, idx) => ({
|
|
249
|
+
_id: idx,
|
|
250
|
+
host: (0, _db_util.getHost)(uri)
|
|
251
|
+
}));
|
|
252
|
+
const rsConfig = {
|
|
253
|
+
_id: _this7.opts.replSet.name,
|
|
254
|
+
members
|
|
255
|
+
};
|
|
256
|
+
yield admin.command({
|
|
257
|
+
replSetInitiate: rsConfig
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
_this7.debug('Waiting for replica set to have a PRIMARY member.');
|
|
261
|
+
|
|
262
|
+
yield _this7._waitForPrimary(admin);
|
|
263
|
+
|
|
264
|
+
_this7.emit(_this7._state = 'running');
|
|
265
|
+
|
|
266
|
+
_this7.debug('running');
|
|
267
|
+
} finally {
|
|
268
|
+
yield conn.close();
|
|
269
|
+
}
|
|
270
|
+
})();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
_startServer(instanceOpts) {
|
|
274
|
+
const serverOpts = {
|
|
275
|
+
autoStart: true,
|
|
276
|
+
debug: this.opts.debug,
|
|
277
|
+
binary: this.opts.binary,
|
|
278
|
+
instance: instanceOpts,
|
|
279
|
+
spawn: this.opts.replSet.spawn
|
|
280
|
+
};
|
|
281
|
+
const server = new _MongoMemoryServer.default(serverOpts);
|
|
282
|
+
return server;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
_waitForPrimary(db) {
|
|
286
|
+
var _this8 = this;
|
|
287
|
+
|
|
288
|
+
return _asyncToGenerator(function* () {
|
|
289
|
+
const replStatus = yield (0, _db_util.getReplStatus)(db);
|
|
290
|
+
|
|
291
|
+
_this8.debug(' replStatus:', replStatus);
|
|
292
|
+
|
|
293
|
+
const hasPrimary = replStatus.members.some(m => m.stateStr === 'PRIMARY');
|
|
294
|
+
|
|
295
|
+
if (!hasPrimary) {
|
|
296
|
+
_this8.debug('No PRIMARY yet. Waiting...');
|
|
297
|
+
|
|
298
|
+
return new Promise(resolve => setTimeout(() => resolve(_this8._waitForPrimary(db)), 1000));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return true;
|
|
302
|
+
})();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
exports.default = MongoMemoryReplSet;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import events from 'events';
|
|
3
|
+
import { MongoClient } from 'mongodb';
|
|
4
|
+
import MongoMemoryServer from './MongoMemoryServer';
|
|
5
|
+
import type { MongoMemoryServerOptsT } from './MongoMemoryServer';
|
|
6
|
+
import { generateDbName, getHost, getReplStatus } from './util/db_util';
|
|
7
|
+
import type { MongoBinaryOpts } from './util/MongoBinary';
|
|
8
|
+
import type {
|
|
9
|
+
DebugFn,
|
|
10
|
+
MongoMemoryInstancePropT,
|
|
11
|
+
MongoMemoryInstancePropBaseT,
|
|
12
|
+
SpawnOptions,
|
|
13
|
+
StorageEngineT,
|
|
14
|
+
} from './types';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Replica set specific options.
|
|
18
|
+
*
|
|
19
|
+
* @property {boolean} auth enable auth; (default: false)
|
|
20
|
+
* @property {string[]} args additional command line args passed to `mongod`
|
|
21
|
+
* @property {number} count number of `mongod` servers to start (default: 1)
|
|
22
|
+
* @property {string} dbName database name used in connection string
|
|
23
|
+
* @property {string} ip bind to all IP addresses specify `::,0.0.0.0`; (default '127.0.0.1')
|
|
24
|
+
* @property {string} name replSet name (default: 'testset')
|
|
25
|
+
* @property {number} oplogSize oplog size (in MB); (default: 1)
|
|
26
|
+
* @property {StorageEngineT} storageEngine `mongod` storage engine type; (default: 'ephemeralForTest')
|
|
27
|
+
*/
|
|
28
|
+
export interface ReplSetOpts {
|
|
29
|
+
auth: boolean;
|
|
30
|
+
args: string[];
|
|
31
|
+
count: number;
|
|
32
|
+
dbName: string;
|
|
33
|
+
ip: string;
|
|
34
|
+
name: string;
|
|
35
|
+
oplogSize: number;
|
|
36
|
+
spawn: SpawnOptions;
|
|
37
|
+
storageEngine: StorageEngineT;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface MongoMemoryReplSetOptsT {
|
|
41
|
+
instanceOpts: MongoMemoryInstancePropBaseT[];
|
|
42
|
+
binary: MongoBinaryOpts;
|
|
43
|
+
replSet: ReplSetOpts;
|
|
44
|
+
autoStart?: boolean;
|
|
45
|
+
debug?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default class MongoMemoryReplSet extends events.EventEmitter {
|
|
49
|
+
servers: MongoMemoryServer[];
|
|
50
|
+
opts: MongoMemoryReplSetOptsT;
|
|
51
|
+
debug: DebugFn;
|
|
52
|
+
_state: 'init' | 'running' | 'stopped';
|
|
53
|
+
|
|
54
|
+
constructor(opts?: $Shape<MongoMemoryReplSetOptsT> = {}) {
|
|
55
|
+
super();
|
|
56
|
+
const replSetDefaults: ReplSetOpts = {
|
|
57
|
+
auth: false,
|
|
58
|
+
args: [],
|
|
59
|
+
name: 'testset',
|
|
60
|
+
count: 1,
|
|
61
|
+
dbName: generateDbName(),
|
|
62
|
+
ip: '127.0.0.1',
|
|
63
|
+
oplogSize: 1,
|
|
64
|
+
spawn: {},
|
|
65
|
+
storageEngine: 'ephemeralForTest',
|
|
66
|
+
};
|
|
67
|
+
this._state = 'stopped';
|
|
68
|
+
this.opts = {
|
|
69
|
+
binary: opts.binary || {},
|
|
70
|
+
debug: !!opts.debug,
|
|
71
|
+
instanceOpts: opts.instanceOpts || [],
|
|
72
|
+
replSet: Object.assign(replSetDefaults, (opts.replSet: any)),
|
|
73
|
+
};
|
|
74
|
+
this.opts.replSet.args.push('--oplogSize', `${this.opts.replSet.oplogSize}`);
|
|
75
|
+
this.debug = (...args: any[]) => {
|
|
76
|
+
if (!this.opts.debug) return;
|
|
77
|
+
console.log(...args);
|
|
78
|
+
};
|
|
79
|
+
// auto start by default
|
|
80
|
+
if (opts.autoStart || !('autoStart' in opts)) {
|
|
81
|
+
this.debug('Autostarting MongoMemoryReplSet.');
|
|
82
|
+
setTimeout(() => this.start(), 0);
|
|
83
|
+
}
|
|
84
|
+
process.on('beforeExit', () => this.stop());
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async getConnectionString(otherDb?: string | boolean): Promise<string> {
|
|
88
|
+
return this.getUri(otherDb);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Returns database name.
|
|
93
|
+
*/
|
|
94
|
+
async getDbName(): Promise<string> {
|
|
95
|
+
// this function is only async for consistency with MongoMemoryServer
|
|
96
|
+
// I don't see much point to either of them being async but don't
|
|
97
|
+
// care enough to change it and introduce a breaking change.
|
|
98
|
+
return this.opts.replSet.dbName;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Returns instance options suitable for a MongoMemoryServer.
|
|
103
|
+
* @param {MongoMemoryInstancePropBaseT} baseOpts
|
|
104
|
+
*/
|
|
105
|
+
getInstanceOpts(baseOpts: MongoMemoryInstancePropBaseT = {}): MongoMemoryInstancePropT {
|
|
106
|
+
const rsOpts: ReplSetOpts = this.opts.replSet;
|
|
107
|
+
const opts: MongoMemoryInstancePropT = {
|
|
108
|
+
auth: !!rsOpts.auth,
|
|
109
|
+
args: rsOpts.args,
|
|
110
|
+
dbName: rsOpts.dbName,
|
|
111
|
+
ip: rsOpts.ip,
|
|
112
|
+
replSet: rsOpts.name,
|
|
113
|
+
};
|
|
114
|
+
if (baseOpts.args) opts.args = rsOpts.args.concat(baseOpts.args);
|
|
115
|
+
if (baseOpts.port) opts.port = baseOpts.port;
|
|
116
|
+
if (baseOpts.dbPath) opts.dbPath = baseOpts.dbPath;
|
|
117
|
+
if (baseOpts.storageEngine) opts.storageEngine = baseOpts.storageEngine;
|
|
118
|
+
this.debug(' instance opts:', opts);
|
|
119
|
+
return opts;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Returns a mongodb: URI to connect to a given database.
|
|
124
|
+
*/
|
|
125
|
+
async getUri(otherDb?: string | boolean): Promise<string> {
|
|
126
|
+
if (this._state === 'init') {
|
|
127
|
+
await this._waitForPrimary();
|
|
128
|
+
}
|
|
129
|
+
if (this._state !== 'running') {
|
|
130
|
+
throw new Error('Replica Set is not running. Use opts.debug for more info.');
|
|
131
|
+
}
|
|
132
|
+
let dbName: string;
|
|
133
|
+
if (otherDb) {
|
|
134
|
+
dbName = typeof otherDb === 'string' ? otherDb : generateDbName();
|
|
135
|
+
} else {
|
|
136
|
+
dbName = this.opts.replSet.dbName;
|
|
137
|
+
}
|
|
138
|
+
const ports = await Promise.all(this.servers.map(s => s.getPort()));
|
|
139
|
+
const hosts = ports.map(port => `127.0.0.1:${port}`).join(',');
|
|
140
|
+
return `mongodb://${hosts}/${dbName}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Start underlying `mongod` instances.
|
|
145
|
+
*/
|
|
146
|
+
async start(): Promise<void> {
|
|
147
|
+
this.debug('start');
|
|
148
|
+
if (this._state !== 'stopped') {
|
|
149
|
+
throw new Error(`Already in 'init' or 'running' state. Use opts.debug = true for more info.`);
|
|
150
|
+
}
|
|
151
|
+
this.emit((this._state = 'init'));
|
|
152
|
+
this.debug('init');
|
|
153
|
+
// Any servers defined within `opts.instanceOpts` should be started first as
|
|
154
|
+
// the user could have specified a `dbPath` in which case we would want to perform
|
|
155
|
+
// the `replSetInitiate` command against that server.
|
|
156
|
+
const servers = this.opts.instanceOpts.map(opts => {
|
|
157
|
+
this.debug(' starting server from instanceOpts:', opts, '...');
|
|
158
|
+
return this._startServer(this.getInstanceOpts(opts));
|
|
159
|
+
});
|
|
160
|
+
while (servers.length < this.opts.replSet.count) {
|
|
161
|
+
this.debug(' starting a server due to count...');
|
|
162
|
+
const server = this._startServer(this.getInstanceOpts({}));
|
|
163
|
+
servers.push(server);
|
|
164
|
+
}
|
|
165
|
+
this.servers = servers;
|
|
166
|
+
// Brief delay to wait for servers to start up.
|
|
167
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
168
|
+
await this._initReplSet();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Stop the underlying `mongod` instance(s).
|
|
173
|
+
*/
|
|
174
|
+
async stop(): Promise<boolean> {
|
|
175
|
+
if (this._state === 'stopped') return false;
|
|
176
|
+
const servers = this.servers;
|
|
177
|
+
this.servers = [];
|
|
178
|
+
return Promise.all(servers.map(s => s.stop()))
|
|
179
|
+
.then(() => {
|
|
180
|
+
this.emit((this._state = 'stopped'));
|
|
181
|
+
return true;
|
|
182
|
+
})
|
|
183
|
+
.catch(err => {
|
|
184
|
+
this.debug(err);
|
|
185
|
+
this.emit((this._state = 'stopped'), err);
|
|
186
|
+
return false;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async waitUntilRunning() {
|
|
191
|
+
if (this._state === 'running') return;
|
|
192
|
+
await new Promise(resolve => this.once('running', () => resolve()));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Connects to the first server from the list of servers and issues the `replSetInitiate`
|
|
197
|
+
* command passing in a new replica set configuration object.
|
|
198
|
+
*/
|
|
199
|
+
async _initReplSet(): Promise<void> {
|
|
200
|
+
if (this._state !== 'init') {
|
|
201
|
+
throw new Error('Not in init phase.');
|
|
202
|
+
}
|
|
203
|
+
this.debug('Initializing replica set.');
|
|
204
|
+
if (!this.servers.length) {
|
|
205
|
+
throw new Error('One or more server is required.');
|
|
206
|
+
}
|
|
207
|
+
const uris = await Promise.all(this.servers.map(server => server.getUri()));
|
|
208
|
+
const conn = await MongoClient.connect(
|
|
209
|
+
uris[0],
|
|
210
|
+
{ useNewUrlParser: true }
|
|
211
|
+
);
|
|
212
|
+
try {
|
|
213
|
+
const db = await conn.db(this.opts.replSet.dbName);
|
|
214
|
+
const admin = db.admin();
|
|
215
|
+
const members = uris.map((uri, idx) => ({ _id: idx, host: getHost(uri) }));
|
|
216
|
+
const rsConfig = {
|
|
217
|
+
_id: this.opts.replSet.name,
|
|
218
|
+
members,
|
|
219
|
+
};
|
|
220
|
+
await admin.command({ replSetInitiate: rsConfig });
|
|
221
|
+
this.debug('Waiting for replica set to have a PRIMARY member.');
|
|
222
|
+
await this._waitForPrimary(admin);
|
|
223
|
+
this.emit((this._state = 'running'));
|
|
224
|
+
this.debug('running');
|
|
225
|
+
} finally {
|
|
226
|
+
await conn.close();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
_startServer(instanceOpts: MongoMemoryInstancePropT): MongoMemoryServer {
|
|
231
|
+
const serverOpts: MongoMemoryServerOptsT = {
|
|
232
|
+
autoStart: true,
|
|
233
|
+
debug: this.opts.debug,
|
|
234
|
+
binary: this.opts.binary,
|
|
235
|
+
instance: instanceOpts,
|
|
236
|
+
spawn: (this.opts.replSet.spawn: any),
|
|
237
|
+
};
|
|
238
|
+
const server = new MongoMemoryServer(serverOpts);
|
|
239
|
+
return server;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async _waitForPrimary(db: any): Promise<boolean> {
|
|
243
|
+
const replStatus = await getReplStatus(db);
|
|
244
|
+
this.debug(' replStatus:', replStatus);
|
|
245
|
+
const hasPrimary = replStatus.members.some(m => m.stateStr === 'PRIMARY');
|
|
246
|
+
if (!hasPrimary) {
|
|
247
|
+
this.debug('No PRIMARY yet. Waiting...');
|
|
248
|
+
return new Promise(resolve => setTimeout(() => resolve(this._waitForPrimary(db)), 1000));
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
}
|