-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
256 lines (239 loc) · 6.14 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'use strict';
var debug = require('debug')('mysqldb');
/**
* Start a database transcation and return a promise
* which resolve into a database connection in the transaction
* or reject with error.
* @param {mysql.pool} pool - mysql databse connection pool object.
*/
function beginTransaction(pool) {
return new Promise((resolve, reject) => {
pool.getConnection((err, conn) => {
if (err) {
debug('getConnection---reject---');
reject(err);
} else {
conn.beginTransaction(errr => {
if (errr) {
debug('beginTransaction---reject---');
reject(errr);
} else {
resolve(conn);
}
});
}
});
});
}
/**
* Return a promise which resolve into a database
* connection or reject with an error.
* select afterwards.
* @param {mysql.pool} pool - mysql database connection pool
*/
function beginQuery(pool) {
return new Promise((resolve, reject) => {
pool.getConnection((err, conn) => {
if (err) {
debug('getConnection---reject---');
reject(err);
} else {
resolve(conn);
}
});
});
}
/**
* Commit database changes made from callback function and
* return a promise which is the promise return from the callback
* function or rollback all the changes and reject with an
* error. All the database changes
* should be put inside the callback function sharing same connection object
* and return a promise object.
* @param {myql.pool} pool - mysql database connection pool
* @param {function(conn: mysql.connection) Promise => {}} callback - callback function which includes all the database changes in a transaction.
*
*/
function tx(pool, callback) {
return new Promise((resolve, reject) => {
beginTransaction(pool).then(conn => {
debug('---begin transaction---');
Promise.resolve(conn).then(callback).then(data => {
conn.commit(err => {
debug('---commit---');
if (err) {
debug('commit---reject---');
reject(err);
} else {
resolve(data);
}
});
debug('---connection release');
conn.release();
}).catch(err => {
reject(err);
debug('rollback:%o', err);
conn.rollback(error => {
debug('---rollback---');
});
debug('---connection release');
conn.release();
});
}, err => {
debug('beginTransaction---reject---%o', err);
reject(err);
});
});
}
/**
* return a promise which is the promise return from the callback
* function or reject with an
* error. All the database select statements
* should be put inside the callback function shareing with same connection
* object and return a promise
* object.
* @param {myql.pool} pool - mysql database connection pool
* @param {function(conn: mysql.connection) Promise => {}} callback - callback function which includes all the database changes in a transaction.
*
*/
function sql(pool, callback) {
return new Promise((resolve, reject) => {
beginQuery(pool).then(conn => {
Promise.resolve(conn).then(callback).then(data => {
debug('---connection release');
conn.release();
if (resolve) {
resolve(data);
}
}).catch(err => {
debug('---connection release');
conn.release();
debug('---reject---%o', err);
reject(err);
});
}, err => {
debug('---reject---%o', err);
reject(err);
});
});
}
/**
* return a promise which resolves to null or first record from a select statement
* @param {mysql.connection} conn - the database connection
* @param {squel.ql} ql - select statement
*/
function get(conn, ql) {
debug('start get');
ql.limit(1);
return new Promise((resolve, reject) => {
let param = ql.toParam();
debug('*********sql:%o', param);
conn.query(param.text, param.values, (err, rows, fields) => {
if (err) {
debug('conn.query---reject---');
reject(err);
return;
}
if (rows.length > 0) {
var result = rows[0];
debug('success:%o', result);
resolve(result);
} else {
resolve(null);
}
});
});
}
/**
* return an promise which resolve to an empty array or records from a select statement
* @param {mysql.connection} conn - the database connection
* @param {squel.ql} ql - select statement
*/
function list(conn, ql) {
debug('start list');
return new Promise((resolve, reject) => {
let param = ql.toParam();
debug('*********sql:%o', param);
conn.query(param.text, param.values, (err, rows, fields) => {
if (err) {
debug('conn.query---reject--');
reject(err);
return;
}
var result = rows;
debug('success:%o', result);
resolve(result);
});
});
}
/**
* return a promise which resolve from the result of executing a insert/update/delete statement
* @param {mysql.connection} conn - the database connection
* @param {squel.ql} ql - select statement
*/
function update(conn, ql) {
debug('start update');
return new Promise((resolve, reject) => {
let param = ql.toParam();
debug('*********sql:%o', param);
conn.query(param.text, param.values, (err, result) => {
if (err) {
debug('conn.query---reject---');
reject(err);
return;
}
debug('success:%o', result);
resolve(result);
});
});
}
/**
* return a promise which resolve from the result of executing a insert/update/delete statement
* @param {mysql.connection} conn - the database connection
* @param {string} ql - select statement string
*/
function run(conn, sql) {
debug('start update');
return new Promise((resolve, reject) => {
debug('*********sql:%s', sql);
conn.query(sql, (err, result) => {
if (err) {
debug('conn.query---reject---');
reject(err);
return;
}
debug('success:%o', result);
resolve(result);
});
});
}
/**
* return a promise which resolve from the result rows of executing a select statement
* @param {mysql.connection} conn - the database connection
* @param {string} ql - select statement string
*/
function runList(conn, sql) {
debug('start list');
return new Promise((resolve, reject) => {
debug('*********sql:%s', sql);
conn.query(sql, (err, rows, fields) => {
if (err) {
debug('conn.query---reject---');
reject(err);
return;
}
var result = rows;
debug('success:%o', result);
resolve(result);
});
});
}
module.exports = {
tx,
sql,
get,
list,
update,
run,
runList,
};