pool.js 2.9 KB

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