@@ -30,7 +30,11 @@ const uri = await mongod.getConnectionString();
3030const port = await mongod .getPort ();
3131const dbPath = await mongod .getDbPath ();
3232
33+ // some code
34+
35+ // you may stop mongod manually
3336mongod .stop ();
37+ // or it will be stopped automatically when you exit from script
3438```
3539
3640### Provide connection string to mongoose
@@ -78,6 +82,14 @@ mongoose.Promise = Promise;
7882const mongoServer1 = new MongoMemoryServer ();
7983const mongoServer2 = new MongoMemoryServer ();
8084
85+ // Firstly create connection objects, which you may import in other files and create mongoose models.
86+ // Connection to databases will be estimated later (after model creation).
87+ const connections = {
88+ conn1: mongoose .createConnection (),
89+ conn2: mongoose .createConnection (),
90+ conn3: mongoose .createConnection (),
91+ };
92+
8193const mongooseOpts = {
8294 server: {
8395 promiseLibrary = Promise ;
@@ -87,41 +99,25 @@ const mongooseOpts = {
8799 },
88100};
89101
90- function addEvents (connection , mongoUri ) {
91- connection .on (' error' , (e ) => {
92- if (e .message .code === ' ETIMEDOUT' ) {
93- console .log (e);
94- connect (mongoUri, mongooseOpts);
95- }
96- console .log (e);
97- });
98-
102+ mongoServer1 .getConnectionString (' server1_db1' ).then ((mongoUri ) => {
103+ connections .conn1 .open (mongoUri, mongooseOpts);
99104 connection .once (' open' , () => {
100105 console .log (` MongoDB successfully connected to ${ mongoUri} ` );
101106 });
102- }
103-
104- // Firstly create connection objects, which you may import in other files and create mongoose models.
105- // Connection to databases will be estimated later (after model creation).
106- const connections = {
107- conn1: mongoose .createConnection (),
108- conn2: mongoose .createConnection (),
109- conn3: mongoose .createConnection (),
110- };
111-
112- mongoServer1 .getConnectionString (' server1_db1' ).then ((mongoUri ) => {
113- connections .conn1 .connect (mongoUri, mongooseOpts);
114- addEvents (connections .conn1 , mongoUri);
115107});
116108
117109mongoServer1 .getConnectionString (' server1_db2' ).then ((mongoUri ) => {
118- connections .conn2 .connect (mongoUri, mongooseOpts);
119- addEvents (connections .conn2 , mongoUri);
110+ connections .conn2 .open (mongoUri, mongooseOpts);
111+ connection .once (' open' , () => {
112+ console .log (` MongoDB successfully connected to ${ mongoUri} ` );
113+ });
120114});
121115
122116mongoServer2 .getConnectionString (' server2_db' ).then ((mongoUri ) => {
123- connections .conn3 .connect (mongoUri, mongooseOpts);
124- addEvents (connections .conn3 , mongoUri);
117+ connections .conn3 .open (mongoUri, mongooseOpts);
118+ connection .once (' open' , () => {
119+ console .log (` MongoDB successfully connected to ${ mongoUri} ` );
120+ });
125121});
126122
127123export default connections ;
@@ -147,6 +143,24 @@ export default {
147143}
148144```
149145
146+ Note: When you create mongoose connection manually, you should do:
147+ ``` js
148+ import mongoose from ' mongoose' ;
149+
150+ const conn = mongoose .createConnection (); // just create connection instance
151+ const User = conn .model (' User' , new mongoose.Schema ({ name: String })); // define model
152+ conn .open (uri, opts); // open connection to database (NOT `connect` method!)
153+ ```
154+ With default connection:
155+ ``` js
156+ import mongoose from ' mongoose' ;
157+
158+ mongoose .connect (uri, opts);
159+ const User = mongoose .model (' User' , new mongoose.Schema ({ name: String })); // define model
160+ ```
161+
162+
163+
150164### Simple Mocha test example
151165``` js
152166import mongoose from ' mongoose' ;
0 commit comments