client.js
11.2 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
exports.__esModule = true;
var _cloneDeep2 = require('lodash/cloneDeep');
var _cloneDeep3 = _interopRequireDefault(_cloneDeep2);
var _uniqueId2 = require('lodash/uniqueId');
var _uniqueId3 = _interopRequireDefault(_uniqueId2);
var _assign2 = require('lodash/assign');
var _assign3 = _interopRequireDefault(_assign2);
var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
var _helpers = require('./helpers');
var helpers = _interopRequireWildcard(_helpers);
var _raw = require('./raw');
var _raw2 = _interopRequireDefault(_raw);
var _runner = require('./runner');
var _runner2 = _interopRequireDefault(_runner);
var _formatter = require('./formatter');
var _formatter2 = _interopRequireDefault(_formatter);
var _transaction = require('./transaction');
var _transaction2 = _interopRequireDefault(_transaction);
var _builder = require('./query/builder');
var _builder2 = _interopRequireDefault(_builder);
var _compiler = require('./query/compiler');
var _compiler2 = _interopRequireDefault(_compiler);
var _builder3 = require('./schema/builder');
var _builder4 = _interopRequireDefault(_builder3);
var _compiler3 = require('./schema/compiler');
var _compiler4 = _interopRequireDefault(_compiler3);
var _tablebuilder = require('./schema/tablebuilder');
var _tablebuilder2 = _interopRequireDefault(_tablebuilder);
var _tablecompiler = require('./schema/tablecompiler');
var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
var _columnbuilder = require('./schema/columnbuilder');
var _columnbuilder2 = _interopRequireDefault(_columnbuilder);
var _columncompiler = require('./schema/columncompiler');
var _columncompiler2 = _interopRequireDefault(_columncompiler);
var _genericPool = require('generic-pool');
var _inherits = require('inherits');
var _inherits2 = _interopRequireDefault(_inherits);
var _events = require('events');
var _string = require('./query/string');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var debug = require('debug')('knex:client');
var debugQuery = require('debug')('knex:query');
var debugBindings = require('debug')('knex:bindings');
var debugPool = require('debug')('knex:pool');
var id = 0;
function clientId() {
return 'client' + id++;
}
// The base client provides the general structure
// for a dialect specific client object.
function Client() {
var config = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
this.config = config;
//Client is a required field, so throw error if it's not supplied.
//If 'this.dialect' is set, then this is a 'super()' call, in which case
//'client' does not have to be set as it's already assigned on the client prototype.
if (!this.config.client && !this.dialect) {
throw new Error('knex: Required configuration option \'client\' is missing.');
}
this.connectionSettings = (0, _cloneDeep3.default)(config.connection || {});
if (this.driverName && config.connection) {
this.initializeDriver();
if (!config.pool || config.pool && config.pool.max !== 0) {
this.__cid = clientId();
this.initializePool(config);
}
}
this.valueForUndefined = this.raw('DEFAULT');
if (config.useNullAsDefault) {
this.valueForUndefined = null;
}
}
(0, _inherits2.default)(Client, _events.EventEmitter);
(0, _assign3.default)(Client.prototype, {
formatter: function formatter() {
return new _formatter2.default(this);
},
queryBuilder: function queryBuilder() {
return new _builder2.default(this);
},
queryCompiler: function queryCompiler(builder) {
return new _compiler2.default(this, builder);
},
schemaBuilder: function schemaBuilder() {
return new _builder4.default(this);
},
schemaCompiler: function schemaCompiler(builder) {
return new _compiler4.default(this, builder);
},
tableBuilder: function tableBuilder(type, tableName, fn) {
return new _tablebuilder2.default(this, type, tableName, fn);
},
tableCompiler: function tableCompiler(tableBuilder) {
return new _tablecompiler2.default(this, tableBuilder);
},
columnBuilder: function columnBuilder(tableBuilder, type, args) {
return new _columnbuilder2.default(this, tableBuilder, type, args);
},
columnCompiler: function columnCompiler(tableBuilder, columnBuilder) {
return new _columncompiler2.default(this, tableBuilder, columnBuilder);
},
runner: function runner(connection) {
return new _runner2.default(this, connection);
},
transaction: function transaction(container, config, outerTx) {
return new _transaction2.default(this, container, config, outerTx);
},
raw: function raw() {
var _ref;
return (_ref = new _raw2.default(this)).set.apply(_ref, arguments);
},
_formatQuery: function _formatQuery(sql, bindings, timeZone) {
var _this = this;
bindings = bindings == null ? [] : [].concat(bindings);
var index = 0;
return sql.replace(/\\?\?/g, function (match) {
if (match === '\\?') {
return '?';
}
if (index === bindings.length) {
return match;
}
var value = bindings[index++];
return _this._escapeBinding(value, { timeZone: timeZone });
});
},
_escapeBinding: (0, _string.makeEscape)({
escapeString: function escapeString(str) {
return '\'' + str.replace(/'/g, "''") + '\'';
}
}),
query: function query(connection, obj) {
var _this2 = this;
if (typeof obj === 'string') obj = { sql: obj };
obj.bindings = this.prepBindings(obj.bindings);
debugQuery(obj.sql);
this.emit('query', (0, _assign3.default)({ __knexUid: connection.__knexUid }, obj));
debugBindings(obj.bindings);
return this._query(connection, obj).catch(function (err) {
err.message = _this2._formatQuery(obj.sql, obj.bindings) + ' - ' + err.message;
_this2.emit('query-error', err, (0, _assign3.default)({ __knexUid: connection.__knexUid }, obj));
throw err;
});
},
stream: function stream(connection, obj, _stream, options) {
if (typeof obj === 'string') obj = { sql: obj };
this.emit('query', (0, _assign3.default)({ __knexUid: connection.__knexUid }, obj));
debugQuery(obj.sql);
obj.bindings = this.prepBindings(obj.bindings);
debugBindings(obj.bindings);
return this._stream(connection, obj, _stream, options);
},
prepBindings: function prepBindings(bindings) {
return bindings;
},
wrapIdentifier: function wrapIdentifier(value) {
return value !== '*' ? '"' + value.replace(/"/g, '""') + '"' : '*';
},
initializeDriver: function initializeDriver() {
try {
this.driver = this._driver();
} catch (e) {
helpers.exit('Knex: run\n$ npm install ' + this.driverName + ' --save\n' + e.stack);
}
},
poolDefaults: function poolDefaults(poolConfig) {
var _this3 = this;
var name = this.dialect + ':' + this.driverName + ':' + this.__cid;
return {
min: 2,
max: 10,
name: name,
log: function log(str, level) {
if (level === 'info') {
debugPool(level.toUpperCase() + ' pool ' + name + ' - ' + str);
}
},
create: function create(callback) {
_this3.acquireRawConnection().tap(function (connection) {
connection.__knexUid = (0, _uniqueId3.default)('__knexUid');
if (poolConfig.afterCreate) {
return _bluebird2.default.promisify(poolConfig.afterCreate)(connection);
}
}).asCallback(callback);
},
destroy: function destroy(connection) {
if (poolConfig.beforeDestroy) {
helpers.warn('\n beforeDestroy is deprecated, please open an issue if you use this\n to discuss alternative apis\n ');
poolConfig.beforeDestroy(connection, function () {});
}
if (connection !== void 0) {
_this3.destroyRawConnection(connection);
}
},
validate: function validate(connection) {
if (connection.__knex__disposed) {
helpers.warn('Connection Error: ' + connection.__knex__disposed);
return false;
}
return _this3.validateConnection(connection);
}
};
},
initializePool: function initializePool(config) {
if (this.pool) {
helpers.warn('The pool has already been initialized');
return;
}
this.pool = new _genericPool.Pool((0, _assign3.default)(this.poolDefaults(config.pool || {}), config.pool));
},
validateConnection: function validateConnection(connection) {
return true;
},
// Acquire a connection from the pool.
acquireConnection: function acquireConnection() {
var _this4 = this;
return new _bluebird2.default(function (resolver, rejecter) {
if (!_this4.pool) {
return rejecter(new Error('Unable to acquire a connection'));
}
var wasRejected = false;
var t = setTimeout(function () {
wasRejected = true;
rejecter(new _bluebird2.default.TimeoutError('Knex: Timeout acquiring a connection. The pool is probably full. ' + 'Are you missing a .transacting(trx) call?'));
}, _this4.config.acquireConnectionTimeout || 60000);
_this4.pool.acquire(function (err, connection) {
clearTimeout(t);
if (err) {
return rejecter(err);
}
if (wasRejected) {
_this4.pool.release(connection);
} else {
debug('acquired connection from pool: %s', connection.__knexUid);
resolver(connection);
}
});
});
},
// Releases a connection back to the connection pool,
// returning a promise resolved when the connection is released.
releaseConnection: function releaseConnection(connection) {
var _this5 = this;
return new _bluebird2.default(function (resolver) {
debug('releasing connection to pool: %s', connection.__knexUid);
_this5.pool.release(connection);
resolver();
});
},
// Destroy the current connection pool for the client.
destroy: function destroy(callback) {
var _this6 = this;
var promise = new _bluebird2.default(function (resolver) {
if (!_this6.pool) {
return resolver();
}
_this6.pool.drain(function () {
_this6.pool.destroyAllNow(function () {
_this6.pool = undefined;
resolver();
});
});
});
// Allow either a callback or promise interface for destruction.
if (typeof callback === 'function') {
promise.asCallback(callback);
} else {
return promise;
}
},
// Return the database being used by this client.
database: function database() {
return this.connectionSettings.database;
},
toString: function toString() {
return '[object KnexClient]';
},
canCancelQuery: false,
assertCanCancelQuery: function assertCanCancelQuery() {
if (!this.canCancelQuery) {
throw new Error("Query cancelling not supported for this dialect");
}
},
cancelQuery: function cancelQuery() {
throw new Error("Query cancelling not supported for this dialect");
}
});
exports.default = Client;
module.exports = exports['default'];