2015-04-22 15:39:29 -04:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var assign = require('lodash/object/assign');
|
2015-04-22 10:34:14 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
var Migrator = require('../migrate');
|
|
|
|
var Seeder = require('../seed');
|
|
|
|
var FunctionHelper = require('../functionhelper');
|
|
|
|
var QueryInterface = require('../query/methods');
|
|
|
|
var helpers = require('../helpers');
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
module.exports = function makeKnex(client) {
|
|
|
|
|
|
|
|
// The object we're potentially using to kick off an initial chain.
|
|
|
|
function knex(tableName) {
|
2015-05-09 13:58:18 -04:00
|
|
|
var qb = knex.queryBuilder();
|
2015-04-22 10:34:14 -04:00
|
|
|
if (!tableName) {
|
2015-05-09 13:58:18 -04:00
|
|
|
helpers.warn('calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.');
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
2015-05-09 13:58:18 -04:00
|
|
|
return tableName ? qb.table(tableName) : qb;
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assign(knex, {
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2015-04-29 15:14:41 -04:00
|
|
|
Promise: require('../promise'),
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
// A new query builder instance
|
2015-05-09 13:58:18 -04:00
|
|
|
queryBuilder: function queryBuilder() {
|
|
|
|
return client.queryBuilder();
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
raw: function raw() {
|
|
|
|
return client.raw.apply(client, arguments);
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Runs a new transaction, taking a container and returning a promise
|
|
|
|
// for when the transaction is resolved.
|
2015-05-09 13:58:18 -04:00
|
|
|
transaction: function transaction(container, config) {
|
|
|
|
return client.transaction(container, config);
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Typically never needed, initializes the pool for a knex client.
|
2015-05-09 13:58:18 -04:00
|
|
|
initialize: function initialize(config) {
|
|
|
|
return client.initialize(config);
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Convenience method for tearing down the pool.
|
2015-05-09 13:58:18 -04:00
|
|
|
destroy: function destroy(callback) {
|
|
|
|
return client.destroy(callback);
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
// The `__knex__` is used if you need to duck-type check whether this
|
|
|
|
// is a knex builder, without a full on `instanceof` check.
|
2015-05-20 11:21:59 -04:00
|
|
|
knex.VERSION = knex.__knex__ = '0.8.6';
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
// Hook up the "knex" object as an EventEmitter.
|
2015-05-09 13:58:18 -04:00
|
|
|
var ee = new EventEmitter();
|
2015-04-22 10:34:14 -04:00
|
|
|
for (var key in ee) {
|
2015-05-09 13:58:18 -04:00
|
|
|
knex[key] = ee[key];
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow chaining methods from the root object, before
|
|
|
|
// any other information is specified.
|
2015-05-09 13:58:18 -04:00
|
|
|
QueryInterface.forEach(function (method) {
|
|
|
|
knex[method] = function () {
|
|
|
|
var builder = knex.queryBuilder();
|
|
|
|
return builder[method].apply(builder, arguments);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
knex.client = client;
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
Object.defineProperties(knex, {
|
|
|
|
|
|
|
|
schema: {
|
2015-05-09 13:58:18 -04:00
|
|
|
get: function get() {
|
|
|
|
return client.schemaBuilder();
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
migrate: {
|
2015-05-09 13:58:18 -04:00
|
|
|
get: function get() {
|
|
|
|
return new Migrator(knex);
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
seed: {
|
2015-05-09 13:58:18 -04:00
|
|
|
get: function get() {
|
|
|
|
return new Seeder(knex);
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fn: {
|
2015-05-09 13:58:18 -04:00
|
|
|
get: function get() {
|
|
|
|
return new FunctionHelper(client);
|
2015-04-22 10:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
2015-04-22 10:34:14 -04:00
|
|
|
|
|
|
|
// Passthrough all "start" and "query" events to the knex object.
|
2015-05-09 13:58:18 -04:00
|
|
|
client.on('start', function (obj) {
|
|
|
|
knex.emit('start', obj);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('query', function (obj) {
|
|
|
|
knex.emit('query', obj);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.makeKnex = function (client) {
|
|
|
|
return makeKnex(client);
|
|
|
|
};
|
2015-04-22 10:34:14 -04:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
return knex;
|
|
|
|
};
|