Coming from a non Node background, my first instinct is to define my service as such
MyService.js
module.exports = new function(dbConnection)
{
// service uses the db
}
Now, I want one open db connection per request, so I define in middleware:
res.locals.db = openDbConnection();
And in some consuming Express api code:
api.js
var MyService = require(./services/MyService')
...
router.get('/foo/:id?', function (req, res) {
var service = new MyService(res.locals.db);
});
Now, being that Node's preferred method of dependency injection is via the require(...)
statement, it seems that I shouldn't be using the constructor of MyService
for injection of the db
.
So let's say I want to have
var db = require('db');
at the top of MyService
and then use somehow like db.current
....but how would I tie the db to the current res.locals
object now that db
is a module itself? What's a recommended way of handling this kind of thin in Node?
Updated Answer: 05/02/15
If you want to attach a DB connection to each request object, then use that connection in your service, the connection will have to be passed to
myService
some how. The example below shows one way of doing that. If we try to usedb.current
or something to that effect, we'll be storing state in our DB module. In my experience, that will lead to trouble.Alternatively, I lay out the approach I've used (and still use) in this previous answer. What this means for this example is the following:
With this approach, we've decoupled the DB module from the api/app/router modules and only the module that actually uses it will know it exists.
Previous Answer: 05/01/15
What you're talking about could be done using an express middleware. Here's what it might look like:
I personally have never seen something like
new MyService
before in express applications. That doesn't mean it can't be done, but you might consider an approach like this