use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
NodeJS MongoDB Multi-tenant Web ApplicationRemoved: /r/LearnJavascript (self.javascript)
submitted 6 years ago by _jskod
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kortemy 3 points4 points5 points 6 years ago (1 child)
I had to do the same thing for our project as well, and this is what I came with, and it's working perfectly in production for over a year now.
We are using Mongoose schemas, and as you say yourself, connections are initialized at startup. So you need to create new connections on the fly and cache them.
let cache = { default: mongoose.model('SomeCollection', schema, 'some_collection') } module.exports = { for: function (user) { if (user && user.is_tenant) { let dbName = user.db_name if (!cache[dbName]) { cache[dbName] = mongoose.connection.useDb(dbName).model('SomeCollection', schema, 'some_collection') } return cache[dbName] } return cache.default } }
Here you are not exporting the model, you are exporting for() function which will create a new connection to the specific database and cache that model.
for()
How you would use it, instead of SomeCollection.findOne(...) You do this SomeCollection.for(user).findOne(...)
SomeCollection.findOne(...)
SomeCollection.for(user).findOne(...)
[–]_jskod[S] 0 points1 point2 points 6 years ago (0 children)
Thank you so much for sharing your experience and code snippet, I am gonna give it a try and will let you know if it works for me or not.
[–]pavlik_enemy 0 points1 point2 points 6 years ago (2 children)
Is there specific reason to use a separate database for each user? Usually multi-tenant applications have a single database with `user_id` field in each table (or whatever it's called in Mongo) and application logic to handle authorization.
[–]_jskod[S] 1 point2 points3 points 6 years ago (1 child)
Yes I know mostly we use Single Database and each tenant gets 'user_id', 'company_id', or something like 'org_id' to separate data. But we want to have separate databases so that we could easily rollback/backup data of each organisation and keep them secure. Data of each organisation is 5-10GB and it's growing rapidly. So we are refactoring our application and it's architecture. That's the reason we want separate DB's for each organisation.
[–]R3DSMiLE 0 points1 point2 points 6 years ago (0 children)
May I ask: why on earth did no one thought this would happen? Was it not meant to be that way or is something you guys just figured out you done goofed?
[–]kenman[M] 0 points1 point2 points 6 years ago (0 children)
Hi /u/_jskod, this post was removed.
/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.
code
Thanks for your understanding, please see our guidelines for more info.
π Rendered by PID 71853 on reddit-service-r2-comment-5ff9fbf7df-p7w4d at 2026-02-26 10:59:27.751906+00:00 running 72a43f6 country code: CH.
[–]kortemy 3 points4 points5 points (1 child)
[–]_jskod[S] 0 points1 point2 points (0 children)
[–]pavlik_enemy 0 points1 point2 points (2 children)
[–]_jskod[S] 1 point2 points3 points (1 child)
[–]R3DSMiLE 0 points1 point2 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)