Skip to content

Commit

Permalink
docs(connection): add note about exporting schemas, not models, in mu…
Browse files Browse the repository at this point in the history
…lti connection paradigm

Fix #8275
  • Loading branch information
vkarpov15 committed Nov 2, 2019
1 parent 5b6eeab commit 374246d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/connections.pug
Expand Up @@ -323,6 +323,35 @@ block content
create and retrieve [models](./api.html#model_Model). Models are
**always** scoped to a single connection.

```javascript
const UserModel = conn.model('User', userSchema);
```

If you use multiple connections, you should make sure you export schemas,
**not** models.

```javascript
const userSchema = new Schema({ name: String, email: String });

// The correct pattern is to export a schema
module.exports = userSchema;

// Because if you export a model as shown below, the model will be scoped
// to Mongoose's default connection.
// module.exports = mongoose.model('User', userSchema);
```

In addition, you should define a factory function that registers models on a
connection to make it easy to register all your models on a given connection.

```javascript
const userSchema = require('./userSchema');

module.exports = conn => {
conn.model('User', userSchema);
};
```

Mongoose creates a _default connection_ when you call `mongoose.connect()`.
You can access the default connection using `mongoose.connection`.

Expand Down

0 comments on commit 374246d

Please sign in to comment.