mariadb-connector.class.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { QueryOptions, PoolConnection, Pool } from 'mysql';
  2. import { Logger } from '../../../common/util/logger.class';
  3. const MAX_CONNECTION_RETRIES = 60;
  4. export class MariaDBConnector {
  5. private _conn?: PoolConnection;
  6. constructor(private pool: Pool) {}
  7. public query(options: string | QueryOptions, values: any): Promise<Array<object | object[]>> {
  8. return new Promise(async (resolve, reject) => {
  9. try {
  10. if (!this._conn) return reject('Database not opened.');
  11. this._conn.query(options, values, (err, results) => (err ? reject(err) : resolve(results)));
  12. } catch (e) {
  13. reject(e);
  14. }
  15. });
  16. }
  17. public beginTransaction() {
  18. return new Promise<void>((resolve, reject) => {
  19. try {
  20. if (!this._conn) return reject('Database not opened.');
  21. this._conn.beginTransaction(err => (err ? reject(err) : resolve()));
  22. } catch (e) {
  23. reject(e);
  24. }
  25. });
  26. }
  27. public commit() {
  28. return new Promise<void>((resolve, reject) => {
  29. try {
  30. if (!this._conn) return reject('Database not opened.');
  31. this._conn.commit(err => (err ? reject(err) : resolve()));
  32. } catch (e) {
  33. reject(e);
  34. }
  35. });
  36. }
  37. public async rollback() {
  38. return new Promise<void>((resolve, reject) => {
  39. try {
  40. if (!this._conn) return reject('Database not opened.');
  41. this._conn.rollback(err => (err ? reject(err) : resolve()));
  42. } catch (e) {
  43. reject(e);
  44. }
  45. });
  46. }
  47. public async connect(): Promise<void> {
  48. let retries = 1;
  49. let lasterror: any | null = null;
  50. while (retries <= MAX_CONNECTION_RETRIES) {
  51. try {
  52. Logger.debug(`[DEBUG] Connecting mariadb connection pool (${retries}/${MAX_CONNECTION_RETRIES}) ...`);
  53. await this._connect();
  54. lasterror = null;
  55. break;
  56. } catch (e) {
  57. lasterror = e;
  58. await new Promise<void>(res => setTimeout(res, 1000));
  59. } finally {
  60. retries++;
  61. }
  62. }
  63. if (lasterror) throw lasterror;
  64. }
  65. private async _connect(): Promise<void> {
  66. return new Promise((resolve, reject) => {
  67. try {
  68. if (!this._conn || !['connected', 'authenticated'].includes(this._conn?.state)) {
  69. return this.pool.getConnection((err, conn) => {
  70. if (err) return reject(err);
  71. this._conn = conn;
  72. resolve();
  73. });
  74. }
  75. resolve();
  76. } catch (e) {
  77. reject(e);
  78. }
  79. });
  80. }
  81. public close(): Promise<void> {
  82. return new Promise(async (resolve, reject) => {
  83. try {
  84. if (this._conn && ['connected', 'authenticated'].includes(this._conn?.state)) {
  85. Logger.debug(`[DEBUG] Closing mariadb connection.`);
  86. this._conn.release();
  87. }
  88. resolve();
  89. } catch (e) {
  90. reject(e);
  91. }
  92. });
  93. }
  94. }