This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
knex.js
200 lines (168 loc) · 5.27 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
const Promise = require('bluebird');
const assert = require('assert');
const knex = require('knex');
const { formatQuery } = require('knex/lib/execution/internal/query-executioner');
const co = require('co');
const helper = require('./helper');
const SUPPORTS = [ 'sqlite', 'mysql', 'mysql2', 'oracledb', 'mssql', 'maria', 'mariasql', 'mariadb', 'pg', 'postgresql' ];
const isSupport = dialect => SUPPORTS.indexOf(dialect) !== -1;
const canExplain = dialect => [ 'mysql', 'mysql2', 'maria', 'mariasql', 'mariadb' ].includes(dialect);
const clientDesc = config => [
config.filename,
config.host,
config.port,
config.database,
config.user,
].filter(c => !!c).join('#');
module.exports = app => {
init(app);
app.beforeClose(function () {
app.knex && app.knex.end && app.knex.end();
app.logger.info('[egg-knex] knex singleton has been destroyed');
});
};
let count = 0;
function init (app) {
app.knexLogger = app.getLogger('knex') || app.logger;
app.addSingleton('knex', (config, app) => {
config.client = config.client || config.dialect || 'mysql2';
const connection = config.connection;
assert(isSupport(config.client),
`[egg-knex] ${config.client} is not in (${SUPPORTS})`);
app.knexLogger.info(
'[egg-knex] %s connecting %j',
config.client,
connection
);
if (app.config.env === 'local') {
if (config.debug !== false) {
config.debug = true;
}
}
const index = count++;
const done = app.readyCallback(`createKnex-${index}`);
const client = createInstance(app, config, (...args) => {
done(...args);
app.knexLogger.info(`[egg-knex] instance[${index}] status OK`);
});
return client;
});
if (app.config.knex.clients) {
app.knex.end = function (...args) {
const key = args[0];
let callback = args[1];
if (args.length === 1 && typeof args[0] === 'function') {
callback = args[0];
}
if (!key) {
const tasks = [];
for (const client of app.knex.clients) {
tasks.push(client.end());
}
const future = Promise.all(tasks);
if (callback) {
future.then(callback, callback);
return;
}
return future;
}
const client = app.knex.get(key);
if (!client) {
callback = callback || function () { };
callback();
return;
}
return client.destroy(callback);
};
} else {
app.knex.end = app.knex.destroy;
}
}
function injectLogger (app, config) {
if (config.log) {
return;
}
const logger = app.getLogger('knex') || app.logger;
config.log = {};
config.log.warn = logger.warn.bind(logger, '[egg-knex]');
config.log.error = logger.error.bind(logger, '[egg-knex]');
config.log.debug = msg => {
if (isExplainQuery(msg)) return;
logger.debug('[egg-knex]', msg);
};
config.log.deprecate = logger.warn.bind(logger, '[egg-knex] DEPRECATE');
}
function createInstance (app, config, callback) {
injectLogger(app, config);
const client = createKnexClient(config);
client.dialect = config.client;
if (app.instrument) {
injectInstrument(app, client);
}
if (config.debug && canExplain(config.dialect)) {
explainAfterQuery(app, client);
}
co(async function isReady () {
const result = await client.raw(helper.showTables(client.dialect));
const rows = helper.rawResult(client.dialect, result);
if (rows && rows.length) {
return rows.length;
}
}).then(num => {
app.knexLogger.info(`[egg-knex] ${config.client} ${clientDesc(config.connection)} status OK, got ${num} tables`);
callback(undefined, client);
}).catch(e => {
app.knexLogger.error(e);
callback(e);
});
return client;
}
function createKnexClient (config) {
const client = knex(config);
return client;
}
function injectInstrument (app, knex) {
const client = knex.client;
[ 'query', 'stream' ].forEach(method => {
client[`_raw_${method}`] = client[method];
client[method] = function (connection, obj) {
if (typeof obj === 'string') {
obj = { sql: obj };
}
const sql = this.positionBindings(obj.sql);
const ins = app.instrument(client.dialect, sql);
return this[`_raw_${method}`]
.apply(this, [ connection, obj ])
.then(result => {
ins.end();
return result;
});
};
});
}
function explainAfterQuery (app, knex) {
knex.on('query-response', (_response, { sql, bindings }) => {
sql = formatQuery(sql, bindings, null, knex.client).trim();
if (haveQueryPlan(sql)) {
knex.raw(`explain ${sql}`)
.then(result => {
const explains = helper.rawResult(knex.dialect, result);
app.knexLogger.info('[egg-knex] explains of %s\n=====> result: %j', sql, explains);
})
.catch(() => app.knexLogger.info('[egg-knex] Whoops! Explain doesn\'t work with:', sql));
}
});
}
function isExplainQuery (sql) {
if (typeof sql === 'string') {
const method = `${sql}`.split(' ').shift().toUpperCase();
return method === 'EXPLAIN';
}
if (sql && sql.sql) return isExplainQuery(sql.sql);
return false;
}
function haveQueryPlan (sql) {
const method = `${sql}`.split(' ').shift().toUpperCase();
return [ 'INSERT', 'SELECT', 'DELETE', 'UPDATE' ].includes(method);
}