We have a NodeJS + Express Rest API application that manages requests for multiple clients. We have separated our customers data in own databases. Based on the incoming request, we can easily decide which database we have to use.
Now let’s consider how we can aggregate database connections to avoid high memory consumption:
The simplest but, in our opinion, least performant solution: We create a new connection to the customer database per REST call in order to access customer-specific data.
We create a Mongoose instance and switch databases using .useDb() as follows:
const db = mongoose.connection.useDb("company") const collection = db.model('...
Is it ensured that the data does not mix if there are two incoming connections from different customers at the same time and the database is changed in between?
The same database is used but different collection prefixes are used. However, there is no easy way to determine data consumption per customer (compared to database size).
Another consideration was to introduce a “customer key” and store all data in the same collection. However, a level of security feels lost there. Same loss of consumption reporting feature.
Are there any better approaches? Any ideas about this?
I’m looking forward to a swarm of knowledgeable reactions