Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(connections): improve description of connection creation patterns #14564

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions docs/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,30 +457,10 @@ module.exports = userSchema;
// module.exports = mongoose.model('User', userSchema);
```

If you use the export schema pattern, you still need to create models
somewhere. There are two common patterns. First is to export a connection
and register the models on the connection in the file:

```javascript
// connections/fast.js
const mongoose = require('mongoose');

const conn = mongoose.createConnection(process.env.MONGODB_URI);
conn.model('User', require('../schemas/user'));

module.exports = conn;

// connections/slow.js
const mongoose = require('mongoose');

const conn = mongoose.createConnection(process.env.MONGODB_URI);
conn.model('User', require('../schemas/user'));
conn.model('PageView', require('../schemas/pageView'));

module.exports = conn;
```

Another alternative is to register connections with a dependency injector
If you use the export schema pattern, you still need to create models somewhere.
There are two common patterns.
The first is to create a function that instantiates a new connection and registers all models on that connection.
With this pattern, you may also register connections with a dependency injector
or another [inversion of control (IOC) pattern](https://thecodebarbarian.com/using-ramda-as-a-dependency-injector).

```javascript
Expand All @@ -496,6 +476,23 @@ module.exports = function connectionFactory() {
};
```

Exporting a function that creates a new connection is the most flexible pattern.
However, that pattern can make it tricky to get access to your connection from your route handlers or wherever your business logic is.
An alternative pattern is to export a connection and register the models on the connection in the file's top-level scope as follows.

```javascript
// connections/index.js
const mongoose = require('mongoose');

const conn = mongoose.createConnection(process.env.MONGODB_URI);
conn.model('User', require('../schemas/user'));

module.exports = conn;
```

You can create separate files for each connection, like `connections/web.js` and `connections/mobile.js` if you want to create separate connections for your web API backend and your mobile API backend.
Your business logic can then `require()` or `import` the connection it needs.

<h2 id="connection_pools"><a href="#connection_pools">Connection Pools</a></h2>

Each `connection`, whether created with `mongoose.connect` or
Expand Down