@@ -12,6 +12,7 @@ export type MongoMemoryServerOptsT = {
1212 storageEngine ?: string ,
1313 dbPath ?: string ,
1414 autoStart ?: boolean ,
15+ debug ?: boolean ,
1516} ;
1617
1718export type MongoInstanceDataT = {
@@ -22,8 +23,8 @@ export type MongoInstanceDataT = {
2223 mongodCli : MongodHelper ,
2324} ;
2425
25- async function generateConnectionString ( port : string , dbName : ?string ) : string {
26- const db = dbName || await uuid ( ) ;
26+ async function generateConnectionString ( port : string , dbName : ?string ) : Promise < string > {
27+ const db = dbName || ( await uuid ( ) ) ;
2728 return `mongodb://localhost:${ port } /${ db } ` ;
2829}
2930
@@ -37,17 +38,19 @@ export default class MongoDBMemoryServer {
3738 this . opts = opts ;
3839
3940 // autoStart by default
40- if ( ! opts . hasOwnProperty ( 'autoStart' ) || opts . autoStart ) { // eslint-disable-line
41+ if ( ! opts . hasOwnProperty ( 'autoStart' ) || opts . autoStart ) {
4142 if ( opts . debug ) {
4243 console . log ( 'Autostarting MongoDB instance...' ) ;
4344 }
4445 this . start ( ) ;
4546 }
4647 }
4748
48- async start ( ) : Promise < MongoInstanceDataT > {
49+ async start ( ) : Promise < boolean > {
4950 if ( this . runningInstance ) {
50- throw new Error ( 'MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.' ) ;
51+ throw new Error (
52+ 'MongoDB instance already in status startup/running/error. Use opts.debug = true for more info.'
53+ ) ;
5154 }
5255
5356 this . runningInstance = Promise . resolve ( ) . then ( async ( ) => {
@@ -68,14 +71,15 @@ export default class MongoDBMemoryServer {
6871 console . log ( `Starting MongoDB instance with following options: ${ JSON . stringify ( data ) } ` ) ;
6972 }
7073
71- const mongodCli = new MongodHelper (
72- [
73- '--port' , data . port ,
74- '--storageEngine' , data . storageEngine ,
75- '--dbpath' , data . dbPath ,
76- '--noauth' ,
77- ]
78- ) ;
74+ const mongodCli = new MongodHelper ( [
75+ '--port' ,
76+ data . port ,
77+ '--storageEngine' ,
78+ data . storageEngine ,
79+ '--dbpath' ,
80+ data . dbPath ,
81+ '--noauth' ,
82+ ] ) ;
7983
8084 mongodCli . debug . enabled = this . opts . debug ;
8185
@@ -88,14 +92,19 @@ export default class MongoDBMemoryServer {
8892
8993 return data ;
9094 } ) ;
95+
96+ return this . runningInstance . then ( ( ) => true ) ;
9197 }
9298
93- async stop ( ) {
99+ async stop ( ) : Promise < boolean > {
94100 const { mongodCli , port , tmpDir } = await this . getInstanceData ( ) ;
95101
96- if ( mongodCli && mongodCli . mongoBin . childProcess ) { // .mongoBin.childProcess.connected
102+ if ( mongodCli && mongodCli . mongoBin . childProcess ) {
103+ // .mongoBin.childProcess.connected
97104 if ( this . opts . debug ) {
98- console . log ( `Shutdown MongoDB server on port ${ port } with pid ${ mongodCli . mongoBin . childProcess . pid } ` ) ;
105+ console . log (
106+ `Shutdown MongoDB server on port ${ port } with pid ${ mongodCli . mongoBin . childProcess . pid } `
107+ ) ;
99108 }
100109 mongodCli . mongoBin . childProcess . kill ( ) ;
101110 }
@@ -108,16 +117,19 @@ export default class MongoDBMemoryServer {
108117 }
109118
110119 this . runningInstance = null ;
120+ return true ;
111121 }
112122
113123 async getInstanceData ( ) : Promise < MongoInstanceDataT > {
114124 if ( this . runningInstance ) {
115125 return this . runningInstance ;
116126 }
117- 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.' ) ;
127+ throw new Error (
128+ '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.'
129+ ) ;
118130 }
119131
120- async getUri ( otherDbName ?: string | boolean = false ) : Promise < string > {
132+ async getUri ( otherDbName ?: string | boolean = false ) : Promise < string > {
121133 const { uri, port } = await this . getInstanceData ( ) ;
122134
123135 // IF true OR string
@@ -133,7 +145,7 @@ export default class MongoDBMemoryServer {
133145 return uri ;
134146 }
135147
136- async getConnectionString ( otherDbName = false ) : Promise < string > {
148+ async getConnectionString ( otherDbName : string | boolean = false ) : Promise < string > {
137149 return this . getUri ( otherDbName ) ;
138150 }
139151
0 commit comments