pool_cluster.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. const PromisePoolConnection = require('./pool_connection');
  3. const makeDoneCb = require('./make_done_cb');
  4. class PromisePoolNamespace {
  5. constructor(poolNamespace, thePromise) {
  6. this.poolNamespace = poolNamespace;
  7. this.Promise = thePromise || Promise;
  8. }
  9. getConnection() {
  10. const corePoolNamespace = this.poolNamespace;
  11. return new this.Promise((resolve, reject) => {
  12. corePoolNamespace.getConnection((err, coreConnection) => {
  13. if (err) {
  14. reject(err);
  15. } else {
  16. resolve(new PromisePoolConnection(coreConnection, this.Promise));
  17. }
  18. });
  19. });
  20. }
  21. query(sql, values) {
  22. const corePoolNamespace = this.poolNamespace;
  23. const localErr = new Error();
  24. if (typeof values === 'function') {
  25. throw new Error(
  26. 'Callback function is not available with promise clients.'
  27. );
  28. }
  29. return new this.Promise((resolve, reject) => {
  30. const done = makeDoneCb(resolve, reject, localErr);
  31. corePoolNamespace.query(sql, values, done);
  32. });
  33. }
  34. execute(sql, values) {
  35. const corePoolNamespace = this.poolNamespace;
  36. const localErr = new Error();
  37. if (typeof values === 'function') {
  38. throw new Error(
  39. 'Callback function is not available with promise clients.'
  40. );
  41. }
  42. return new this.Promise((resolve, reject) => {
  43. const done = makeDoneCb(resolve, reject, localErr);
  44. corePoolNamespace.execute(sql, values, done);
  45. });
  46. }
  47. }
  48. module.exports = PromisePoolNamespace;