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)

57 lines (56 loc) 2.16 kB
/** * 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 * */ interface IConnectionConfig { transaction?: boolean; } /** * @description Database Configuration * @param maxRetryCount Number of Retries in case of errors * @param retryErrorCodes Pass Array of Error Codes like ER_LOCK_DEADLOCK, ERR_LOCK_WAIT_TIMEOUT */ interface IDBConfig { maxRetryCount?: number; retryErrorCodes?: string[]; } declare class BaseDatabase { private _pool; private _connection; private _inTransaction; private _maxRetryCount; private _retryErrorCodes; constructor(pool: any, dbConfig?: IDBConfig); /** * @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?: IConnectionConfig): Promise<{}>; beginTransaction(): Promise<{}>; /** * @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: string, queryParams?: any, retryErrorCodes?: string[]): Promise<{}>; commit(): Promise<{}>; rollback(cb?: any): void; close(): void; private _executeQuery; } export default BaseDatabase;