Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.98k stars 3.85k forks source link

connect Multiple Mongodb database with single interface #15045

Open KTBsomen opened 5 days ago

KTBsomen commented 5 days ago

Prerequisites

🚀 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:xxxxxx@cluster0.xxxxx.mongodb.net/${dbname}?retryWrites=true&w=majority`;
const mongoURI2 = `readonly:mongodb+srv://xxxxxxx:xxxxxx@cluster0.xxxxxx.mongodb.net/${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

vkarpov15 commented 2 days ago

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.

KTBsomen commented 2 days ago

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