@churchapps/apihelper
Version:
Library of helper functions not specific to any one ChurchApps project or framework.
55 lines • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DB = void 0;
const Pool_1 = require("./Pool");
const LoggingHelper_1 = require("./LoggingHelper");
class DB {
// wraps in promise
static async getConnection() {
const promise = new Promise((resolve, reject) => {
Pool_1.Pool.current.getConnection((ex, conn) => { if (ex)
reject(ex);
else
resolve(conn); });
});
;
const connection = await promise;
return connection;
}
// wraps in promise
static async getQuery(connection, sql, params) {
const promise = new Promise((resolve, reject) => {
connection.query(sql, params, async (ex, rows) => {
if (ex) {
LoggingHelper_1.LoggingHelper.getCurrent().error(ex);
reject(ex);
}
else {
resolve(rows);
}
});
});
const query = await promise;
return query;
}
static async query(sql, params) {
let result = null;
const connection = await this.getConnection();
try {
result = await this.getQuery(connection, sql, params);
}
catch (ex) {
LoggingHelper_1.LoggingHelper.getCurrent().error(ex);
}
finally {
connection.release();
}
return result;
}
static async queryOne(sql, params) {
const result = await this.query(sql, params);
return result?.length > 0 ? result[0] : null;
}
}
exports.DB = DB;
//# sourceMappingURL=DB.js.map