promise.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. const SqlString = require('sqlstring');
  3. const EventEmitter = require('events').EventEmitter;
  4. const parserCache = require('./lib/parsers/parser_cache.js');
  5. const PoolCluster = require('./lib/pool_cluster.js');
  6. const createConnection = require('./lib/create_connection.js');
  7. const createPool = require('./lib/create_pool.js');
  8. const createPoolCluster = require('./lib/create_pool_cluster.js');
  9. const PromiseConnection = require('./lib/promise/connection.js');
  10. const PromisePool = require('./lib/promise/pool.js');
  11. const makeDoneCb = require('./lib/promise/make_done_cb.js');
  12. const PromisePoolConnection = require('./lib/promise/pool_connection.js');
  13. const inheritEvents = require('./lib/promise/inherit_events.js');
  14. function createConnectionPromise(opts) {
  15. const coreConnection = createConnection(opts);
  16. const createConnectionErr = new Error();
  17. const thePromise = opts.Promise || Promise;
  18. if (!thePromise) {
  19. throw new Error(
  20. 'no Promise implementation available.' +
  21. 'Use promise-enabled node version or pass userland Promise' +
  22. " implementation as parameter, for example: { Promise: require('bluebird') }",
  23. );
  24. }
  25. return new thePromise((resolve, reject) => {
  26. coreConnection.once('connect', () => {
  27. resolve(new PromiseConnection(coreConnection, thePromise));
  28. });
  29. coreConnection.once('error', (err) => {
  30. createConnectionErr.message = err.message;
  31. createConnectionErr.code = err.code;
  32. createConnectionErr.errno = err.errno;
  33. createConnectionErr.sqlState = err.sqlState;
  34. reject(createConnectionErr);
  35. });
  36. });
  37. }
  38. // note: the callback of "changeUser" is not called on success
  39. // hence there is no possibility to call "resolve"
  40. function createPromisePool(opts) {
  41. const corePool = createPool(opts);
  42. const thePromise = opts.Promise || Promise;
  43. if (!thePromise) {
  44. throw new Error(
  45. 'no Promise implementation available.' +
  46. 'Use promise-enabled node version or pass userland Promise' +
  47. " implementation as parameter, for example: { Promise: require('bluebird') }",
  48. );
  49. }
  50. return new PromisePool(corePool, thePromise);
  51. }
  52. class PromisePoolCluster extends EventEmitter {
  53. constructor(poolCluster, thePromise) {
  54. super();
  55. this.poolCluster = poolCluster;
  56. this.Promise = thePromise || Promise;
  57. inheritEvents(poolCluster, this, ['warn', 'remove']);
  58. }
  59. getConnection(pattern, selector) {
  60. const corePoolCluster = this.poolCluster;
  61. return new this.Promise((resolve, reject) => {
  62. corePoolCluster.getConnection(
  63. pattern,
  64. selector,
  65. (err, coreConnection) => {
  66. if (err) {
  67. reject(err);
  68. } else {
  69. resolve(new PromisePoolConnection(coreConnection, this.Promise));
  70. }
  71. },
  72. );
  73. });
  74. }
  75. query(sql, args) {
  76. const corePoolCluster = this.poolCluster;
  77. const localErr = new Error();
  78. if (typeof args === 'function') {
  79. throw new Error(
  80. 'Callback function is not available with promise clients.',
  81. );
  82. }
  83. return new this.Promise((resolve, reject) => {
  84. const done = makeDoneCb(resolve, reject, localErr);
  85. corePoolCluster.query(sql, args, done);
  86. });
  87. }
  88. execute(sql, args) {
  89. const corePoolCluster = this.poolCluster;
  90. const localErr = new Error();
  91. if (typeof args === 'function') {
  92. throw new Error(
  93. 'Callback function is not available with promise clients.',
  94. );
  95. }
  96. return new this.Promise((resolve, reject) => {
  97. const done = makeDoneCb(resolve, reject, localErr);
  98. corePoolCluster.execute(sql, args, done);
  99. });
  100. }
  101. of(pattern, selector) {
  102. return new PromisePoolCluster(
  103. this.poolCluster.of(pattern, selector),
  104. this.Promise,
  105. );
  106. }
  107. end() {
  108. const corePoolCluster = this.poolCluster;
  109. const localErr = new Error();
  110. return new this.Promise((resolve, reject) => {
  111. corePoolCluster.end((err) => {
  112. if (err) {
  113. localErr.message = err.message;
  114. localErr.code = err.code;
  115. localErr.errno = err.errno;
  116. localErr.sqlState = err.sqlState;
  117. localErr.sqlMessage = err.sqlMessage;
  118. reject(localErr);
  119. } else {
  120. resolve();
  121. }
  122. });
  123. });
  124. }
  125. }
  126. /**
  127. * proxy poolCluster synchronous functions
  128. */
  129. (function (functionsToWrap) {
  130. for (let i = 0; functionsToWrap && i < functionsToWrap.length; i++) {
  131. const func = functionsToWrap[i];
  132. if (
  133. typeof PoolCluster.prototype[func] === 'function' &&
  134. PromisePoolCluster.prototype[func] === undefined
  135. ) {
  136. PromisePoolCluster.prototype[func] = (function factory(funcName) {
  137. return function () {
  138. return PoolCluster.prototype[funcName].apply(
  139. this.poolCluster,
  140. arguments,
  141. );
  142. };
  143. })(func);
  144. }
  145. }
  146. })(['add']);
  147. function createPromisePoolCluster(opts) {
  148. const corePoolCluster = createPoolCluster(opts);
  149. const thePromise = (opts && opts.Promise) || Promise;
  150. if (!thePromise) {
  151. throw new Error(
  152. 'no Promise implementation available.' +
  153. 'Use promise-enabled node version or pass userland Promise' +
  154. " implementation as parameter, for example: { Promise: require('bluebird') }",
  155. );
  156. }
  157. return new PromisePoolCluster(corePoolCluster, thePromise);
  158. }
  159. exports.createConnection = createConnectionPromise;
  160. exports.createPool = createPromisePool;
  161. exports.createPoolCluster = createPromisePoolCluster;
  162. exports.escape = SqlString.escape;
  163. exports.escapeId = SqlString.escapeId;
  164. exports.format = SqlString.format;
  165. exports.raw = SqlString.raw;
  166. exports.PromisePool = PromisePool;
  167. exports.PromiseConnection = PromiseConnection;
  168. exports.PromisePoolConnection = PromisePoolConnection;
  169. exports.__defineGetter__('Types', () => require('./lib/constants/types.js'));
  170. exports.__defineGetter__('Charsets', () =>
  171. require('./lib/constants/charsets.js'),
  172. );
  173. exports.__defineGetter__('CharsetToEncoding', () =>
  174. require('./lib/constants/charset_encodings.js'),
  175. );
  176. exports.setMaxParserCache = function (max) {
  177. parserCache.setMaxCache(max);
  178. };
  179. exports.clearParserCache = function () {
  180. parserCache.clearCache();
  181. };