1
1
import { ChildProcess } from 'child_process' ;
2
2
import tmp from 'tmp' ;
3
3
import getPort from 'get-port' ;
4
- import { generateDbName } from './util/db_util' ;
4
+ import { generateDbName , getUriBase } from './util/db_util' ;
5
5
import MongoInstance from './util/MongoInstance' ;
6
6
import { MongoBinaryOpts } from './util/MongoBinary' ;
7
- import {
8
- CallbackFn ,
9
- DebugFn ,
10
- MongoMemoryInstancePropT ,
11
- StorageEngineT ,
12
- SpawnOptions ,
13
- } from './types' ;
14
- import { DirResult } from 'tmp' ;
7
+ import { DebugFn , MongoMemoryInstancePropT , StorageEngineT , SpawnOptions } from './types' ;
15
8
import { isNullOrUndefined } from 'util' ;
16
9
17
10
tmp . setGracefulCleanup ( ) ;
18
11
12
+ /**
13
+ * Starting Options
14
+ */
19
15
export interface MongoMemoryServerOptsT {
20
16
instance ?: MongoMemoryInstancePropT ;
21
17
binary ?: MongoBinaryOpts ;
@@ -24,20 +20,28 @@ export interface MongoMemoryServerOptsT {
24
20
autoStart ?: boolean ;
25
21
}
26
22
27
- export interface MongoInstanceDataT {
23
+ /**
24
+ * Data used by _startUpInstance's "data" variable
25
+ */
26
+ export interface StartupInstanceData {
28
27
port : number ;
29
- dbPath : string ;
28
+ dbPath ? : string ;
30
29
dbName : string ;
31
30
ip : string ;
32
- uri : string ;
31
+ uri ? : string ;
33
32
storageEngine : StorageEngineT ;
34
- instance : MongoInstance ;
35
- childProcess : ChildProcess ;
36
- tmpDir ?: {
37
- name : string ;
38
- removeCallback : CallbackFn ;
39
- } ;
40
33
replSet ?: string ;
34
+ tmpDir ?: tmp . DirResult ;
35
+ }
36
+
37
+ /**
38
+ * Information about the currently running instance
39
+ */
40
+ export interface MongoInstanceDataT extends StartupInstanceData {
41
+ dbPath : string ; // re-declare, because in this interface it is *not* optional
42
+ uri : string ; // same as above
43
+ instance : MongoInstance ;
44
+ childProcess ?: ChildProcess ;
41
45
}
42
46
43
47
export default class MongoMemoryServer {
@@ -88,7 +92,7 @@ export default class MongoMemoryServer {
88
92
* (when options.autoStart is true, this already got called)
89
93
*/
90
94
async start ( ) : Promise < boolean > {
91
- this . debug ( 'Called MongoMemoryServer.start() method: ' ) ;
95
+ this . debug ( 'Called MongoMemoryServer.start() method' ) ;
92
96
if ( this . runningInstance ) {
93
97
throw new Error (
94
98
'MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.'
@@ -108,10 +112,7 @@ export default class MongoMemoryServer {
108
112
} )
109
113
. catch ( ( err ) => {
110
114
if ( ! this . opts . debug ) {
111
- throw new Error (
112
- `${ err . message } \n\nUse debug option for more info: ` +
113
- `new MongoMemoryServer({ debug: true })`
114
- ) ;
115
+ console . warn ( 'Starting the instance failed, please enable "debug" for more infomation' ) ;
115
116
}
116
117
throw err ;
117
118
} ) ;
@@ -127,26 +128,26 @@ export default class MongoMemoryServer {
127
128
* @private
128
129
*/
129
130
async _startUpInstance ( ) : Promise < MongoInstanceDataT > {
130
- const data : any = { } ;
131
- let tmpDir : DirResult ;
132
-
133
- const instOpts = this . opts . instance || { } ;
134
- data . port = await getPort ( { port : instOpts . port || undefined } ) ;
135
- data . dbName = generateDbName ( instOpts . dbName ) ;
136
- data . ip = instOpts . ip || '127.0.0.1' ;
137
- data . uri = await this . _getUriBase ( data . ip , data . port , data . dbName ) ;
138
- data . storageEngine = instOpts . storageEngine || 'ephemeralForTest' ;
139
- data . replSet = instOpts . replSet ;
140
- if ( instOpts . dbPath ) {
141
- data . dbPath = instOpts . dbPath ;
142
- } else {
143
- tmpDir = tmp . dirSync ( {
131
+ /** Shortcut to this.opts.instance */
132
+ const instOpts = this . opts . instance ?? { } ;
133
+ const data : StartupInstanceData = {
134
+ port : await getPort ( { port : instOpts . port ?? undefined } ) , // do (null or undefined) to undefined
135
+ dbName : generateDbName ( instOpts . dbName ) ,
136
+ ip : instOpts . ip ?? '127.0.0.1' ,
137
+ storageEngine : instOpts . storageEngine ?? 'ephemeralForTest' ,
138
+ replSet : instOpts . replSet ,
139
+ dbPath : instOpts . dbPath ,
140
+ tmpDir : undefined ,
141
+ } ;
142
+
143
+ data . uri = await getUriBase ( data . ip , data . port , data . dbName ) ;
144
+ if ( ! data . dbPath ) {
145
+ data . tmpDir = tmp . dirSync ( {
144
146
mode : 0o755 ,
145
147
prefix : 'mongo-mem-' ,
146
148
unsafeCleanup : true ,
147
149
} ) ;
148
- data . dbPath = tmpDir . name ;
149
- data . tmpDir = tmpDir ;
150
+ data . dbPath = data . tmpDir . name ;
150
151
}
151
152
152
153
this . debug ( `Starting MongoDB instance with following options: ${ JSON . stringify ( data ) } ` ) ;
@@ -168,10 +169,14 @@ export default class MongoMemoryServer {
168
169
spawn : this . opts . spawn ,
169
170
debug : this . debug ,
170
171
} ) ;
171
- data . instance = instance ;
172
- data . childProcess = instance . childProcess ;
173
172
174
- return data ;
173
+ return {
174
+ ...data ,
175
+ dbPath : data . dbPath as string , // because otherwise the types would be incompatible
176
+ uri : data . uri as string , // same as above
177
+ instance : instance ,
178
+ childProcess : instance . childProcess ?? undefined , // convert null | undefined to undefined
179
+ } ;
175
180
}
176
181
177
182
/**
@@ -182,6 +187,7 @@ export default class MongoMemoryServer {
182
187
183
188
// just return "true" if the instance is already running / defined
184
189
if ( isNullOrUndefined ( this . runningInstance ) ) {
190
+ this . debug ( 'Instance is already stopped, returning true' ) ;
185
191
return true ;
186
192
}
187
193
@@ -230,14 +236,6 @@ export default class MongoMemoryServer {
230
236
}
231
237
}
232
238
233
- /**
234
- * Basic MongoDB Connection string
235
- * @private
236
- */
237
- _getUriBase ( host : string , port : number , dbName : string ) {
238
- return `mongodb://${ host } :${ port } /${ dbName } ?` ;
239
- }
240
-
241
239
/**
242
240
* Get a mongodb-URI for a different DataBase
243
241
* @param otherDbName Set this to "true" to generate a random DataBase name, otherwise a string to specify a DataBase name
@@ -249,10 +247,10 @@ export default class MongoMemoryServer {
249
247
if ( otherDbName ) {
250
248
if ( typeof otherDbName === 'string' ) {
251
249
// generate uri with provided DB name on existed DB instance
252
- return this . _getUriBase ( ip , port , otherDbName ) ;
250
+ return getUriBase ( ip , port , otherDbName ) ;
253
251
}
254
252
// generate new random db name
255
- return this . _getUriBase ( ip , port , generateDbName ( ) ) ;
253
+ return getUriBase ( ip , port , generateDbName ( ) ) ;
256
254
}
257
255
258
256
return uri ;
@@ -261,6 +259,7 @@ export default class MongoMemoryServer {
261
259
/**
262
260
* Get a mongodb-URI for a different DataBase
263
261
* @param otherDbName Set this to "true" to generate a random DataBase name, otherwise a string to specify a DataBase name
262
+ * @deprecated
264
263
*/
265
264
async getConnectionString ( otherDbName : string | boolean = false ) : Promise < string > {
266
265
// should this function be marked deprecated? because it is just a pass-through to getUri
0 commit comments