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