1

I'm using jugglingdb as ORM for my nodejs project.

there is a central database with all users, and also every user have it's own database.

when I'm using models I just require them where I need.

The problem is that models should be connected for every user to different database and I don't want o include models in every route in order to inject request to it.

Can I somehow set a middleware or something?

Keloo
  • 1,368
  • 1
  • 17
  • 36
  • I do the following for `mongoose` and MongoDB, but the approach should work for you as well: instead of standard `exports.Person = mongoose.model('Person', PersonSchema)` I do `exports.getModel = function(conn) { return conn.model('Person', PersonSchema); }`. Here `conn` is a custom connection that I can aquire with `mongoose.createConnection` and can supply to get a DB-specific model where I define routes and have access to session that can for example store user-specific connection information. – Oleg Sklyar Apr 25 '14 at 12:30
  • yes you're right but when creating the database connection you need to know user data which you can get only from request I mean `function (request, response) {}` the route. And the idea is not to include models in every route. – Keloo Apr 25 '14 at 12:48
  • 2
    Assuming you have some connection pool, where given userId you can fetch your connection from. Then what is wrong with: `function(req, res) { var conn = getConnFromPool(res.session.userId); var Model=modelmodule.getModel(conn); Model.find(...`? Maintaining such a connection pool is a separate issue ;) – Oleg Sklyar Apr 25 '14 at 12:53

0 Answers0