express - InversifyJS: dependency instantiation per HTTP Request -
i'm using inversify.js in project express. create connection neo4j database, , process has 2 objets:
- the driver object - shared across application , created 1 time only
- the session object - each http request should create session against driver, lifecyle same http request lifecycle (as long request ends, connection destroyed)
without insersify.js, problem solved using simple algorithm:
exports.getsession = function (context) { // 'context' http request if(context.neo4jsession) { return context.neo4jsession; } else { context.neo4jsession = driver.session(); return context.neo4jsession; } }; (example: https://github.com/neo4j-examples/neo4j-movies-template/blob/master/api/neo4j/dbutils.js#l13-l21)
to create static dependency driver, can inject constant:
container.bind<dbdriver>("dbdriver").toconstantvalue(new neo4jdbdriver());
how can create dependency instantiated once per request , retrieve them container?
i suspect must invoke container on middleware this:
this._express.use((request, response, next) => { // container , create instance of neo4jsession request lifecycle next(); }); thanks in advance.
Comments
Post a Comment