promise.js 6.2 KB

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