I am working on an B2B application that can be customized and branded by our customers. The idea is that the users login via a generic account (stored in a "global" database), and all application specific data is stored in seperate per-customer database. The account defines which database(s) a user has access to.I am using ExpressJS and Mongoose.I have defined all my models in a single module which returns a fresh object upon each require. The constructor has 1 argument, which is the per-customer database name. This object has getters for each of my models. For instance, if some specific models are requested (like account) i switch to the "global" database e.g. mongoose.connection.useDb("my_global_database") and return the model. In all other cases, i switch to the per-customer database and return the requested model.Pseudo code: var models = require("models")("database-customer-1");// useDb will be invoked with "my_global_database" and return the user model. models.user.find(); // useDb will be invoked with "database-customer-1" and return the ranking model. models.ranking.find(); However, this is causing memory leaks it seems:possible EventEmitter memory leak detected. 11 close listeners added. Use emitter.setMaxListeners() to increase limit. Increasing the limits resolves this issue, but i feel like i am doing something wrong with this approach.Am i better off creating multiple mongoose connections, determining which one to use upon a request instead of switching databases on the same connection per-request? Or is there some other solution i am not seeing here?Thanks for taking the time to read!
Submitted March 02, 2016 at 03:40PM by Punishirt
No comments:
Post a Comment