-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (102 loc) · 2.75 KB
/
index.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
'use strict';
const mysql = require('mysql');
const mysql2 = require('mysql2');
const connect = require('./lib/connect');
module.exports = MysqlQuery;
var prototype = MysqlQuery.prototype;
var pool = {};
/**
* @param {object} opt
* @return {MysqlQuery}
* @constructor
*/
function MysqlQuery(opt) {
if (!(this instanceof MysqlQuery)) return new MysqlQuery(opt);
var driver = ~['mysql', 'mysql2'].indexOf(opt.driver) ? opt.driver : 'mysql';
delete opt.driver;
var key = opt.host + opt.user + opt.database;
// pool shall be singleton
if (!pool[key]) {
pool[key] = mysql.createPool(opt);
}
Object.defineProperties(this, {
/**
* @property mysql
*/
mysql: { get: _=> driver === 'mysql' ? mysql : mysql2 },
/**
* @property pool
*/
pool: { get: _=> pool[key] }
});
}
/**
* query(): do not return anything except error
* @protected
* @param {string} method
* @param {string} sql
* @param {*} values
* @param {function} cb
*/
prototype.query = function(method, sql, values, cb) {
//this.pool.getConnection(connect(method, sql, values, cb));
// query(): do not return anything except error
this.pool.getConnection(connect(method, sql, values, err=> cb(err)));
};
/**
* @protected
* @param {string} method
* @param {string} sql
* @param {*} values
* @param {function} cb
*/
prototype.insertId = function(method, sql, values, cb) {
this.pool.getConnection(connect(method, sql, values, function(err, res) {
if (err) return cb(err);
if (res && res.insertId) return cb(null, res.insertId);
cb(null, null);
}))
};
/**
* @protected
* @param {string} method
* @param {string} sql
* @param {*} values
* @param {function} cb
*/
prototype.objectSelect = function(method, sql, values, cb) {
this.pool.getConnection(connect(method, sql, values, function(err, res) {
if (err) return cb(err);
if (!res[0]) return cb(null, null);
cb(null, Object.assign({}, res[0]));
}));
};
/**
* @protected
* @param {string} method
* @param {string} sql
* @param {*} values
* @param {function} cb
*/
prototype.arraySelect = function(method, sql, values, cb) {
this.pool.getConnection(connect(method, sql, values, function(err, res) {
if (err) return cb(err);
cb(null, res.map(raw_data_packet=> Object.assign({}, raw_data_packet)));
}));
};
/**
* Return a payload with attributes limited to whitelist
* @param payload
* @param attributes
* @return {{}}
*/
MysqlQuery.whitelist = function(payload, attributes) {
var state = {};
if (!Array.isArray(attributes) || !attributes.length) return state;
var keys = Object.keys(payload);
for (let i = 0, len = keys.length; i < len; ++i) {
let key = keys[i];
if (attributes.indexOf(key) >= 0) state[key] = payload[key];
}
return state;
};