loopbackio / loopbackio/loopback-connector-mongodb

Loopback 3.x multiple database connections

Open
#658 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
186
Forks
238
Avg merge
2h 49m
Merged PRs (30d)
1

Description

Hi everyone,

I am current managing a project in which there is a single instance of Loopback application but each Model must have a different datasource based on the current client that is using an application.

E.g.
**CLIENT 1**
client: jack.it
dbname: jack

**CLIENT 2**
client: paul.it
dbname: paul

I have a single model defined with a default datasource that is commons for each client, except for the dbName that changes dinamically.

So, for example, I could have a model named model1 that the first time has to execute an operation on db jack and the same model the second time has to execute an operation on db paul.

Now. I have implemented a dsChanger that, based on the clientId it creates a new db if it is not already present in the dbCache of mongodb and return the instance of the new db object, something like that:

```
var dsChanger = function(self, clienteId){
if(app.dbsMap){
let newDb = app.dbsMap[clienteId];
if(!newDb){
newDb = app.dbsMap.default;
}
return self.db.db(newDb, {noListener: false});
}
};
```

I want to modify the collection function to take as second param the db instance, but I don't really like this solution.

What is the best practice to do this (change dinamically the db) without break the connector logic?

Thank you in advance.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names dsChanger, app.dbsMap/dbCache, and the collection function; start by tracing how the MongoDB connector selects a datasource and how model operations reach that function. Compare the requested per-client database behavior with the connector’s existing interfaces, and define what a supported approach would need to preserve without breaking connector logic.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.