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

connect Multiple Mongodb database with single interface #15045

Open
2 tasks done
KTBsomen opened this issue Nov 16, 2024 · 2 comments
Open
2 tasks done

connect Multiple Mongodb database with single interface #15045

KTBsomen opened this issue Nov 16, 2024 · 2 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@KTBsomen
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I just need more space so I created two different database and now I want to Marge all the database into single database so I can query it as a single database instance.

I just created a npm package called mongoplusplus to do the same. but I want this feature in native mongoose if possible.

also it dose the job well for my requirement but I am not 100% confident about the performance and security of my package and moreover the documentation kind of sucks so if anyone can help me evaluate the project that will be greatness at it's peak.

GitHub link of my package
https://github.com/KTBsomen/mongoplusplus

Motivation

lack of money and low free tier of mongodb. nothing fits into 512 MB

Example

it will just work like
i have connection uris of mongodb database
i will pass them to my constructor it will handle all the connections.

const dbname = 'testforUP';

const mongoURI1 = `mongodb+srv://xxxxx:[email protected]/${dbname}?retryWrites=true&w=majority`;
const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:[email protected]/${dbname}?retryWrites=true&w=majority`;
const mongodb = new mongoplusplus([mongoURI1, mongoURI2]);

now mejority of the function are the same like creating schemes, building models.
one key difference is I will have some extra query method to query all the database at once or just anyone of them.

const result = await likes.findInAllDatabase({ name: 'John' }, { limit: 10, skip: 0 });
Returns: An array of documents matching the filter from all databases.

like so on. you can check my current implementation on GitHub

https://github.com/KTBsomen/mongoplusplus

@KTBsomen KTBsomen added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Nov 16, 2024
@vkarpov15
Copy link
Collaborator

We've run into similar issues before, what we've done in the past is the following, which puts all the connection models on the global mongoose.models, so you can reference models from multiple connections by just doing require('mongoose')

  for (const connection of mongoose.connections) {
    for (const modelName of Object.keys(connection.models)) {
      if (mongoose.models[modelName]) {
        throw new Error(`Conflicting model ${modelName}`);
      }
    }
    Object.assign(mongoose.models, connection.models);
  }

It would be great to add something similar as a feature.

@vkarpov15 vkarpov15 added this to the 8.10 milestone Nov 20, 2024
@KTBsomen
Copy link
Author

you are awesome.
have you checked my implementation it's very easy just a single file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

2 participants