UNPKG

mysql-async-wrapper

Version:

This is a Wrapper class, which helps to get rid of callbacks of mysql package functions and provides a way to use them in async await (es7) syntax, Below Examples uses express framework in both (import/export syntax and commonJs syntax)

185 lines 7.19 kB
"use strict"; /** * This is a Wrapper class, which helps to get rid of callbacks of mysql package functions * and provides a way to use them in async await (es7) syntax * * Just Import BaseDatabase class and create an instance of it by passing pool as 1st parameter * configuration in 2nd parameter * * It supports retry query execution also, by passing maxRetryCount and retryErrorCodes in * configuration while creating db instance * * To BeginTransaction Just pass transaction true in options while calling getConnection, * or if required beginTransaction function can be called separately * * Incase of Error in Executing Query and connection is in transaction then it will automatically * get rollback * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); class BaseDatabase { constructor(pool, dbConfig = {}) { this._pool = null; this._connection = null; this._inTransaction = false; this._maxRetryCount = 0; this._retryErrorCodes = []; this._pool = pool; this._maxRetryCount = dbConfig.maxRetryCount || 0; this._retryErrorCodes = dbConfig.retryErrorCodes || []; } /** * @description To Create Connection * @param configuration Config Object, right now supports only transaction key * if transaction is passed as true then connection with transaction will be created */ getConnection(configuration = {}) { return new Promise((resolve, reject) => { this._pool.getConnection((err, connection) => __awaiter(this, void 0, void 0, function* () { if (err) { reject(err); return; } this._connection = connection; if (configuration.transaction) { try { const beginTransaction = yield this.beginTransaction(); resolve(this); } catch (err) { reject(err); } } else { resolve(this); } })); }); } beginTransaction() { return new Promise((resolve, reject) => { this._connection.beginTransaction((err) => { if (err) { reject(err); return; } this._inTransaction = true; resolve(); }); }); } /** * @description To Execute DB Query, In case of Error and Connecton in transaction auto rollback will be called * @param query {required} Query String * @param queryParams {optional} Query Array * @param retryErrorCodes {optional} Array of Error Codes in which you want to retry for this query */ executeQuery(query, queryParams, retryErrorCodes) { return new Promise((resolve, reject) => { if (this._connection) { this._executeQuery(query, queryParams, this._retryErrorCodes.concat(retryErrorCodes), 0).then(result => { resolve(result); }).catch(err => { // In case of Error in Query and connection is in transaction rollback if (this._inTransaction) { this._inTransaction = false; this.rollback(() => { reject(err); }); return; } reject(err); }); } else { reject(new Error("Connection Doesn't Exist")); } }); } // To Commit Transaction commit() { return new Promise((resolve, reject) => { if (this._connection && this._inTransaction) { // In case of error in commit then rollback transaction this._connection.commit((err) => { if (err) { this.rollback(); reject(err); return; } // after committing close transaction this._inTransaction = false; resolve(); }); } else { reject(new Error("Connection or Transaction Doesn't Exist")); } }); } // To Roll back transaction rollback(cb) { if (this._connection && this._inTransaction) { this._inTransaction = false; this._connection.rollback((err) => { if (err) { if (cb) { cb(err); } return; } if (cb) { cb(null); } }); } else { if (cb) { cb(new Error("Connection Doesn't Exist")); } } } // To Release Connection Back To Pool close() { if (this._connection) { this._connection.release(); this._connection = null; } } // Executes Query and Retry retryCount times if error code is present in retryErrorCodes _executeQuery(query, queryParams, retryErrorCodes, retryCount) { return new Promise((resolve, reject) => { const queryString = this._connection.query(query, queryParams, (err, result) => __awaiter(this, void 0, void 0, function* () { // Incase of Error and Connection in transaction rollback transaction and make inTransaction to Avoid auto commit on connection close if (err) { const retryCheck = retryErrorCodes.findIndex((item) => item === err.code) > -1; if (retryCheck && retryCount <= this._maxRetryCount) { try { const retryResult = yield this._executeQuery(query, queryParams, retryErrorCodes, retryCount + 1); resolve(retryResult); } catch (err) { reject(err); } } else { reject(err); } } else { resolve(result); } })); }); } } exports.default = BaseDatabase; //# sourceMappingURL=index.js.map