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-08 15:57:32 -04:00
|
|
|
// Base library dependencies of the app.
|
|
|
|
var _ = require('underscore');
|
|
|
|
var when = require('when');
|
2013-09-04 20:47:27 -04:00
|
|
|
|
2013-09-08 15:57:32 -04: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.
|
2013-09-04 20:47:27 -04:00
|
|
|
var Raw = require('./lib/raw').Raw;
|
|
|
|
var Transaction = require('./lib/transaction').Transaction;
|
|
|
|
var Builder = require('./lib/builder').Builder;
|
2013-09-05 16:36:49 -04:00
|
|
|
|
|
|
|
// var Interface = require('./lib/builder/interface').Interface;
|
|
|
|
var ClientBase = require('./clients/base').ClientBase;
|
2013-09-08 15:57:32 -04:00
|
|
|
var SchemaBuilder = require('./lib/schemabuilder').SchemaBuilder;
|
|
|
|
var SchemaInterface = require('./lib/schemainterface').SchemaInterface;
|
2013-09-04 20:47:27 -04:00
|
|
|
|
|
|
|
// 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.
|
2013-09-08 15:57:32 -04:00
|
|
|
var Knex = function(config) {
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
var Dialect, client;
|
2013-09-04 20:47:27 -04:00
|
|
|
|
|
|
|
// 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.
|
2013-09-08 15:57:32 -04:00
|
|
|
if (config instanceof ClientBase) {
|
|
|
|
client = config;
|
|
|
|
} else {
|
2013-09-04 20:47:27 -04:00
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
|
throw new Error('A valid `Knex` client must be passed into the Knex constructor.');
|
|
|
|
} else {
|
2013-09-08 15:57:32 -04:00
|
|
|
var clientName = config.client;
|
2013-09-04 20:47:27 -04:00
|
|
|
if (!Clients[clientName]) {
|
|
|
|
throw new Error(clientName + ' is not a valid Knex client, did you misspell it?');
|
|
|
|
}
|
2013-09-11 23:36:55 -04:00
|
|
|
Dialect = require(Clients[clientName]);
|
|
|
|
client = new Dialect.Client(_.omit(config, 'client'));
|
2013-09-04 20:47:27 -04:00
|
|
|
}
|
2013-05-18 00:07:28 -04:00
|
|
|
}
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Enables the `knex('tableName')` shorthand syntax.
|
|
|
|
var knex = function(tableName) {
|
|
|
|
return knex.builder(tableName);
|
2013-09-04 20:47:27 -04:00
|
|
|
};
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
knex.grammar = Dialect.grammar;
|
|
|
|
knex.schemaGrammar = Dialect.schemaGrammar;
|
2013-09-08 15:57:32 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Main namespaces for key library components.
|
2013-09-08 15:57:32 -04:00
|
|
|
knex.schema = {};
|
|
|
|
knex.migrate = {};
|
2013-03-12 11:52:08 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Enable the `Builder('tableName')` syntax, as is used in the main `knex('tableName')`.
|
|
|
|
knex.builder = function(tableName) {
|
|
|
|
var builder = new Builder(knex);
|
2013-09-11 23:36:55 -04:00
|
|
|
return tableName ? builder.from(tableName) : builder;
|
2013-09-04 20:47:27 -04:00
|
|
|
};
|
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
|
2013-09-08 15:57:32 -04:00
|
|
|
// the `knex` object, for ease of use when creating a new query builder chain, e.g.:
|
|
|
|
// `knex.select('*').from('tableName').then(...`
|
|
|
|
// `knex.insert(values).into('tableName').then(...`
|
|
|
|
// `knex.update(values).then(...`
|
2013-09-05 16:36:49 -04:00
|
|
|
// _.each(Interface, function(val, key) {
|
2013-09-08 15:57:32 -04:00
|
|
|
// knex[key] = function() {
|
|
|
|
// var builder = new Builder(knex);
|
2013-09-05 16:36:49 -04:00
|
|
|
// return builder[key].apply(builder, arguments);
|
|
|
|
// };
|
|
|
|
// });
|
2013-04-30 18:29:53 -04:00
|
|
|
|
2013-09-08 15:57:32 -04: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-09-04 20:47:27 -04:00
|
|
|
_.each(SchemaInterface, function(val, key) {
|
2013-09-08 15:57:32 -04:00
|
|
|
knex.schema[key] = function() {
|
|
|
|
var schemaBuilder = new SchemaBuilder(knex);
|
2013-09-04 20:47:27 -04:00
|
|
|
schemaBuilder.table = _.first(arguments);
|
2013-09-05 16:36:49 -04:00
|
|
|
return SchemaInterface[key].apply(schemaBuilder, _.rest(arguments));
|
2013-09-04 20:47:27 -04:00
|
|
|
};
|
|
|
|
});
|
2013-04-30 18:29:53 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Attach each of the `Migrate` "interface" methods directly on to the `knex.
|
2013-09-05 16:36:49 -04:00
|
|
|
// _.each(MigrateInterface, function(val, key) {
|
2013-09-08 15:57:32 -04:00
|
|
|
// knex.migrate[key] = function() {
|
|
|
|
// var migrateBuilder = new MigrateBuilder(knex);
|
2013-09-05 16:36:49 -04:00
|
|
|
// 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.
|
2013-09-08 15:57:32 -04:00
|
|
|
knex.raw = function(sql, bindings) {
|
|
|
|
return new Raw(knex).query(sql, bindings);
|
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.
|
2013-09-08 15:57:32 -04:00
|
|
|
knex.client = client;
|
2013-05-18 00:07:28 -04:00
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
// Keep in sync with package.json
|
2013-09-08 15:57:32 -04:00
|
|
|
knex.VERSION = '0.4.0';
|
2013-05-18 00:07:28 -04:00
|
|
|
|
2013-09-13 10:51:12 -04:00
|
|
|
// Runs a new transaction, taking a container and returning a promise
|
|
|
|
// for when the transaction is resolved.
|
2013-09-08 15:57:32 -04:00
|
|
|
knex.transaction = function(container) {
|
|
|
|
return new Transaction(knex).run(container);
|
2013-05-08 12:27:33 -04:00
|
|
|
};
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Return the new `Knex` instance.
|
|
|
|
return knex;
|
2013-04-30 18:29:53 -04:00
|
|
|
};
|
|
|
|
|
2013-09-04 20:47:27 -04:00
|
|
|
var Clients = Knex.Clients = {
|
2013-09-08 15:57:32 -04:00
|
|
|
'mysql' : './clients/server/mysql.js',
|
|
|
|
'pg' : './clients/server/postgres.js',
|
|
|
|
'postgres' : './clients/server/postgres.js',
|
|
|
|
'sqlite' : './clients/server/sqlite3.js',
|
|
|
|
'sqlite3' : './clients/server/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-09-05 16:36:49 -04:00
|
|
|
Knex.initialize = function(config) {
|
|
|
|
return Knex(config);
|
|
|
|
};
|
|
|
|
|
2013-08-21 12:17:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
})(
|
|
|
|
typeof define === 'function' && define.amd ? define : function (factory) { factory(require, exports, module); }
|
|
|
|
);
|