@coko/server
Version:
Reusable server for use by Coko's projects
34 lines • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useTransaction;
const objection_1 = require("objection");
const db_1 = require("../db");
objection_1.Model.knex(db_1.db);
async function useTransaction(callback, options = {}) {
// console.log('im in')
const { passedTrxOnly = false, trx } = options;
if (!callback) {
throw new Error('Use transaction: Invalid arguments!');
}
/**
* Most common case (eg. useTransaction(callback))
* No pre-defined transaction was provided.
* Use transaction anyway.
*/
if (!trx && !passedTrxOnly) {
return objection_1.Model.transaction(async (newtrx) => callback(newtrx));
}
/**
* I want to use a transaction only if one is provided through the options,
* None was. Just run function without a transaction.
*/
if (!trx && passedTrxOnly) {
return callback();
}
/**
* Transaction was passed from a parent.
* Use passed transaction on current cb.
*/
return callback(trx);
}
//# sourceMappingURL=useTransaction.js.map