-
Notifications
You must be signed in to change notification settings - Fork 0
/
knex.js
133 lines (111 loc) · 4.68 KB
/
knex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Knex.js 0.5.11
// --------------
// (c) 2013 Tim Griesser
// Knex may be freely distributed under the MIT license.
// For details and documentation:
// http://knexjs.org
// 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.
var Raw = require('./lib/raw').Raw;
var Transaction = require('./lib/transaction').Transaction;
var Builder = require('./lib/builder').Builder;
var Promise = require('./lib/promise').Promise;
var ClientBase = require('./clients/base').ClientBase;
var SchemaBuilder = require('./lib/schemabuilder').SchemaBuilder;
var SchemaInterface = require('./lib/schemainterface').SchemaInterface;
// 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;
// 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 {
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;
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'));
}
}
// Enables the `knex('tableName')` shorthand syntax.
var knex = function(tableName) {
return knex.builder(tableName);
};
knex.grammar = client.grammar;
knex.schemaGrammar = client.schemaGrammar;
// Main namespaces for key library components.
knex.schema = {};
knex.migrate = {};
// 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;
};
// 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');`
_.each(SchemaInterface, function(val, key) {
knex.schema[key] = function() {
var schemaBuilder = new SchemaBuilder(knex);
var table = schemaBuilder.table = _.first(arguments);
if (!table) {
return Promise.reject(new Error('The table must be defined for the ' + key + ' method.'));
}
return SchemaInterface[key].apply(schemaBuilder, _.rest(arguments));
};
});
// Method to run a new `Raw` query on the current client.
knex.raw = function(sql, bindings) {
return new Raw(knex).query(sql, bindings);
};
// Keep a reference to the current client.
knex.client = client;
// Keep in sync with package.json
knex.VERSION = '0.5.11';
// 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);
};
// 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) {
knex.migrate[method] = function() {
var Migrate = require('./lib/migrate');
var migration = new Migrate(knex);
return migration[method].apply(migration, arguments);
};
});
// Return the new `Knex` instance.
return knex;
};
// The client names we'll allow in the `{name: lib}` pairing.
var Clients = Knex.Clients = {
'mysql' : './clients/server/mysql.js',
'pg' : './clients/server/postgres.js',
'postgres' : './clients/server/postgres.js',
'postgresql' : './clients/server/postgres.js',
'sqlite' : './clients/server/sqlite3.js',
'sqlite3' : './clients/server/sqlite3.js'
};
// 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;
// finally, export the `Knex` object for node and the browser.
module.exports = Knex;
Knex.initialize = function(config) {
return Knex(config);
};