knex/knex.js

148 lines
5.2 KiB
JavaScript
Raw Normal View History

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
define(function(require, exports, module) {
2013-05-22 13:10:42 -04:00
// Base library dependencies of the app.
var _ = require('underscore');
var when = require('when');
2013-09-04 20:47:27 -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;
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.
var Knex = function(config) {
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.
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 {
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?');
}
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
// 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
knex.grammar = Dialect.grammar;
knex.schemaGrammar = Dialect.schemaGrammar;
2013-09-04 20:47:27 -04:00
// Main namespaces for key library components.
knex.schema = {};
knex.migrate = {};
2013-03-12 11:52:08 -04:00
// Enable the `Builder('tableName')` syntax, as is used in the main `knex('tableName')`.
knex.builder = function(tableName) {
var builder = new Builder(knex);
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
// 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) {
// 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
// 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) {
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
// 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) {
// 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.
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.
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
knex.VERSION = '0.4.0';
2013-05-18 00:07:28 -04:00
// Runs a new transaction, taking a container and returning a promise
// for when the transaction is resolved.
knex.transaction = function(container) {
return new Transaction(knex).run(container);
2013-05-08 12:27:33 -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 = {
'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);
};
});
})(
typeof define === 'function' && define.amd ? define : function (factory) { factory(require, exports, module); }
);