2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import { EventEmitter } from 'events';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import Migrator from '../migrate';
|
|
|
|
import Seeder from '../seed';
|
|
|
|
import FunctionHelper from '../functionhelper';
|
|
|
|
import QueryInterface from '../query/methods';
|
|
|
|
import * as helpers from '../helpers';
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign } from 'lodash'
|
2016-05-09 19:01:00 +02: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.
|
|
|
|
function knex(tableName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const qb = knex.queryBuilder()
|
|
|
|
if (!tableName) helpers.warn(
|
|
|
|
'calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.'
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
return tableName ? qb.table(tableName) : qb
|
|
|
|
}
|
|
|
|
|
|
|
|
assign(knex, {
|
2015-07-02 18:28:34 -04:00
|
|
|
|
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() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return client.queryBuilder()
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
raw() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return client.raw.apply(client, arguments)
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
batchInsert(table, batch, chunkSize = 1000) {
|
2016-05-09 19:01:00 +02:00
|
|
|
return new 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) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return client.transaction(container, config)
|
|
|
|
},
|
|
|
|
|
|
|
|
// Typically never needed, initializes the pool for a knex client.
|
2016-05-17 01:01:34 +10:00
|
|
|
initialize(config) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return client.initialize(config)
|
|
|
|
},
|
|
|
|
|
|
|
|
// Convenience method for tearing down the pool.
|
2016-05-17 01:01:34 +10:00
|
|
|
destroy(callback) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return client.destroy(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// The `__knex__` is used if you need to duck-type check whether this
|
|
|
|
// is a knex builder, without a full on `instanceof` check.
|
2016-05-25 21:05:41 +03:00
|
|
|
knex.VERSION = knex.__knex__ = require('../../package.json').version;
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
// Hook up the "knex" object as an EventEmitter.
|
2016-05-17 01:01:34 +10:00
|
|
|
const ee = new EventEmitter()
|
|
|
|
for (const key in ee) {
|
2015-05-09 13:58:18 -04:00
|
|
|
knex[key] = ee[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow chaining methods from the root object, before
|
|
|
|
// any other information is specified.
|
|
|
|
QueryInterface.forEach(function(method) {
|
|
|
|
knex[method] = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const builder = knex.queryBuilder()
|
2015-05-09 13:58:18 -04:00
|
|
|
return builder[method].apply(builder, arguments)
|
|
|
|
}
|
|
|
|
})
|
2015-07-02 18:28:34 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
knex.client = client
|
|
|
|
|
|
|
|
Object.defineProperties(knex, {
|
|
|
|
|
|
|
|
schema: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return client.schemaBuilder()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
migrate: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return new Migrator(knex)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
seed: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return new Seeder(knex)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fn: {
|
2016-05-17 01:01:34 +10:00
|
|
|
get() {
|
2015-05-09 13:58:18 -04:00
|
|
|
return new FunctionHelper(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
// Passthrough all "start" and "query" events to the knex object.
|
|
|
|
client.on('start', function(obj) {
|
|
|
|
knex.emit('start', obj)
|
|
|
|
})
|
2015-07-02 18:28:34 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
client.on('query', function(obj) {
|
|
|
|
knex.emit('query', obj)
|
|
|
|
})
|
|
|
|
|
2016-01-28 19:57:26 +01:00
|
|
|
client.on('query-error', function(err, obj) {
|
|
|
|
knex.emit('query-error', err, obj)
|
|
|
|
})
|
|
|
|
|
2016-02-26 19:51:35 +01:00
|
|
|
client.on('query-response', function(response, obj, builder) {
|
|
|
|
knex.emit('query-response', response, obj, builder)
|
|
|
|
})
|
|
|
|
|
2016-05-18 20:09:51 +10:00
|
|
|
client.makeKnex = makeKnex
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
return knex
|
2015-11-02 14:54:38 +11:00
|
|
|
}
|