Skip to content

Multiple connections with models located in separate files #8275

@weblogics

Description

@weblogics

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions