2013-09-04 20:47:27 -04:00
|
|
|
// Knex.js 0.4.0
|
|
|
|
|
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-08-22 16:04:20 -04:00
|
|
|
(function(define) {
|
|
|
|
|
|
|
|
"use strict";
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-08-21 12:17:37 -04:00
|
|
|
define(function(require, exports, module) {
|
2013-05-22 13:10:42 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
var _ = require('underscore');
|
|
|
|
var when = require('when');
|
|
|
|
|
|
|
|
var Raw = require('./lib/raw').Raw;
|
|
|
|
var Transaction = require('./lib/transaction').Transaction;
|
|
|
|
var Builder = require('./lib/builder').Builder;
|
|
|
|
var Interface = require('./lib/builder/interface').Interface;
|
|
|
|
var Schema = require('./lib/schema').Schema;
|
|
|
|
var ClientBase = require('./clients/clientbase').ClientBase;
|
|
|
|
|
|
|
|
// The `Knex` module, taking either a fully initialized
|
|
|
|
// database client, or a configuration to initialize one. This is something
|
|
|
|
// you'll typically only want to call once per application cycle.
|
|
|
|
var Knex = function(client) {
|
|
|
|
|
|
|
|
// If the client isn't actually a client, we need to configure it into one.
|
|
|
|
// On the client, this isn't acceptable, since we need to return immediately
|
|
|
|
// rather than wait on an async load of a client library.
|
|
|
|
if (!client instanceof ClientBase) {
|
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
throw new Error('A valid `Knex` client must be passed into the Knex constructor.');
|
|
|
|
} else {
|
|
|
|
var clientName = client.client;
|
|
|
|
if (!Clients[clientName]) {
|
|
|
|
throw new Error(clientName + ' is not a valid Knex client, did you misspell it?');
|
|
|
|
}
|
|
|
|
var ClientCtor = require(Clients[clientName]);
|
|
|
|
client = new ClientCtor(_.omit(options, 'client'));
|
|
|
|
}
|
2013-05-18 00:07:28 -04:00
|
|
|
}
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Enables the `Knex('tableName')` shorthand syntax.
|
|
|
|
var instance = function(tableName) {
|
|
|
|
return instance.builder(tableName);
|
|
|
|
};
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Main namespaces for key library components.
|
|
|
|
instance.schema = {};
|
|
|
|
instance.migrate = {};
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Enable the `Builder('tableName')` syntax, as is used in the main `Knex('tableName')`.
|
|
|
|
instance.builder = function(tableName) {
|
|
|
|
var builder = new Builder(instance);
|
|
|
|
return tableName ? builder.table(tableName) : builder;
|
|
|
|
};
|
2013-04-30 18:29:53 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Attach each of the `Builder` "interface" methods direcly onto
|
|
|
|
// the `Knex` object, for ease of use when creating a new query builder chain:
|
|
|
|
// `Knex.select('*').from('tableName').then(...`
|
|
|
|
// `Knex.insert(values).into('tableName').then(...`
|
|
|
|
// `Knex.update(values).then(...`
|
|
|
|
_.each(Interface, function(val, key) {
|
|
|
|
instance[key] = function() {
|
|
|
|
var builder = new Builder(instance);
|
|
|
|
return builder[key].apply(builder, arguments);
|
2013-04-30 18:29:53 -04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Attach each of the `Schema` "interface" methods directly onto to `Knex.Schema` namespace:
|
|
|
|
// `Knex.Schema.table('tableName', function() {...`
|
|
|
|
// `Knex.Schema.createTable('tableName', function() {...`
|
|
|
|
// `Knex.Schema.dropTableIfExists('tableName');`
|
|
|
|
_.each(SchemaInterface, function(val, key) {
|
|
|
|
instance.schema[key] = function() {
|
|
|
|
var schemaBuilder = new SchemaBuilder(instance);
|
|
|
|
schemaBuilder.table = _.first(arguments);
|
|
|
|
return schemaBuilder[key].apply(schemaBuilder, _.rest(arguments));
|
|
|
|
};
|
|
|
|
});
|
2013-04-30 18:29:53 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Attach each of the `Migrate` "interface" methods directly on to
|
|
|
|
_.each(MigrateInterface, function(val, key) {
|
|
|
|
instance.migrate[key] = function() {
|
|
|
|
var migrateBuilder = new MigrateBuilder(instance);
|
|
|
|
return MigrateBuilder[key].apply(migrateBuilder, arguments);
|
|
|
|
};
|
|
|
|
});
|
2013-04-30 18:29:53 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Method to run a new `Raw` query on the current client.
|
|
|
|
instance.raw = function() {
|
|
|
|
var raw = new Raw(instance);
|
|
|
|
return raw.query.apply(raw, arguments);
|
2013-05-08 16:52:44 -04:00
|
|
|
};
|
2013-05-18 00:07:28 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Keep a reference to the current client.
|
|
|
|
instance.client = client;
|
2013-05-18 00:07:28 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Keep in sync with package.json
|
|
|
|
instance.VERSION = '0.5.0';
|
2013-05-18 00:07:28 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Runs a new transaction, taking a container and
|
|
|
|
instance.transaction = function(container) {
|
|
|
|
var transaction = new Transaction(instance);
|
|
|
|
return transaction.run(container);
|
2013-05-08 12:27:33 -04:00
|
|
|
};
|
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Return the new Knex instance.
|
|
|
|
return instance;
|
2013-04-30 18:29:53 -04:00
|
|
|
};
|
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
var Clients = Knex.Clients = {
|
|
|
|
'mysql' : './clients/mysql.js',
|
|
|
|
'pg' : './clients/postgres.js',
|
|
|
|
'postgres' : './clients/postgres.js',
|
|
|
|
'sqlite' : './clients/sqlite3.js',
|
|
|
|
'sqlite3' : './clients/sqlite3.js'
|
2013-04-30 18:29:53 -04:00
|
|
|
};
|
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Used primarily to type-check a potential `Knex` client in `Bookshelf.js`,
|
|
|
|
// by examining whether the object's `client` is an `instanceof Knex.ClientBase`.
|
|
|
|
Knex.ClientBase = ClientBase;
|
2013-04-30 18:29:53 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// finally, export the `Knex` object for node and the browser.
|
2013-03-12 11:52:08 -04:00
|
|
|
module.exports = Knex;
|
|
|
|
|
2013-08-21 12:17:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
})(
|
|
|
|
typeof define === 'function' && define.amd ? define : function (factory) { factory(require, exports, module); }
|
|
|
|
);
|