Middleware factory for using a single transaction per request. Transactions are only started if a downstream handler requests one. If a transaction is started by the handler, it is automatically committed or rolled back once the handler completes.
Enable the middleware:
app.use(makeDatabaseTransactionMiddleware(knex));
In handler:
async function handleRequest(req, res) {
const transaction = req.getTransaction();
// Potentially failing logic here
}