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

Is it possible to remove default empty connection from mongoose.connections? #8302

Closed
NenadJovicic opened this issue Nov 5, 2019 · 9 comments
Labels
discussion If you have any thoughts or comments on this issue, please share them! help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Milestone

Comments

@NenadJovicic
Copy link

Do you want to request a feature or report a bug?
feature
What is the current behavior?
When mongoose is imported in some file, it will automatically call constructor of mongoose, where we have call of createConnection() method, without any uri.

That means that mongoose.connections[0] is a bit useless. So, when I use .createConnection(someUri, options) to have real connection to db and my connection will be on 2nd place in an array of connections. Problem here is that mongoose.model() won't be able to properly instance model, because it will watch default connection, which is equal to mongoose.connections[0]

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');

(() => {
  console.log('Number of connections is', mongoose.connections.length); // will print that there is one connection
})();

What is the expected behavior?

Expecting that this default connection disappear from list of connections when real connection is provided and to override mongoose.connection with first connection from list

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node 12.13.0
mongoose 5.7.7
mongoDB 3.6.2

@vkarpov15
Copy link
Collaborator

It should be safe, but the better question is, why do you need to do this? If you want to use mongoose.model(), we assume you're using mongoose.connect(). If you want to use a connection other than the default connection, you can just use Connection#model(). So if you only want to use 1 connection, why do you need createConnection()?

@vkarpov15 vkarpov15 added discussion If you have any thoughts or comments on this issue, please share them! help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Nov 8, 2019
@NenadJovicic
Copy link
Author

@vkarpov15 well, i am using nestjs framework, which got its own module to connect to db, where I only provide params for connection
And now, I want to apply some plugin globally to mongoose, which use default mongoose.connection
Nestjs use in the background createConnection method, which means proper connection is the 2nd one in mongoose.connections, and mongoose.connection that plugin use is not proper one
So, I had to do a lot of hacking to make it work.

This is the main reason why I asked this question

Other reason was that, if I instance model differently from suggested way by framework, and I do mongoose.model(), it won't initialize model for proper db

nestjs/mongoose#109
nestjs/mongoose#27
some of the issues opened to this framework just because they changed from mongoose.connect to mongoose.createConnection and some plugins are not working

@vkarpov15
Copy link
Collaborator

So then why not use mongoose.connections[0].model(), or, better yet, const conn = mongoose.connections[0]; conn.model();?

@syned13
Copy link

syned13 commented Jan 5, 2022

So then why not use mongoose.connections[0].model(), or, better yet, const conn = mongoose.connections[0]; conn.model();?

Doing so does not work because the first connection is an empty connection. The following error is thrown:

Schema hasn't been registered for model \"{MODEL_NAME}\".\nUse mongoose.model(name, schema)

It might be the case where the schema is not present(let's say, something dynamic where that schema could be anything).

I'm building a plugin where that's the case.

@vkarpov15

@vkarpov15
Copy link
Collaborator

@syned13 I don't understand the issue. What do you mean by "the first connection is an empty connection"?

@vkarpov15 vkarpov15 reopened this May 13, 2024
@vkarpov15 vkarpov15 added this to the 8.5 milestone May 13, 2024
@vkarpov15
Copy link
Collaborator

Should be possible now with createInitialConnection option, just need a way to set that

Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label May 28, 2024
Copy link

github-actions bot commented Jun 2, 2024

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2024
@vkarpov15 vkarpov15 reopened this Jun 16, 2024
@github-actions github-actions bot removed the Stale label Jun 17, 2024
vkarpov15 added a commit that referenced this issue Jun 19, 2024
vkarpov15 added a commit that referenced this issue Jun 24, 2024
vkarpov15 added a commit that referenced this issue Jun 24, 2024
vkarpov15 added a commit that referenced this issue Jun 24, 2024
Remove default connection if setting createInitialConnection to false after Mongoose instance created
@vkarpov15
Copy link
Collaborator

With #14679, mongoose.set('createInitialConnection', false) will also remove the existing default connection, as long as that default connection hasn't been used yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants