db-komodo.js
1.26 KB
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
const mysql = require('mysql');
const config = require('komodo-sdk/config');
exports.ERROR_POOL_NOT_READY = new Error('DB-KOMODO: pool is not ready');
exports.pool = config.mysql ? mysql.createPool({
host: config.mysql.komodo.host || 'localhost',
database: config.mysql.komodo.database || 'komodo',
user: config.mysql.komodo.user || 'komodo',
password: config.mysql.komodo.password,
}) : null;
exports.query = (query, values, cb) => {
// pool.query.apply(null, arguments);
if (!this.pool || !this.pool.query) {
if (typeof cb === 'function') {
cb(this.ERROR_POOL_NOT_READY);
}
return;
}
this.pool.query(query, values, cb);
};
exports.format = (sql, values, cb) => new Promise((resolve, reject) => {
if (!this.pool) {
reject(this.ERROR_POOL_NOT_READY);
if (typeof cb === 'function') cb(this.ERROR_POOL_NOT_READY);
return;
}
this.pool.getConnection((err, connection) => {
if (err) {
reject(err);
if (typeof cb === 'function') cb(err);
return;
}
const formatted = connection.format(sql, values);
connection.release();
resolve(formatted);
if (typeof cb === 'function') cb(null, formatted);
});
});