pool.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. const EventEmitter = require('events').EventEmitter;
  3. const {
  4. captureStackHolder,
  5. applyCapturedStack,
  6. } = require('./capture_local_err.js');
  7. const makeDoneCb = require('./make_done_cb.js');
  8. const PromisePoolConnection = require('./pool_connection.js');
  9. const inheritEvents = require('./inherit_events.js');
  10. const BasePool = require('../base/pool.js');
  11. class PromisePool extends EventEmitter {
  12. constructor(pool, thePromise) {
  13. super();
  14. this.pool = pool;
  15. this.Promise = thePromise || Promise;
  16. inheritEvents(pool, this, ['acquire', 'connection', 'enqueue', 'release']);
  17. }
  18. getConnection() {
  19. const corePool = this.pool;
  20. return new this.Promise((resolve, reject) => {
  21. corePool.getConnection((err, coreConnection) => {
  22. if (err) {
  23. reject(err);
  24. } else {
  25. resolve(new PromisePoolConnection(coreConnection, this.Promise));
  26. }
  27. });
  28. });
  29. }
  30. releaseConnection(connection) {
  31. if (connection instanceof PromisePoolConnection) connection.release();
  32. }
  33. query(sql, args) {
  34. const corePool = this.pool;
  35. const stackHolder = captureStackHolder(PromisePool.prototype.query);
  36. if (typeof args === 'function') {
  37. throw new Error(
  38. 'Callback function is not available with promise clients.'
  39. );
  40. }
  41. return new this.Promise((resolve, reject) => {
  42. const done = makeDoneCb(resolve, reject, stackHolder);
  43. if (args !== undefined) {
  44. corePool.query(sql, args, done);
  45. } else {
  46. corePool.query(sql, done);
  47. }
  48. });
  49. }
  50. execute(sql, args) {
  51. const corePool = this.pool;
  52. const stackHolder = captureStackHolder(PromisePool.prototype.execute);
  53. if (typeof args === 'function') {
  54. throw new Error(
  55. 'Callback function is not available with promise clients.'
  56. );
  57. }
  58. return new this.Promise((resolve, reject) => {
  59. const done = makeDoneCb(resolve, reject, stackHolder);
  60. if (args) {
  61. corePool.execute(sql, args, done);
  62. } else {
  63. corePool.execute(sql, done);
  64. }
  65. });
  66. }
  67. end() {
  68. const corePool = this.pool;
  69. const stackHolder = captureStackHolder(PromisePool.prototype.end);
  70. return new this.Promise((resolve, reject) => {
  71. corePool.end((err) => {
  72. if (err) {
  73. applyCapturedStack(err, stackHolder);
  74. reject(err);
  75. } else {
  76. resolve();
  77. }
  78. });
  79. });
  80. }
  81. async [Symbol.asyncDispose]() {
  82. if (!this.pool._closed) {
  83. await this.end();
  84. }
  85. }
  86. }
  87. (function (functionsToWrap) {
  88. for (let i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
  89. const func = functionsToWrap[i];
  90. if (
  91. typeof BasePool.prototype[func] === 'function' &&
  92. PromisePool.prototype[func] === undefined
  93. ) {
  94. PromisePool.prototype[func] = (function factory(funcName) {
  95. return function () {
  96. return BasePool.prototype[funcName].apply(this.pool, arguments);
  97. };
  98. })(func);
  99. }
  100. }
  101. })([
  102. // synchronous functions
  103. 'escape',
  104. 'escapeId',
  105. 'format',
  106. ]);
  107. module.exports = PromisePool;