UNPKG

pg-tx

Version:

Transaction wrapper for node-postgres

41 lines 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const client_1 = require("./client"); const proxy_client_1 = require("./proxy_client"); const transaction_1 = require("./transaction"); /** * @param pg node postgres pool * @param callback callback that will use provided transaction client * @param forceRollback force rollback even without errors - useful for tests * @returns */ async function tx(pg, callback, forceRollback) { const { client, clientMode } = await client_1.getClient(pg); const transactionMode = await transaction_1.beginTransaction({ client, clientMode }); try { let result; const proxyClient = new proxy_client_1.ProxyClient(client); try { result = await callback(proxyClient); } catch (e) { await transaction_1.cancelTransaction({ client, transactionMode }); throw e; } finally { proxyClient.proxyRelease(); } if (forceRollback) { await transaction_1.cancelTransaction({ client, transactionMode }); } else { await transaction_1.commitTransaction({ client, transactionMode }); } return result; } finally { client_1.releaseClient({ client, clientMode }); } } exports.default = tx; //# sourceMappingURL=index.js.map