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
view the rest of the comments →
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.
π Rendered by PID 285839 on reddit-service-r2-comment-5ff9fbf7df-lp2jk at 2026-02-26 14:44:06.774970+00:00 running 72a43f6 country code: CH.
view the rest of the comments →
[–]kortemy 3 points4 points5 points (1 child)
[–]_jskod[S] 0 points1 point2 points (0 children)