0.2.2 • Published 5 years ago

express-knex-trx v0.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Build Status Coveralls github branch GitHub last commit

express-knex-trx

Wrap express middlewares with in a knex transaction!

Usage

// my-request-handler.js

import { withKnexTrx } from 'express-knex-trx';
import knexInstance from './path/to/your/knex-config';

const createDriver = async (req, res, next) => {
  try {
    // Access the transaction object with `res.locals.trx`.
    const driverId = await knex('drivers')
      .transacting(res.locals.trx)
      .insert({ name: 'Joe' });

    const carId = await knex('cars')
      .transacting(res.locals.trx)
      .insert({
        driver_id: driverId,
        year: 2020,
      });
  } catch (e) {
    next(e); // Handled by express-knex-trx. Transaction will be rolled back and error passed on to the next handler.
  }
  
  // Any unhandled errors here will also cause the transaction to be rolled back.

  next(); // Since the commit handler comes after, make sure you do not send the response in this handler.
};

export default [withKnexTrx(knexInstance, handler), sendResponseHandler];