knex/knex.js

124 lines
4.2 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
// (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
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);
}
// 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) {
2013-12-27 14:44:21 -05:00
return new Raw(sql, bindings);
};
2013-12-10 13:09:01 -05:00
// 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.
var Raw = require('./lib/raw');
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/dialects/mysql',
'pg' : './lib/dialects/postgres',
'postgres' : './lib/dialects/postgres',
'postgresql' : './lib/dialects/postgres',
'sqlite' : './lib/dialects/sqlite3',
'sqlite3' : './lib/dialects/sqlite3',
'websql' : './lib/dialects/websql'
2013-12-27 14:44:21 -05:00
};
2013-12-10 13:09:01 -05:00
// Require lodash.
var _ = require('lodash');
// Each of the methods which may be statically chained from knex.
2014-04-16 01:23:50 -04:00
var QueryInterface = require('./lib/query/methods');
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.QueryBuilder;
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(sql, bindings) {
return new client.Raw(sql, bindings);
2013-12-10 13:09:01 -05:00
};
2013-04-30 18:29:53 -04:00
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(container) {
2014-04-16 01:23:50 -04:00
return new client.Transaction(container);
2013-12-27 14:44:21 -05:00
};
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(QueryInterface, function(method) {
if (method.charAt(0) === '_') return;
2013-12-27 14:44:21 -05:00
knex[method] = function() {
var builder = (this instanceof client.QueryBuilder) ? this : new client.QueryBuilder();
2013-12-27 14:44:21 -05:00
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
// Attach each of the `Migrator` "interface" methods directly onto to `knex.migrate` namespace, e.g.:
2013-12-10 13:09:01 -05:00
// knex.migrate.latest().then(...
// knex.migrate.currentVersion(...
_.each(['make', 'latest', 'rollback', 'currentVersion'], function(method) {
migrate[method] = function(config) {
if (!client.Migrator) client.initMigrator();
var migrator = new client.Migrator(config);
return migrator[method].apply(migrator, arguments);
2013-12-10 13:09:01 -05:00
};
});
return knex;
};
// Convenience for checking whether `obj instanceof Knex.Client`.
Knex.Client = require('./lib/client');
2013-12-27 14:44:21 -05:00
module.exports = Knex;