Skip to content

Commit 62b3f45

Browse files
authored
Merge pull request #14150 from Automattic/vkarpov15/gh-13879
docs(connections): add example of registering connection event handlers
2 parents c463e45 + 95a0d6d commit 62b3f45

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

docs/connections.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,42 @@ connection may emit.
267267

268268
* `connecting`: Emitted when Mongoose starts making its initial connection to the MongoDB server
269269
* `connected`: Emitted when Mongoose successfully makes its initial connection to the MongoDB server, or when Mongoose reconnects after losing connectivity. May be emitted multiple times if Mongoose loses connectivity.
270-
* `open`: Emitted after `'connected'` and `onOpen` is executed on all of this connection's models.
271-
* `disconnecting`: Your app called [`Connection#close()`](api/connection.html#connection_Connection-close) to disconnect from MongoDB
270+
* `open`: Emitted after `'connected'` and `onOpen` is executed on all of this connection's models. May be emitted multiple times if Mongoose loses connectivity.
271+
* `disconnecting`: Your app called [`Connection#close()`](api/connection.html#connection_Connection-close) to disconnect from MongoDB. This includes calling `mongoose.disconnect()`, which calls `close()` on all connections.
272272
* `disconnected`: Emitted when Mongoose lost connection to the MongoDB server. This event may be due to your code explicitly closing the connection, the database server crashing, or network connectivity issues.
273273
* `close`: Emitted after [`Connection#close()`](api/connection.html#connection_Connection-close) successfully closes the connection. If you call `conn.close()`, you'll get both a 'disconnected' event and a 'close' event.
274274
* `reconnected`: Emitted if Mongoose lost connectivity to MongoDB and successfully reconnected. Mongoose attempts to [automatically reconnect](https://thecodebarbarian.com/managing-connections-with-the-mongodb-node-driver.html) when it loses connection to the database.
275275
* `error`: Emitted if an error occurs on a connection, like a `parseError` due to malformed data or a payload larger than [16MB](https://www.mongodb.com/docs/manual/reference/limits/#BSON-Document-Size).
276-
* `fullsetup`: Emitted when you're connecting to a replica set and Mongoose has successfully connected to the primary and at least one secondary.
277-
* `all`: Emitted when you're connecting to a replica set and Mongoose has successfully connected to all servers specified in your connection string.
278276

279-
When you're connecting to a single MongoDB server (a "standalone"), Mongoose will emit 'disconnected' if it gets
280-
disconnected from the standalone server, and 'connected' if it successfully connects to the standalone. In a
281-
replica set, Mongoose will emit 'disconnected' if it loses connectivity to the replica set primary, and 'connected' if it manages to reconnect to the replica set primary.
277+
When you're connecting to a single MongoDB server (a ["standalone"](https://www.mongodb.com/docs/cloud-manager/tutorial/deploy-standalone/)), Mongoose will emit `disconnected` if it gets
278+
disconnected from the standalone server, and `connected` if it successfully connects to the standalone. In a
279+
[replica set](https://www.mongodb.com/docs/manual/replication/), Mongoose will emit `disconnected` if it loses connectivity to the replica set primary, and `connected` if it manages to reconnect to the replica set primary.
280+
281+
If you are using `mongoose.connect()`, you can use the following to listen to the above events:
282+
283+
```javascript
284+
mongoose.connection.on('connected', () => console.log('connected'));
285+
mongoose.connection.on('open', () => console.log('open'));
286+
mongoose.connection.on('disconnected', () => console.log('disconnected'));
287+
mongoose.connection.on('reconnected', () => console.log('reconnected'));
288+
mongoose.connection.on('disconnecting', () => console.log('disconnecting'));
289+
mongoose.connection.on('close', () => console.log('close'));
290+
291+
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
292+
```
293+
294+
With `mongoose.createConnection()`, use the following instead:
295+
296+
```javascript
297+
const conn = mongoose.createConnection('mongodb://127.0.0.1:27017/mongoose_test');
298+
299+
conn.on('connected', () => console.log('connected'));
300+
conn.on('open', () => console.log('open'));
301+
conn.on('disconnected', () => console.log('disconnected'));
302+
conn.on('reconnected', () => console.log('reconnected'));
303+
conn.on('disconnecting', () => console.log('disconnecting'));
304+
conn.on('close', () => console.log('close'));
305+
```
282306

283307
<h2 id="keepAlive"><a href="#keepAlive">A note about keepAlive</a></h2>
284308

0 commit comments

Comments
 (0)