Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    connect-reqcontextpublic

    connect-reqcontext

    Middleware for Connect which provides globally accessible request context with lifecycle hooks.

    This middleware can only be used inside an active domain, use connect-domain.

    var connectDomain = require('connect-domain');
    var connectReqContext = require('connect-reqcontext');
    
    ...
    app.use(connectDomain());
    app.use(connectReqContext.requestContext(function() {
      // return context you want to attach to the request
      return {
        db: new pg.Client(...)
      }
    }, function (context) {
      // this callback will be called when response is finished
      // if custom callback on error is provided (see below) then this callback
      // will only be called on successful circumstances
      context.db.query('commit');
      context.db.end();
    });
    ...
    // middleware below is optional if you want custom cleanup on error
    app.use(connectReqContext.requestContextOnError(function(context) {
      context.db.query('rollback');
      context.db.end();
    });
    ...
    
    // in another file, maybe in models.js or something like this
    // you can access db connection without having access to a request object
    var connectReqContext = require('connect-reqcontext');
    
    function getUserById(id, cb) {
      connectReqContext.get('db').query("select ...", cb);
    }
    

    Keywords

    none

    install

    npm i connect-reqcontext

    Downloadsweekly downloads

    3

    version

    0.1.0

    license

    none

    repository

    githubgithub

    last publish

    collaborators

    • avatar