2016-05-17 01:01:34 +10:00
|
|
|
import { EventEmitter } from 'events';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2018-08-24 11:39:20 +02:00
|
|
|
import Migrator from '../migrate/Migrator';
|
2016-05-17 01:01:34 +10:00
|
|
|
import Seeder from '../seed';
|
|
|
|
import FunctionHelper from '../functionhelper';
|
|
|
|
import QueryInterface from '../query/methods';
|
2018-07-09 08:10:34 -04:00
|
|
|
import { assign } from 'lodash';
|
2017-03-22 21:44:36 +01:00
|
|
|
import batchInsert from './batchInsert';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default function makeKnex(client) {
|
2015-05-09 13:58:18 -04:00
|
|
|
// The object we're potentially using to kick off an initial chain.
|
2017-01-26 16:22:09 +00:00
|
|
|
function knex(tableName, options) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const qb = knex.queryBuilder();
|
|
|
|
if (!tableName)
|
|
|
|
client.logger.warn(
|
|
|
|
'calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.'
|
|
|
|
);
|
|
|
|
return tableName ? qb.table(tableName, options) : qb;
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assign(knex, {
|
2016-08-09 17:23:07 -04:00
|
|
|
Promise: require('bluebird'),
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-03-09 06:54:28 +01:00
|
|
|
// A new query builder instance.
|
2016-05-17 01:01:34 +10:00
|
|
|
queryBuilder() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client.queryBuilder();
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
raw() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client.raw.apply(client, arguments);
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
batchInsert(table, batch, chunkSize = 1000) {
|
2017-03-22 21:44:36 +01:00
|
|
|
return batchInsert(this, table, batch, chunkSize);
|
2016-02-02 21:19:36 +01:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// Runs a new transaction, taking a container and returning a promise
|
|
|
|
// for when the transaction is resolved.
|
2016-05-17 01:01:34 +10:00
|
|
|
transaction(container, config) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client.transaction(container, config);
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Typically never needed, initializes the pool for a knex client.
|
2016-05-17 01:01:34 +10:00
|
|
|
initialize(config) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client.initializePool(config);
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Convenience method for tearing down the pool.
|
2016-05-17 01:01:34 +10:00
|
|
|
destroy(callback) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client.destroy(callback);
|
2018-05-02 12:31:16 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ref(ref) {
|
|
|
|
return client.ref(ref);
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
});
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Hook up the "knex" object as an EventEmitter.
|
2018-07-09 08:10:34 -04:00
|
|
|
const ee = new EventEmitter();
|
2016-05-17 01:01:34 +10:00
|
|
|
for (const key in ee) {
|
2018-07-09 08:10:34 -04:00
|
|
|
knex[key] = ee[key];
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow chaining methods from the root object, before
|
|
|
|
// any other information is specified.
|
|
|
|
QueryInterface.forEach(function(method) {
|
|
|
|
knex[method] = function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
const builder = knex.queryBuilder();
|
|
|
|
return builder[method].apply(builder, arguments);
|
|
|
|
};
|
|
|
|
});
|
2015-07-02 18:28:34 -04:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
knex.client = client;
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
const VERSION = '0.12.6';
|
2016-09-12 18:11:56 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
Object.defineProperties(knex, {
|
2016-09-12 18:11:56 -04:00
|
|
|
__knex__: {
|
|
|
|
get() {
|
2018-05-29 17:42:03 +02:00
|
|
|
knex.client.logger.warn(
|
2016-09-12 18:11:56 -04:00
|
|
|
'knex.__knex__ is deprecated, you can get the module version' +
|
2018-07-09 08:10:34 -04:00
|
|
|
"by running require('knex/package').version"
|
|
|
|
);
|
|
|
|
return VERSION;
|
|
|
|
},
|
2016-09-12 18:11:56 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
VERSION: {
|
|
|
|
get() {
|
2018-05-29 17:42:03 +02:00
|
|
|
knex.client.logger.warn(
|
2016-09-12 18:11:56 -04:00
|
|
|
'knex.VERSION is deprecated, you can get the module version' +
|
2018-07-09 08:10:34 -04:00
|
|
|
"by running require('knex/package').version"
|
|
|
|
);
|
|
|
|
return VERSION;
|
|
|
|
},
|
2016-09-12 18:26:49 -04:00
|
|
|
},
|
2016-09-12 18:11:56 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
schema: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return client.schemaBuilder();
|
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
migrate: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Migrator(knex);
|
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
seed: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Seeder(knex);
|
|
|
|
},
|
2015-05-09 13:58:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
fn: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new FunctionHelper(client);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Passthrough all "start" and "query" events to the knex object.
|
|
|
|
client.on('start', function(obj) {
|
2018-07-09 08:10:34 -04:00
|
|
|
knex.emit('start', obj);
|
|
|
|
});
|
2015-05-09 13:58:18 -04:00
|
|
|
client.on('query', function(obj) {
|
2018-07-09 08:10:34 -04:00
|
|
|
knex.emit('query', obj);
|
|
|
|
});
|
2016-01-28 19:57:26 +01:00
|
|
|
client.on('query-error', function(err, obj) {
|
2018-07-09 08:10:34 -04:00
|
|
|
knex.emit('query-error', err, obj);
|
|
|
|
});
|
2016-02-26 19:51:35 +01:00
|
|
|
client.on('query-response', function(response, obj, builder) {
|
2018-07-09 08:10:34 -04:00
|
|
|
knex.emit('query-response', response, obj, builder);
|
|
|
|
});
|
2016-02-26 19:51:35 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
client.makeKnex = makeKnex;
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex;
|
2015-11-02 14:54:38 +11:00
|
|
|
}
|