ts-firebird
Version:
Promisify node-firebird
65 lines • 2.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const node_firebird_1 = require("node-firebird");
const util_1 = require("util");
class FirebirdTransaction {
constructor(db, isolation = node_firebird_1.ISOLATION_READ_COMMITTED) {
this.db = db;
this.isolation = isolation;
}
async init() {
const asyncTransaction = (0, util_1.promisify)(this.db.transaction);
this.transaction = await asyncTransaction.call(this.db, this.isolation);
}
checkTransaction() {
if (!this.transaction)
throw new Error('Transaction is null');
}
async query(query, params, autoCommit = false) {
this.checkTransaction();
const asyncQuery = (0, util_1.promisify)(this.transaction.query);
try {
const response = await asyncQuery.call(this.transaction, query, params);
if (autoCommit)
await this.commit(true);
return response;
}
catch (e) {
if (autoCommit)
await this.rollback(true);
throw e;
}
}
async execute(query, params, autoCommit = false) {
this.checkTransaction();
const asyncExecute = (0, util_1.promisify)(this.transaction.execute);
try {
const response = await asyncExecute.call(this.transaction, query, params);
if (autoCommit)
await this.commit(true);
return response;
}
catch (e) {
if (autoCommit)
await this.rollback(true);
throw e;
}
}
detach() {
this.db.detach();
}
async commit(detach = false) {
const asyncCommit = (0, util_1.promisify)(this.transaction.commit);
await asyncCommit.call(this.transaction);
if (detach)
this.detach();
}
async rollback(detach = false) {
const asyncRollback = (0, util_1.promisify)(this.transaction.rollback);
await asyncRollback.call(this.transaction);
if (detach)
this.detach();
}
}
exports.default = FirebirdTransaction;
//# sourceMappingURL=firebird.transaction.js.map