2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-04-17 15:00:08 -04:00
|
|
|
// Knex.js 0.8.0
|
|
|
|
// --------------
|
2014-04-08 16:25:57 -04:00
|
|
|
// (c) 2014 Tim Griesser
|
2013-04-30 18:29:53 -04:00
|
|
|
// Knex may be freely distributed under the MIT license.
|
2013-03-12 11:52:08 -04:00
|
|
|
// For details and documentation:
|
|
|
|
// http://knexjs.org
|
2013-12-10 13:09:01 -05:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var Raw = require('./lib/raw');
|
|
|
|
var warn = require('./lib/helpers').warn
|
2015-04-17 15:00:08 -04:00
|
|
|
var QueryInterface = require('./lib/query/methods');
|
2015-04-19 16:31:52 -04:00
|
|
|
var Client = require('./lib/client')
|
|
|
|
var assign = require('lodash/object/assign')
|
2014-04-15 11:43:47 -04:00
|
|
|
|
2015-04-17 15:00:08 -04:00
|
|
|
function Knex(config) {
|
2015-04-19 16:31:52 -04:00
|
|
|
if (typeof config === 'string') {
|
|
|
|
return new Knex(assign(parseUrl(config), arguments[2]))
|
|
|
|
}
|
|
|
|
if (arguments.length === 0 || !config.client) {
|
|
|
|
return makeKnex(new Client({}))
|
|
|
|
}
|
|
|
|
var clientName = config.client || config.dialect;
|
|
|
|
var Dialect = require('./lib/dialects/' + (aliases[clientName] || clientName))
|
|
|
|
|
|
|
|
return makeKnex(new Dialect(config))
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeKnex(client) {
|
2013-12-10 13:09:01 -05:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// The object we're potentially using to kick off an initial chain.
|
2013-12-27 14:44:21 -05:00
|
|
|
function knex(tableName) {
|
2015-04-19 16:31:52 -04:00
|
|
|
var qb = knex.queryBuilder();
|
|
|
|
if (!tableName) {
|
|
|
|
warn('calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.')
|
|
|
|
}
|
2014-02-22 17:06:33 -05:00
|
|
|
return tableName ? qb.table(tableName) : qb;
|
2013-12-10 13:09:01 -05:00
|
|
|
}
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
assign(knex, {
|
|
|
|
|
|
|
|
// A new query builder instance
|
|
|
|
queryBuilder: function() {
|
|
|
|
return client.queryBuilder()
|
|
|
|
},
|
2014-04-27 19:35:36 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
raw: function() {
|
|
|
|
return client.raw.apply(client, arguments);
|
|
|
|
},
|
2015-04-17 15:00:08 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Runs a new transaction, taking a container and returning a promise
|
|
|
|
// for when the transaction is resolved.
|
|
|
|
transaction: function(container) {
|
|
|
|
return client.transaction(container);
|
|
|
|
},
|
2014-05-09 14:22:41 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Typically never needed, initializes the pool for a knex client.
|
|
|
|
initialize: function(config) {
|
|
|
|
return client.initialize(config)
|
|
|
|
},
|
2014-09-01 17:18:45 +02:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Convenience method for tearing down the pool.
|
|
|
|
destroy: function(callback) {
|
|
|
|
return client.destroy(callback);
|
2014-05-08 17:58:07 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -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.
|
|
|
|
knex.VERSION = knex.__knex__ = '0.8.0';
|
|
|
|
|
|
|
|
// Hook up the "knex" object as an EventEmitter.
|
|
|
|
var ee = new EventEmitter();
|
|
|
|
for (var key in ee) {
|
|
|
|
knex[key] = ee[key];
|
2013-12-27 14:44:21 -05:00
|
|
|
}
|
2014-05-08 17:58:07 -04:00
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
// Allow chaining methods from the root object, before
|
|
|
|
// any other information is specified.
|
2014-07-14 13:47:19 -04:00
|
|
|
QueryInterface.forEach(function(method) {
|
2013-12-27 14:44:21 -05:00
|
|
|
knex[method] = function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
var builder = knex.queryBuilder();
|
2013-12-27 14:44:21 -05:00
|
|
|
return builder[method].apply(builder, arguments);
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
knex.client = client;
|
2013-05-18 00:07:28 -04:00
|
|
|
|
2014-07-14 13:47:19 -04:00
|
|
|
Object.defineProperties(knex, {
|
|
|
|
|
|
|
|
schema: {
|
|
|
|
get: function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
return client.schemaBuilder();
|
2014-07-14 13:47:19 -04:00
|
|
|
}
|
|
|
|
},
|
2014-09-01 17:18:45 +02:00
|
|
|
|
2014-07-14 13:47:19 -04:00
|
|
|
migrate: {
|
|
|
|
get: function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
return new Migrator(knex);
|
2014-07-14 13:47:19 -04:00
|
|
|
}
|
2014-07-03 11:59:29 +02:00
|
|
|
},
|
2014-09-01 17:18:45 +02:00
|
|
|
|
2014-07-21 09:42:56 -04:00
|
|
|
seed: {
|
|
|
|
get: function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
return new Seeder(knex);
|
2014-07-21 09:42:56 -04:00
|
|
|
}
|
|
|
|
},
|
2014-07-03 11:59:29 +02:00
|
|
|
|
|
|
|
fn: {
|
|
|
|
get: function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
return new FunctionHelper(client);
|
2014-07-03 11:59:29 +02:00
|
|
|
}
|
2014-07-14 13:47:19 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
|
2013-12-10 13:09:01 -05:00
|
|
|
});
|
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Passthrough all "start" and "query" events to the knex object.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-12-10 13:09:01 -05:00
|
|
|
return knex;
|
2015-04-17 15:00:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a "raw" query, though we can't do anything with it other than put
|
|
|
|
// it in a query statement.
|
|
|
|
Knex.raw = function(sql, bindings) {
|
2015-04-19 16:31:52 -04:00
|
|
|
return new Raw({}).set(sql, bindings);
|
|
|
|
}
|
2015-04-17 15:00:08 -04:00
|
|
|
|
|
|
|
// Create a new "knex" instance with the appropriate configured client.
|
|
|
|
Knex.initialize = function(config) {
|
|
|
|
warn('knex.initialize is deprecated, pass your config object directly to the knex module')
|
|
|
|
return new Knex(config)
|
2015-04-19 16:31:52 -04:00
|
|
|
}
|
2015-04-17 15:00:08 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
function parseUrl() {
|
|
|
|
|
|
|
|
}
|
2015-04-17 15:00:08 -04:00
|
|
|
|
|
|
|
// The client names we'll allow in the `{name: lib}` pairing.
|
2015-04-19 16:31:52 -04:00
|
|
|
var aliases = {
|
|
|
|
'mariadb' : 'maria',
|
|
|
|
'mariasql' : 'maria',
|
|
|
|
'pg' : 'postgres',
|
|
|
|
'sqlite' : 'sqlite3',
|
2013-12-10 13:09:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Knex;
|