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