-
Notifications
You must be signed in to change notification settings - Fork 0
/
knex.js
158 lines (135 loc) · 4.8 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Knex.js 0.6.22
// --------------
// (c) 2014 Tim Griesser
// Knex may be freely distributed under the MIT license.
// For details and documentation:
// http://knexjs.org
// 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) {
return new Raw(sql, bindings);
};
// 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');
// Doing it this way makes it easier to build for browserify.
var mysql = function() { return require('./lib/dialects/mysql'); };
var mysql2 = function() { return require('./lib/dialects/mysql2'); };
var maria = function() { return require('./lib/dialects/maria'); };
var pg = function() { return require('./lib/dialects/postgres'); };
var sqlite3 = function() { return require('./lib/dialects/sqlite3'); };
var websql = function() { return require('./lib/dialects/websql'); };
// The client names we'll allow in the `{name: lib}` pairing.
var Clients = Knex.Clients = {
'mysql' : mysql,
'mysql2' : mysql2,
'maria' : maria,
'mariadb' : maria,
'mariasql' : maria,
'pg' : pg,
'postgres' : pg,
'postgresql' : pg,
'sqlite' : sqlite3,
'sqlite3' : sqlite3,
'websql' : websql
};
// Each of the methods which may be statically chained from knex.
var QueryInterface = require('./lib/query/methods');
// Create a new "knex" instance with the appropriate configured client.
Knex.initialize = function(config) {
var Dialect, client;
var EventEmitter = require('events').EventEmitter;
// 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;
if (config.__transactor__) qb.transacting(config.__transactor__);
return tableName ? qb.table(tableName) : qb;
}
// Hook up the "knex" object as an EventEmitter.
var ee = new EventEmitter();
for (var key in ee) {
knex[key] = ee[key];
}
// 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.21';
knex.raw = function(sql, bindings) {
var raw = new client.Raw(sql, bindings);
if (config.__transactor__) raw.transacting(config.__transactor__);
return raw;
};
// Runs a new transaction, taking a container and returning a promise
// for when the transaction is resolved.
knex.transaction = function(container) {
return new client.Transaction(container);
};
// Convenience method for tearing down the pool.
knex.destroy = function (callback) {
return this.client.destroy(callback);
};
if (config.__client__) {
client = config.__client__;
} else {
// Build the "client"
var clientName = config.client || config.dialect;
if (!Clients[clientName]) {
throw new Error(clientName + ' is not a valid Knex client, did you misspell it?');
}
knex.toString = function() {
return '[object Knex:' + clientName + ']';
};
Dialect = Clients[clientName]();
client = new Dialect(config);
// Passthrough all "start" and "query" events to the knex object.
client.on('start', function(obj) {
knex.emit('start', obj);
});
client.on('query', function(obj) {
knex.emit('query', obj);
});
}
// Allow chaining methods from the root object, before
// any other information is specified.
QueryInterface.forEach(function(method) {
knex[method] = function() {
var builder = knex();
return builder[method].apply(builder, arguments);
};
});
knex.client = client;
Object.defineProperties(knex, {
// Lazy-load / return a "schema" builder object, ensuring it's context-aware.
schema: {
get: function() {
if (!client.SchemaBuilder) client.initSchema();
var builder = new client.SchemaBuilder();
if (config.__transactor__) builder.transacting(config.__transactor__);
return builder;
}
},
// Lazy-load / return a `Migrator` object.
migrate: {
get: function() {
if (!client.Migrator) client.initMigrator();
return new client.Migrator(knex);
}
},
// Lazy-load / return a `FunctionHelper` object.
fn: {
get: function() {
if (!client.FunctionHelper) client.initFunctionHelper();
return client.FunctionHelper;
}
}
});
return knex;
};
module.exports = Knex;