express-knex-transaction

1.1.0 • Public • Published

express-knex-transaction

Express middleware that adds a knex transaction object as req.trx so you can easily wrap middleware and routes in a single transaction.

Usage

Called with a valid knex object, this module returns a middleware function you can .use() before any middleware that requires database access. It then adds a trx property to the request object that you can use for knex.transacting() or as a knex client.

Commit and rollback are handled based on the status code of the final response.

Example

var expressKnexTransaction = require('express-knex-transaction');
var express = require('express');
var knex = require('knex')({});
 
var app = new express();
 
app.use(expressKnexTransaction(knex));
 
app.get('/', function(req, res) {
  knex('table1')
    .transacting(req.trx)
    .then(function(data) {
      res.status(200).json(data);
    })
    .catch(function() {
      res.sendStatus(400);
    });
});

Readme

Keywords

none

Package Sidebar

Install

npm i express-knex-transaction

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

7.65 kB

Total Files

5

Last publish

Collaborators

  • siraiedail