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

Multiple connections with models located in separate files #8275

Closed
weblogics opened this issue Oct 23, 2019 · 4 comments
Closed

Multiple connections with models located in separate files #8275

weblogics opened this issue Oct 23, 2019 · 4 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@weblogics
Copy link

weblogics commented Oct 23, 2019

Do you want to request a feature or report a bug?

feature

What is the current behavior?

When initialising more than one connection (different hosts / servers) its impossible to separate models from the initial app. I have tried to implement useDb(), but this seems to be for the same connection with different DB's hosted on this.

I have also tried to implement mongoose.connections[index].model('ModelName', Schema), this doesn't work if the connection hasn't been run yet (Please note: I'm using mongoose.createConnection()). Using TypeScript imports need to be the first thing in the file and therefore there's no way to create these connections prior to loading the models.

There really needs to be a better way to separate models from connections. Is there nothing that can be done to use a connection such as a useConnection function? The current implementation is unreliable and it's hard to work with.

Has anyone else managed to solve this issue?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

MongoDB: 4.0.3
Node: v11.12.0
Mongoose: 5.7.5

@vkarpov15
Copy link
Collaborator

useConnection is an interesting idea, but we aren't likely to support it because it encourages changing the connection when using the model.

The right design pattern here is to export a schema, not a model. Then have a separate file that exports a function which registers models on a connection.

import fooSchema from './fooSchema';

export default function(conn) {
  conn.model('Foo', fooSchema);
}

@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Oct 29, 2019
@vkarpov15 vkarpov15 added this to the 5.7.8 milestone Oct 29, 2019
@lauti7
Copy link

lauti7 commented Mar 17, 2020

@vkarpov15 I am having same issue and in the docs, i saw that i have to create exported function but where i am supposed to run this function?

@vkarpov15
Copy link
Collaborator

@lauti7 there's a couple possibilities. First is to have separate connection files:

// conn1.js
const config = require('./config');
const mongoose = require('mongoose');

const db = mongoose.createConnection(config.mongodbUri);
require('./decorateModels')(db);

module.exports = db;

// conn2.js
const config = require('./config');
const mongoose = require('mongoose');

const db = mongoose.createConnection(config.mongodbUri);
require('./decorateModels')(db);

module.exports = db;

Where decorateModels() is a function that attaches models to a connection.

Another alternative is to use a DI framework or some other IOC pattern. These days I just use ramda's applySpec() for IOC.

@vkarpov15 vkarpov15 reopened this Mar 18, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.7.8, 5.9.6 Mar 18, 2020
@vkarpov15
Copy link
Collaborator

Will close this in favor of #8679

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants