knex/knex.js

132 lines
4.6 KiB
JavaScript
Raw Normal View History

2013-12-27 14:44:21 -05:00
// Knex.js 0.6.0
2013-09-13 13:50:41 -04:00
// --------------
2013-09-04 20:47:27 -04:00
2013-03-12 11:52:08 -04:00
// (c) 2013 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
2013-12-27 14:44:21 -05:00
// The "Knex" object we're exporting is just a passthrough to `Knex.initialize`.
function Knex() {
return Knex.initialize.apply(null, arguments);
}
Knex.raw = function Knex$raw(sql, bindings) {
return new Raw(sql, bindings);
};
2013-12-10 13:09:01 -05:00
// Base library dependencies of the app.
var _ = require('lodash');
// Require the main constructors necessary for a `Knex` instance,
// each of which are injected with the current instance, so they maintain
// the correct client reference & grammar.
2013-12-27 14:44:21 -05:00
var Raw = require('./lib/raw');
var Helpers = require('./lib/helpers');
2013-12-10 13:09:01 -05:00
2013-12-27 14:44:21 -05:00
// Lazy-loaded modules.
var Transaction, Migrate;
2013-12-10 13:09:01 -05:00
2013-12-27 14:44:21 -05:00
// The client names we'll allow in the `{name: lib}` pairing.
var Clients = Knex.Clients = {
'mysql' : './lib/clients/mysql',
'pg' : './lib/clients/postgres',
'postgres' : './lib/clients/postgres',
'postgresql' : './lib/clients/postgres',
'sqlite' : './lib/clients/sqlite3',
'sqlite3' : './lib/clients/sqlite3',
'websql' : './lib/clients/websql'
};
2013-12-10 13:09:01 -05:00
2013-12-27 14:44:21 -05:00
// Create a new "knex" instance with the appropriate configured client.
Knex.initialize = function(config) {
2013-12-10 13:09:01 -05:00
var Dialect, client;
2013-12-27 14:44:21 -05:00
// The object we're potentially using to kick off an
// initial chain. It is assumed that `knex` isn't a
// constructor, so we have no reference to 'this' just
// in case it's called with `new`.
function knex(tableName) {
var qb = new client.Query;
return tableName ? qb.table(tableName) : qb;
2013-12-10 13:09:01 -05:00
}
2013-03-12 11:52:08 -04:00
2013-12-27 14:44:21 -05: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.6.0';
knex.raw = function knex$raw(sql, bindings) {
var raw = new Raw(sql, bindings);
raw.__client = client;
return raw;
2013-12-10 13:09:01 -05:00
};
2013-04-30 18:29:53 -04:00
// Return a version of knex which doesn't mutate the chain each time
// you call a new method. Useful for creating partial query chains.
knex.stateless = _.once(function() {
client.initStateless();
_.each(_.keys(client.StatelessQuery.prototype), function(method) {
knex[method] = function() {
var builder = (this instanceof client.StatelessQuery) ? this : new client.StatelessQuery();
return builder[method].apply(builder, arguments);
};
});
return client.StatelessQuery;
});
2013-12-27 14:44:21 -05:00
// Runs a new transaction, taking a container and returning a promise
// for when the transaction is resolved.
knex.transaction = function knex$transaction(container) {
Transaction = Transaction || require('./lib/transaction');
return new Transaction(client).run(container);
};
2013-05-18 00:07:28 -04:00
2013-12-27 14:44:21 -05:00
// Build the "client"
var clientName = config.client;
if (!Clients[clientName]) {
throw new Error(clientName + ' is not a valid Knex client, did you misspell it?');
}
Dialect = require(Clients[clientName]);
client = new Dialect(config);
// Allow chaining methods from the root object, before
// any other information is specified.
_.each(_.keys(client.Query.prototype), function(method) {
if (method.charAt(0) === '_') return;
2013-12-27 14:44:21 -05:00
knex[method] = function() {
var builder = (this instanceof client.Query) ? this : new client.Query();
return builder[method].apply(builder, arguments);
};
});
knex.client = client;
2013-05-18 00:07:28 -04:00
2013-12-27 14:44:21 -05:00
// Namespaces for additional library components.
var schema = knex.schema = {};
var migrate = knex.migrate = {};
2013-05-18 00:07:28 -04:00
2013-12-10 13:09:01 -05:00
// Attach each of the `Schema` "interface" methods directly onto to `knex.schema` namespace, e.g.:
// `knex.schema.table('tableName', function() {...`
// `knex.schema.createTable('tableName', function() {...`
// `knex.schema.dropTableIfExists('tableName');`
2013-12-27 14:44:21 -05:00
_.each(['table', 'createTable', 'editTable', 'dropTable',
'dropTableIfExists', 'renameTable', 'hasTable', 'hasColumn'], function(key) {
schema[key] = function(tableName) {
if (!client.SchemaBuilder) client.initSchema();
var builder = new client.SchemaBuilder();
return builder[key].apply(builder, arguments);
2013-05-08 12:27:33 -04:00
};
2013-12-10 13:09:01 -05:00
});
2013-05-08 12:27:33 -04:00
2013-12-10 13:09:01 -05:00
// Attach each of the `Migrate` "interface" methods directly onto to `knex.migrate` namespace, e.g.:
// knex.migrate.latest().then(...
// knex.migrate.currentVersion(...
_.each(['make', 'latest', 'rollback', 'currentVersion'], function(method) {
2013-12-27 14:44:21 -05:00
migrate[method] = function() {
Migrate = Migrate || require('./lib/migrate');
2013-12-10 13:09:01 -05:00
var migration = new Migrate(knex);
return migration[method].apply(migration, arguments);
};
});
return knex;
};
2013-12-27 14:44:21 -05:00
module.exports = Knex;