mysql2-core 0.3.5 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -92
- package/lib/index.js +317 -44
- package/package.json +2 -2
- package/src/build.ts +5 -7
- package/src/index.ts +53 -43
- package/tsconfig.json +3 -1
- package/lib/build.js +0 -289
package/README.md
CHANGED
|
@@ -93,10 +93,10 @@ without hiding SQL from developers.
|
|
|
93
93
|
Executor Interface
|
|
94
94
|
│
|
|
95
95
|
▼
|
|
96
|
-
|
|
96
|
+
MySQL Adapter
|
|
97
97
|
┌──────────────┼──────────────┐
|
|
98
98
|
▼ ▼ ▼
|
|
99
|
-
Connection Transaction
|
|
99
|
+
Connection Transaction Stream
|
|
100
100
|
│ │ │
|
|
101
101
|
└──────────────┼──────────────┘
|
|
102
102
|
▼
|
|
@@ -146,15 +146,8 @@ const db = new MySQLDB({
|
|
|
146
146
|
|
|
147
147
|
```ts
|
|
148
148
|
const users = await db.query<User>(
|
|
149
|
-
|
|
150
|
-
`
|
|
151
|
-
SELECT *
|
|
152
|
-
FROM users
|
|
153
|
-
WHERE age > ?
|
|
154
|
-
`,
|
|
155
|
-
|
|
149
|
+
`SELECT * FROM users WHERE age > ?`,
|
|
156
150
|
[18]
|
|
157
|
-
|
|
158
151
|
)
|
|
159
152
|
```
|
|
160
153
|
|
|
@@ -164,15 +157,8 @@ const users = await db.query<User>(
|
|
|
164
157
|
|
|
165
158
|
```ts
|
|
166
159
|
await db.execute(
|
|
167
|
-
|
|
168
|
-
`
|
|
169
|
-
UPDATE users
|
|
170
|
-
SET active = ?
|
|
171
|
-
WHERE id = ?
|
|
172
|
-
`,
|
|
173
|
-
|
|
160
|
+
`UPDATE users SET active = ? WHERE id = ?`
|
|
174
161
|
[true, 10]
|
|
175
|
-
|
|
176
162
|
)
|
|
177
163
|
```
|
|
178
164
|
|
|
@@ -184,25 +170,14 @@ await db.execute(
|
|
|
184
170
|
const tx = await db.beginTransaction()
|
|
185
171
|
|
|
186
172
|
try {
|
|
187
|
-
|
|
188
173
|
await tx.execute(
|
|
189
|
-
|
|
190
|
-
`
|
|
191
|
-
INSERT INTO users(name)
|
|
192
|
-
VALUES(?)
|
|
193
|
-
`,
|
|
194
|
-
|
|
174
|
+
`INSERT INTO users(name) VALUES(?)`,
|
|
195
175
|
["John"]
|
|
196
|
-
|
|
197
176
|
)
|
|
198
|
-
|
|
199
177
|
await tx.commit()
|
|
200
|
-
|
|
201
178
|
}
|
|
202
179
|
catch (err) {
|
|
203
|
-
|
|
204
180
|
await tx.rollback()
|
|
205
|
-
|
|
206
181
|
}
|
|
207
182
|
```
|
|
208
183
|
|
|
@@ -212,27 +187,14 @@ catch (err) {
|
|
|
212
187
|
|
|
213
188
|
```ts
|
|
214
189
|
await db.executeBatch([
|
|
215
|
-
|
|
216
190
|
{
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
"INSERT INTO users(name) VALUES(?)",
|
|
220
|
-
|
|
221
|
-
params:
|
|
222
|
-
["John"]
|
|
223
|
-
|
|
191
|
+
query: "INSERT INTO users(name) VALUES(?)",
|
|
192
|
+
params: ["John"]
|
|
224
193
|
},
|
|
225
|
-
|
|
226
194
|
{
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
"INSERT INTO users(name) VALUES(?)",
|
|
230
|
-
|
|
231
|
-
params:
|
|
232
|
-
["Jane"]
|
|
233
|
-
|
|
195
|
+
query: "INSERT INTO users(name) VALUES(?)",
|
|
196
|
+
params: ["Jane"]
|
|
234
197
|
}
|
|
235
|
-
|
|
236
198
|
])
|
|
237
199
|
```
|
|
238
200
|
|
|
@@ -244,17 +206,11 @@ Stream large result sets without loading everything into memory.
|
|
|
244
206
|
|
|
245
207
|
```ts
|
|
246
208
|
await db.stream(
|
|
247
|
-
|
|
248
209
|
"SELECT * FROM users",
|
|
249
|
-
|
|
250
210
|
[],
|
|
251
|
-
|
|
252
211
|
async user => {
|
|
253
|
-
|
|
254
212
|
console.log(user)
|
|
255
|
-
|
|
256
213
|
}
|
|
257
|
-
|
|
258
214
|
)
|
|
259
215
|
```
|
|
260
216
|
|
|
@@ -307,14 +263,8 @@ Export query results without loading everything into memory.
|
|
|
307
263
|
|
|
308
264
|
```ts
|
|
309
265
|
await exporter.export(
|
|
310
|
-
|
|
311
|
-
`
|
|
312
|
-
SELECT *
|
|
313
|
-
FROM users
|
|
314
|
-
`,
|
|
315
|
-
|
|
266
|
+
`SELECT * FROM users`,
|
|
316
267
|
"users.csv"
|
|
317
|
-
|
|
318
268
|
)
|
|
319
269
|
```
|
|
320
270
|
|
|
@@ -378,31 +328,31 @@ Perfect for
|
|
|
378
328
|
Designed to work seamlessly with **SQL Repository**.
|
|
379
329
|
|
|
380
330
|
```
|
|
381
|
-
Application
|
|
331
|
+
Application
|
|
382
332
|
|
|
383
|
-
|
|
333
|
+
│
|
|
384
334
|
|
|
385
|
-
|
|
335
|
+
▼
|
|
386
336
|
|
|
387
337
|
SqlRepository
|
|
388
338
|
|
|
389
|
-
|
|
339
|
+
│
|
|
390
340
|
|
|
391
|
-
|
|
341
|
+
▼
|
|
392
342
|
|
|
393
|
-
Executor
|
|
343
|
+
Executor
|
|
394
344
|
|
|
395
|
-
|
|
345
|
+
│
|
|
396
346
|
|
|
397
|
-
|
|
347
|
+
▼
|
|
398
348
|
|
|
399
349
|
MySQL Adapter
|
|
400
350
|
|
|
401
|
-
|
|
351
|
+
│
|
|
402
352
|
|
|
403
|
-
|
|
353
|
+
▼
|
|
404
354
|
|
|
405
|
-
mysql2
|
|
355
|
+
mysql2
|
|
406
356
|
```
|
|
407
357
|
|
|
408
358
|
The repository layer never depends directly on mysql2.
|
|
@@ -414,19 +364,19 @@ The repository layer never depends directly on mysql2.
|
|
|
414
364
|
Internally the adapter manages a connection pool.
|
|
415
365
|
|
|
416
366
|
```
|
|
417
|
-
Application
|
|
367
|
+
Application
|
|
418
368
|
|
|
419
|
-
|
|
369
|
+
│
|
|
420
370
|
|
|
421
|
-
|
|
371
|
+
▼
|
|
422
372
|
|
|
423
373
|
Pool Manager
|
|
424
374
|
|
|
425
|
-
|
|
375
|
+
│
|
|
426
376
|
|
|
427
|
-
|
|
377
|
+
▼
|
|
428
378
|
|
|
429
|
-
mysql2 Pool
|
|
379
|
+
mysql2 Pool
|
|
430
380
|
```
|
|
431
381
|
|
|
432
382
|
Connection reuse improves performance.
|
|
@@ -436,25 +386,25 @@ Connection reuse improves performance.
|
|
|
436
386
|
# Transaction Model
|
|
437
387
|
|
|
438
388
|
```
|
|
439
|
-
Application
|
|
389
|
+
Application
|
|
440
390
|
|
|
441
|
-
|
|
391
|
+
│
|
|
442
392
|
|
|
443
|
-
|
|
393
|
+
▼
|
|
444
394
|
|
|
445
|
-
Transaction
|
|
395
|
+
Transaction
|
|
446
396
|
|
|
447
|
-
|
|
397
|
+
│
|
|
448
398
|
|
|
449
|
-
|
|
399
|
+
▼
|
|
450
400
|
|
|
451
401
|
PoolConnection
|
|
452
402
|
|
|
453
|
-
|
|
403
|
+
│
|
|
454
404
|
|
|
455
|
-
|
|
405
|
+
▼
|
|
456
406
|
|
|
457
|
-
mysql2
|
|
407
|
+
mysql2
|
|
458
408
|
```
|
|
459
409
|
|
|
460
410
|
Transactions are isolated from application code.
|
|
@@ -595,14 +545,14 @@ Together they provide a lightweight, SQL-first data access framework.
|
|
|
595
545
|
|
|
596
546
|
---
|
|
597
547
|
|
|
598
|
-
#
|
|
548
|
+
# Contributing
|
|
599
549
|
|
|
600
|
-
|
|
550
|
+
Contributions are welcome!
|
|
601
551
|
|
|
602
|
-
|
|
552
|
+
Feel free to submit issues, feature requests, or pull requests to improve the project.
|
|
603
553
|
|
|
604
|
-
|
|
554
|
+
---
|
|
605
555
|
|
|
606
|
-
|
|
556
|
+
# License
|
|
607
557
|
|
|
608
|
-
|
|
558
|
+
MIT License
|
package/lib/index.js
CHANGED
|
@@ -8,8 +8,293 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { createPool as createPool2 } from "mysql2";
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export function param(i) {
|
|
12
|
+
return "?";
|
|
13
|
+
}
|
|
14
|
+
export function params(length, from) {
|
|
15
|
+
if (from == null) {
|
|
16
|
+
from = 0;
|
|
17
|
+
}
|
|
18
|
+
const ps = [];
|
|
19
|
+
for (let i = 1; i <= length; i++) {
|
|
20
|
+
ps.push(param(i + from));
|
|
21
|
+
}
|
|
22
|
+
return ps;
|
|
23
|
+
}
|
|
24
|
+
export function metadata(attrs) {
|
|
25
|
+
const mp = {};
|
|
26
|
+
const ks = Object.keys(attrs);
|
|
27
|
+
const ats = [];
|
|
28
|
+
const bools = [];
|
|
29
|
+
const fields = [];
|
|
30
|
+
const m = { keys: ats, fields };
|
|
31
|
+
let isMap = false;
|
|
32
|
+
for (const k of ks) {
|
|
33
|
+
const attr = attrs[k];
|
|
34
|
+
attr.name = k;
|
|
35
|
+
if (attr.key) {
|
|
36
|
+
ats.push(attr);
|
|
37
|
+
}
|
|
38
|
+
if (!attr.ignored) {
|
|
39
|
+
fields.push(k);
|
|
40
|
+
}
|
|
41
|
+
if (attr.type === "boolean") {
|
|
42
|
+
bools.push(attr);
|
|
43
|
+
}
|
|
44
|
+
if (attr.version) {
|
|
45
|
+
m.version = k;
|
|
46
|
+
}
|
|
47
|
+
const field = attr.column ? attr.column : k;
|
|
48
|
+
const s = field.toLowerCase();
|
|
49
|
+
if (s !== k) {
|
|
50
|
+
mp[s] = k;
|
|
51
|
+
isMap = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (isMap) {
|
|
55
|
+
m.map = mp;
|
|
56
|
+
}
|
|
57
|
+
if (bools.length > 0) {
|
|
58
|
+
m.bools = bools;
|
|
59
|
+
}
|
|
60
|
+
return m;
|
|
61
|
+
}
|
|
62
|
+
export function buildToSave(obj, table, attrs, ver, buildParam, i) {
|
|
63
|
+
if (!i) {
|
|
64
|
+
i = 1;
|
|
65
|
+
}
|
|
66
|
+
if (!buildParam) {
|
|
67
|
+
buildParam = param;
|
|
68
|
+
}
|
|
69
|
+
const ks = Object.keys(attrs);
|
|
70
|
+
const pks = [];
|
|
71
|
+
const cols = [];
|
|
72
|
+
const values = [];
|
|
73
|
+
const args = [];
|
|
74
|
+
let isVersion = false;
|
|
75
|
+
const o = obj;
|
|
76
|
+
for (const k of ks) {
|
|
77
|
+
const attr = attrs[k];
|
|
78
|
+
attr.name = k;
|
|
79
|
+
if (attr.key) {
|
|
80
|
+
pks.push(attr);
|
|
81
|
+
}
|
|
82
|
+
let v = o[k];
|
|
83
|
+
if (v == null) {
|
|
84
|
+
v = attr.default;
|
|
85
|
+
}
|
|
86
|
+
if (v != null && !attr.ignored && !attr.noinsert) {
|
|
87
|
+
const field = attr.column ? attr.column : k;
|
|
88
|
+
cols.push(field);
|
|
89
|
+
if (k === ver) {
|
|
90
|
+
isVersion = true;
|
|
91
|
+
values.push(`${1}`);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
if (v === "") {
|
|
95
|
+
values.push(`''`);
|
|
96
|
+
}
|
|
97
|
+
else if (typeof v === "number") {
|
|
98
|
+
values.push(toString(v));
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
const p = buildParam(i++);
|
|
102
|
+
values.push(p);
|
|
103
|
+
if (typeof v === "boolean") {
|
|
104
|
+
if (v === true) {
|
|
105
|
+
const v2 = attr.true ? attr.true : `'1'`;
|
|
106
|
+
args.push(v2);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const v2 = attr.false ? attr.false : `'0'`;
|
|
110
|
+
args.push(v2);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
args.push(v);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!isVersion && ver && ver.length > 0) {
|
|
121
|
+
const attr = attrs[ver];
|
|
122
|
+
const field = attr.column ? attr.column : ver;
|
|
123
|
+
cols.push(field);
|
|
124
|
+
values.push(`${1}`);
|
|
125
|
+
}
|
|
126
|
+
if (pks.length === 0) {
|
|
127
|
+
if (cols.length === 0) {
|
|
128
|
+
return { query: "", params: args };
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
132
|
+
return { query: q, params: args };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
const colSet = [];
|
|
137
|
+
for (const k of ks) {
|
|
138
|
+
const v = o[k];
|
|
139
|
+
if (v !== undefined) {
|
|
140
|
+
const attr = attrs[k];
|
|
141
|
+
if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
|
|
142
|
+
const field = attr.column ? attr.column : k;
|
|
143
|
+
let x;
|
|
144
|
+
if (v === null) {
|
|
145
|
+
x = "null";
|
|
146
|
+
}
|
|
147
|
+
else if (v === "") {
|
|
148
|
+
x = `''`;
|
|
149
|
+
}
|
|
150
|
+
else if (typeof v === "number") {
|
|
151
|
+
x = toString(v);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
x = buildParam(i++);
|
|
155
|
+
if (typeof v === "boolean") {
|
|
156
|
+
if (v === true) {
|
|
157
|
+
const v2 = attr.true ? "" + attr.true : `'1'`;
|
|
158
|
+
args.push(v2);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
const v2 = attr.false ? "" + attr.false : `'0'`;
|
|
162
|
+
args.push(v2);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
args.push(v);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
colSet.push(`${field}=${x}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (colSet.length === 0) {
|
|
174
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
175
|
+
return { query: q, params: args };
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`;
|
|
179
|
+
return { query: q, params: args };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
|
|
184
|
+
if (!buildParam) {
|
|
185
|
+
buildParam = param;
|
|
186
|
+
}
|
|
187
|
+
const sts = [];
|
|
188
|
+
const ks = Object.keys(attrs);
|
|
189
|
+
for (const obj of objs) {
|
|
190
|
+
let i = 1;
|
|
191
|
+
const cols = [];
|
|
192
|
+
const values = [];
|
|
193
|
+
const args = [];
|
|
194
|
+
let isVersion = false;
|
|
195
|
+
const o = obj;
|
|
196
|
+
for (const k of ks) {
|
|
197
|
+
const attr = attrs[k];
|
|
198
|
+
let v = o[k];
|
|
199
|
+
if (v == null) {
|
|
200
|
+
v = attr.default;
|
|
201
|
+
}
|
|
202
|
+
if (v != null && v !== undefined && !attr.ignored && !attr.noinsert) {
|
|
203
|
+
const field = attr.column ? attr.column : k;
|
|
204
|
+
cols.push(field);
|
|
205
|
+
if (k === ver) {
|
|
206
|
+
isVersion = true;
|
|
207
|
+
values.push(`${1}`);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
if (v === "") {
|
|
211
|
+
values.push(`''`);
|
|
212
|
+
}
|
|
213
|
+
else if (typeof v === "number") {
|
|
214
|
+
values.push(toString(v));
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
const p = buildParam(i++);
|
|
218
|
+
values.push(p);
|
|
219
|
+
if (typeof v === "boolean") {
|
|
220
|
+
if (v === true) {
|
|
221
|
+
const v2 = attr.true ? attr.true : `'1'`;
|
|
222
|
+
args.push(v2);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
const v2 = attr.false ? attr.false : `'0'`;
|
|
226
|
+
args.push(v2);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
args.push(v);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (!isVersion && ver && ver.length > 0) {
|
|
237
|
+
const attr = attrs[ver];
|
|
238
|
+
const field = attr.column ? attr.column : ver;
|
|
239
|
+
cols.push(field);
|
|
240
|
+
values.push(`${1}`);
|
|
241
|
+
}
|
|
242
|
+
const colSet = [];
|
|
243
|
+
for (const k of ks) {
|
|
244
|
+
const v = o[k];
|
|
245
|
+
if (v !== undefined) {
|
|
246
|
+
const attr = attrs[k];
|
|
247
|
+
if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
|
|
248
|
+
const field = attr.column ? attr.column : k;
|
|
249
|
+
let x;
|
|
250
|
+
if (v === null) {
|
|
251
|
+
x = "null";
|
|
252
|
+
}
|
|
253
|
+
else if (v === "") {
|
|
254
|
+
x = `''`;
|
|
255
|
+
}
|
|
256
|
+
else if (typeof v === "number") {
|
|
257
|
+
x = toString(v);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
x = buildParam(i++);
|
|
261
|
+
if (typeof v === "boolean") {
|
|
262
|
+
if (v === true) {
|
|
263
|
+
const v2 = attr.true ? "" + attr.true : `'1'`;
|
|
264
|
+
args.push(v2);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
const v2 = attr.false ? "" + attr.false : `'0'`;
|
|
268
|
+
args.push(v2);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
args.push(v);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
colSet.push(`${field}=${x}`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (colSet.length === 0) {
|
|
280
|
+
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")});`;
|
|
281
|
+
const smt = { query: q, params: args };
|
|
282
|
+
sts.push(smt);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")};`;
|
|
286
|
+
const smt = { query: q, params: args };
|
|
287
|
+
sts.push(smt);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return sts;
|
|
291
|
+
}
|
|
292
|
+
export function toString(v) {
|
|
293
|
+
if (v === v && v !== Infinity && v !== -Infinity) {
|
|
294
|
+
return "" + v;
|
|
295
|
+
}
|
|
296
|
+
return "null";
|
|
297
|
+
}
|
|
13
298
|
export class resource {
|
|
14
299
|
}
|
|
15
300
|
export function createPool(conf) {
|
|
@@ -987,58 +1272,46 @@ export class MySQLBatchWriter {
|
|
|
987
1272
|
}
|
|
988
1273
|
}
|
|
989
1274
|
export class MySQLChecker {
|
|
990
|
-
constructor(pool, service, timeout) {
|
|
1275
|
+
constructor(pool, service, timeout = 4500) {
|
|
991
1276
|
this.pool = pool;
|
|
992
|
-
this.timeout = timeout
|
|
993
|
-
this.service = service
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1277
|
+
this.timeout = timeout;
|
|
1278
|
+
this.service = service || "mysql";
|
|
1279
|
+
}
|
|
1280
|
+
name() {
|
|
1281
|
+
return this.service;
|
|
1282
|
+
}
|
|
1283
|
+
build(data, error) {
|
|
1284
|
+
return Object.assign(Object.assign({ name: this.name(), healthy: error == null, timestamp: new Date().toISOString() }, data), (error && {
|
|
1285
|
+
error: {
|
|
1286
|
+
message: error.message,
|
|
1287
|
+
code: error.code,
|
|
1288
|
+
},
|
|
1289
|
+
}));
|
|
997
1290
|
}
|
|
998
1291
|
check() {
|
|
999
1292
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1000
|
-
const
|
|
1001
|
-
|
|
1002
|
-
this.pool.query("
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
else {
|
|
1007
|
-
resolve(obj);
|
|
1008
|
-
}
|
|
1009
|
-
});
|
|
1010
|
-
});
|
|
1011
|
-
if (this.timeout > 0) {
|
|
1012
|
-
return promiseTimeOut(this.timeout, promise);
|
|
1293
|
+
const started = Date.now();
|
|
1294
|
+
try {
|
|
1295
|
+
yield Promise.race([this.pool.query("SELECT 1"), this.timeoutPromise()]);
|
|
1296
|
+
return this.build({
|
|
1297
|
+
latency: Date.now() - started,
|
|
1298
|
+
}, null);
|
|
1013
1299
|
}
|
|
1014
|
-
|
|
1015
|
-
return
|
|
1300
|
+
catch (err) {
|
|
1301
|
+
return this.build({
|
|
1302
|
+
latency: Date.now() - started,
|
|
1303
|
+
}, err);
|
|
1016
1304
|
}
|
|
1017
1305
|
});
|
|
1018
1306
|
}
|
|
1019
|
-
|
|
1020
|
-
return
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
data = {};
|
|
1026
|
-
}
|
|
1027
|
-
data["error"] = err;
|
|
1028
|
-
}
|
|
1029
|
-
return data;
|
|
1307
|
+
timeoutPromise() {
|
|
1308
|
+
return new Promise((_, reject) => setTimeout(() => {
|
|
1309
|
+
const error = new Error("Health check timeout");
|
|
1310
|
+
error.code = "TIMEOUT";
|
|
1311
|
+
reject(error);
|
|
1312
|
+
}, this.timeout));
|
|
1030
1313
|
}
|
|
1031
1314
|
}
|
|
1032
|
-
function promiseTimeOut(timeoutInMilliseconds, promise) {
|
|
1033
|
-
return Promise.race([
|
|
1034
|
-
promise,
|
|
1035
|
-
new Promise((resolve, reject) => {
|
|
1036
|
-
setTimeout(() => {
|
|
1037
|
-
reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`);
|
|
1038
|
-
}, timeoutInMilliseconds);
|
|
1039
|
-
}),
|
|
1040
|
-
]);
|
|
1041
|
-
}
|
|
1042
1315
|
export class Exporter {
|
|
1043
1316
|
constructor(connection, buildQuery, ft, write, end, attributes) {
|
|
1044
1317
|
this.connection = connection;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mysql2-core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "mysql2-core",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"clean:lib": "rimraf lib"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"mysql2": "^3.
|
|
12
|
+
"mysql2": "^3.23.1",
|
|
13
13
|
"tslint": "5.10.0",
|
|
14
14
|
"typescript": "^3.3.3333"
|
|
15
15
|
},
|
package/src/build.ts
CHANGED
|
@@ -83,7 +83,7 @@ export function buildToSave<T>(obj: T, table: string, attrs: Attributes, ver?: s
|
|
|
83
83
|
if (v == null) {
|
|
84
84
|
v = attr.default
|
|
85
85
|
}
|
|
86
|
-
if (v
|
|
86
|
+
if (v != null && !attr.ignored && !attr.noinsert) {
|
|
87
87
|
const field = attr.column ? attr.column : k
|
|
88
88
|
cols.push(field)
|
|
89
89
|
if (k === ver) {
|
|
@@ -265,11 +265,9 @@ export function buildToSaveBatch<T>(objs: T[], table: string, attrs: Attributes,
|
|
|
265
265
|
}
|
|
266
266
|
return sts
|
|
267
267
|
}
|
|
268
|
-
const n = "NaN"
|
|
269
268
|
export function toString(v: number): string {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
x = "null"
|
|
269
|
+
if (v === v && v !== Infinity && v !== -Infinity) {
|
|
270
|
+
return "" + v
|
|
273
271
|
}
|
|
274
|
-
return
|
|
275
|
-
}
|
|
272
|
+
return "null"
|
|
273
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -985,61 +985,71 @@ export interface AnyMap {
|
|
|
985
985
|
[key: string]: any
|
|
986
986
|
}
|
|
987
987
|
// tslint:disable-next-line:max-classes-per-file
|
|
988
|
-
export
|
|
989
|
-
|
|
990
|
-
|
|
988
|
+
export interface HealthChecker {
|
|
989
|
+
name(): string
|
|
990
|
+
build(data: AnyMap, error: any): AnyMap
|
|
991
|
+
check(): Promise<AnyMap>
|
|
992
|
+
}
|
|
993
|
+
export class MySQLChecker implements HealthChecker {
|
|
994
|
+
protected readonly service: string
|
|
991
995
|
constructor(
|
|
992
|
-
|
|
996
|
+
protected readonly pool: Pool,
|
|
993
997
|
service?: string,
|
|
994
|
-
timeout
|
|
998
|
+
protected readonly timeout = 4500,
|
|
995
999
|
) {
|
|
996
|
-
this.
|
|
997
|
-
this.service = service ? service : "mysql"
|
|
998
|
-
this.check = this.check.bind(this)
|
|
999
|
-
this.name = this.name.bind(this)
|
|
1000
|
-
this.build = this.build.bind(this)
|
|
1001
|
-
}
|
|
1002
|
-
async check(): Promise<AnyMap> {
|
|
1003
|
-
const obj = {} as AnyMap
|
|
1004
|
-
const promise = new Promise<any>((resolve, reject) => {
|
|
1005
|
-
this.pool.query("select current_time", (err, result) => {
|
|
1006
|
-
if (err) {
|
|
1007
|
-
return reject(err)
|
|
1008
|
-
} else {
|
|
1009
|
-
resolve(obj)
|
|
1010
|
-
}
|
|
1011
|
-
})
|
|
1012
|
-
})
|
|
1013
|
-
if (this.timeout > 0) {
|
|
1014
|
-
return promiseTimeOut(this.timeout, promise)
|
|
1015
|
-
} else {
|
|
1016
|
-
return promise
|
|
1017
|
-
}
|
|
1000
|
+
this.service = service || "mysql"
|
|
1018
1001
|
}
|
|
1002
|
+
|
|
1019
1003
|
name(): string {
|
|
1020
1004
|
return this.service
|
|
1021
1005
|
}
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1006
|
+
|
|
1007
|
+
build(data: AnyMap, error: any): AnyMap {
|
|
1008
|
+
return {
|
|
1009
|
+
name: this.name(),
|
|
1010
|
+
healthy: error == null,
|
|
1011
|
+
timestamp: new Date().toISOString(),
|
|
1012
|
+
...data,
|
|
1013
|
+
...(error && {
|
|
1014
|
+
error: {
|
|
1015
|
+
message: error.message,
|
|
1016
|
+
code: error.code,
|
|
1017
|
+
},
|
|
1018
|
+
}),
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
async check(): Promise<AnyMap> {
|
|
1023
|
+
const started = Date.now()
|
|
1024
|
+
try {
|
|
1025
|
+
await Promise.race([this.pool.query("SELECT 1"), this.timeoutPromise()])
|
|
1026
|
+
return this.build(
|
|
1027
|
+
{
|
|
1028
|
+
latency: Date.now() - started,
|
|
1029
|
+
},
|
|
1030
|
+
null,
|
|
1031
|
+
)
|
|
1032
|
+
} catch (err) {
|
|
1033
|
+
return this.build(
|
|
1034
|
+
{
|
|
1035
|
+
latency: Date.now() - started,
|
|
1036
|
+
},
|
|
1037
|
+
err,
|
|
1038
|
+
)
|
|
1028
1039
|
}
|
|
1029
|
-
return data
|
|
1030
1040
|
}
|
|
1031
|
-
}
|
|
1032
1041
|
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
promise,
|
|
1036
|
-
new Promise((resolve, reject) => {
|
|
1042
|
+
private timeoutPromise(): Promise<never> {
|
|
1043
|
+
return new Promise((_, reject) =>
|
|
1037
1044
|
setTimeout(() => {
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1045
|
+
const error: any = new Error("Health check timeout")
|
|
1046
|
+
error.code = "TIMEOUT"
|
|
1047
|
+
reject(error)
|
|
1048
|
+
}, this.timeout),
|
|
1049
|
+
)
|
|
1050
|
+
}
|
|
1042
1051
|
}
|
|
1052
|
+
|
|
1043
1053
|
export interface Formatter<T> {
|
|
1044
1054
|
format: (row: T) => string
|
|
1045
1055
|
}
|
package/tsconfig.json
CHANGED
package/lib/build.js
DELETED
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
export function param(i) {
|
|
2
|
-
return "?";
|
|
3
|
-
}
|
|
4
|
-
export function params(length, from) {
|
|
5
|
-
if (from == null) {
|
|
6
|
-
from = 0;
|
|
7
|
-
}
|
|
8
|
-
const ps = [];
|
|
9
|
-
for (let i = 1; i <= length; i++) {
|
|
10
|
-
ps.push(param(i + from));
|
|
11
|
-
}
|
|
12
|
-
return ps;
|
|
13
|
-
}
|
|
14
|
-
export function metadata(attrs) {
|
|
15
|
-
const mp = {};
|
|
16
|
-
const ks = Object.keys(attrs);
|
|
17
|
-
const ats = [];
|
|
18
|
-
const bools = [];
|
|
19
|
-
const fields = [];
|
|
20
|
-
const m = { keys: ats, fields };
|
|
21
|
-
let isMap = false;
|
|
22
|
-
for (const k of ks) {
|
|
23
|
-
const attr = attrs[k];
|
|
24
|
-
attr.name = k;
|
|
25
|
-
if (attr.key) {
|
|
26
|
-
ats.push(attr);
|
|
27
|
-
}
|
|
28
|
-
if (!attr.ignored) {
|
|
29
|
-
fields.push(k);
|
|
30
|
-
}
|
|
31
|
-
if (attr.type === "boolean") {
|
|
32
|
-
bools.push(attr);
|
|
33
|
-
}
|
|
34
|
-
if (attr.version) {
|
|
35
|
-
m.version = k;
|
|
36
|
-
}
|
|
37
|
-
const field = attr.column ? attr.column : k;
|
|
38
|
-
const s = field.toLowerCase();
|
|
39
|
-
if (s !== k) {
|
|
40
|
-
mp[s] = k;
|
|
41
|
-
isMap = true;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (isMap) {
|
|
45
|
-
m.map = mp;
|
|
46
|
-
}
|
|
47
|
-
if (bools.length > 0) {
|
|
48
|
-
m.bools = bools;
|
|
49
|
-
}
|
|
50
|
-
return m;
|
|
51
|
-
}
|
|
52
|
-
export function buildToSave(obj, table, attrs, ver, buildParam, i) {
|
|
53
|
-
if (!i) {
|
|
54
|
-
i = 1;
|
|
55
|
-
}
|
|
56
|
-
if (!buildParam) {
|
|
57
|
-
buildParam = param;
|
|
58
|
-
}
|
|
59
|
-
const ks = Object.keys(attrs);
|
|
60
|
-
const pks = [];
|
|
61
|
-
const cols = [];
|
|
62
|
-
const values = [];
|
|
63
|
-
const args = [];
|
|
64
|
-
let isVersion = false;
|
|
65
|
-
const o = obj;
|
|
66
|
-
for (const k of ks) {
|
|
67
|
-
const attr = attrs[k];
|
|
68
|
-
attr.name = k;
|
|
69
|
-
if (attr.key) {
|
|
70
|
-
pks.push(attr);
|
|
71
|
-
}
|
|
72
|
-
let v = o[k];
|
|
73
|
-
if (v == null) {
|
|
74
|
-
v = attr.default;
|
|
75
|
-
}
|
|
76
|
-
if (v !== undefined && v != null && !attr.ignored && !attr.noinsert) {
|
|
77
|
-
const field = attr.column ? attr.column : k;
|
|
78
|
-
cols.push(field);
|
|
79
|
-
if (k === ver) {
|
|
80
|
-
isVersion = true;
|
|
81
|
-
values.push(`${1}`);
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
if (v === "") {
|
|
85
|
-
values.push(`''`);
|
|
86
|
-
}
|
|
87
|
-
else if (typeof v === "number") {
|
|
88
|
-
values.push(toString(v));
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
const p = buildParam(i++);
|
|
92
|
-
values.push(p);
|
|
93
|
-
if (typeof v === "boolean") {
|
|
94
|
-
if (v === true) {
|
|
95
|
-
const v2 = attr.true ? attr.true : `'1'`;
|
|
96
|
-
args.push(v2);
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
const v2 = attr.false ? attr.false : `'0'`;
|
|
100
|
-
args.push(v2);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
args.push(v);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (!isVersion && ver && ver.length > 0) {
|
|
111
|
-
const attr = attrs[ver];
|
|
112
|
-
const field = attr.column ? attr.column : ver;
|
|
113
|
-
cols.push(field);
|
|
114
|
-
values.push(`${1}`);
|
|
115
|
-
}
|
|
116
|
-
if (pks.length === 0) {
|
|
117
|
-
if (cols.length === 0) {
|
|
118
|
-
return { query: "", params: args };
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
122
|
-
return { query: q, params: args };
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
const colSet = [];
|
|
127
|
-
for (const k of ks) {
|
|
128
|
-
const v = o[k];
|
|
129
|
-
if (v !== undefined) {
|
|
130
|
-
const attr = attrs[k];
|
|
131
|
-
if (attr && !attr.key && !attr.ignored && !attr.noupdate) {
|
|
132
|
-
const field = attr.column ? attr.column : k;
|
|
133
|
-
let x;
|
|
134
|
-
if (v === null) {
|
|
135
|
-
x = "null";
|
|
136
|
-
}
|
|
137
|
-
else if (v === "") {
|
|
138
|
-
x = `''`;
|
|
139
|
-
}
|
|
140
|
-
else if (typeof v === "number") {
|
|
141
|
-
x = toString(v);
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
x = buildParam(i++);
|
|
145
|
-
if (typeof v === "boolean") {
|
|
146
|
-
if (v === true) {
|
|
147
|
-
const v2 = attr.true ? "" + attr.true : `'1'`;
|
|
148
|
-
args.push(v2);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
const v2 = attr.false ? "" + attr.false : `'0'`;
|
|
152
|
-
args.push(v2);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
args.push(v);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
colSet.push(`${field}=${x}`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
if (colSet.length === 0) {
|
|
164
|
-
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")})`;
|
|
165
|
-
return { query: q, params: args };
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")}`;
|
|
169
|
-
return { query: q, params: args };
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
export function buildToSaveBatch(objs, table, attrs, ver, buildParam) {
|
|
174
|
-
if (!buildParam) {
|
|
175
|
-
buildParam = param;
|
|
176
|
-
}
|
|
177
|
-
const sts = [];
|
|
178
|
-
const ks = Object.keys(attrs);
|
|
179
|
-
for (const obj of objs) {
|
|
180
|
-
let i = 1;
|
|
181
|
-
const cols = [];
|
|
182
|
-
const values = [];
|
|
183
|
-
const args = [];
|
|
184
|
-
let isVersion = false;
|
|
185
|
-
const o = obj;
|
|
186
|
-
for (const k of ks) {
|
|
187
|
-
const attr = attrs[k];
|
|
188
|
-
let v = o[k];
|
|
189
|
-
if (v == null) {
|
|
190
|
-
v = attr.default;
|
|
191
|
-
}
|
|
192
|
-
if (v != null && v !== undefined && !attr.ignored && !attr.noinsert) {
|
|
193
|
-
const field = attr.column ? attr.column : k;
|
|
194
|
-
cols.push(field);
|
|
195
|
-
if (k === ver) {
|
|
196
|
-
isVersion = true;
|
|
197
|
-
values.push(`${1}`);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
if (v === "") {
|
|
201
|
-
values.push(`''`);
|
|
202
|
-
}
|
|
203
|
-
else if (typeof v === "number") {
|
|
204
|
-
values.push(toString(v));
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
const p = buildParam(i++);
|
|
208
|
-
values.push(p);
|
|
209
|
-
if (typeof v === "boolean") {
|
|
210
|
-
if (v === true) {
|
|
211
|
-
const v2 = attr.true ? attr.true : `'1'`;
|
|
212
|
-
args.push(v2);
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
const v2 = attr.false ? attr.false : `'0'`;
|
|
216
|
-
args.push(v2);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
args.push(v);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
if (!isVersion && ver && ver.length > 0) {
|
|
227
|
-
const attr = attrs[ver];
|
|
228
|
-
const field = attr.column ? attr.column : ver;
|
|
229
|
-
cols.push(field);
|
|
230
|
-
values.push(`${1}`);
|
|
231
|
-
}
|
|
232
|
-
const colSet = [];
|
|
233
|
-
for (const k of ks) {
|
|
234
|
-
const v = o[k];
|
|
235
|
-
if (v !== undefined) {
|
|
236
|
-
const attr = attrs[k];
|
|
237
|
-
if (attr && !attr.key && !attr.ignored && k !== ver && !attr.noupdate) {
|
|
238
|
-
const field = attr.column ? attr.column : k;
|
|
239
|
-
let x;
|
|
240
|
-
if (v === null) {
|
|
241
|
-
x = "null";
|
|
242
|
-
}
|
|
243
|
-
else if (v === "") {
|
|
244
|
-
x = `''`;
|
|
245
|
-
}
|
|
246
|
-
else if (typeof v === "number") {
|
|
247
|
-
x = toString(v);
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
x = buildParam(i++);
|
|
251
|
-
if (typeof v === "boolean") {
|
|
252
|
-
if (v === true) {
|
|
253
|
-
const v2 = attr.true ? "" + attr.true : `'1'`;
|
|
254
|
-
args.push(v2);
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
const v2 = attr.false ? "" + attr.false : `'0'`;
|
|
258
|
-
args.push(v2);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
args.push(v);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
colSet.push(`${field}=${x}`);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
if (colSet.length === 0) {
|
|
270
|
-
const q = `insert ignore into ${table}(${cols.join(",")})values(${values.join(",")});`;
|
|
271
|
-
const smt = { query: q, params: args };
|
|
272
|
-
sts.push(smt);
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
const q = `insert into ${table}(${cols.join(",")})values(${values.join(",")}) on duplicate key update ${colSet.join(",")};`;
|
|
276
|
-
const smt = { query: q, params: args };
|
|
277
|
-
sts.push(smt);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return sts;
|
|
281
|
-
}
|
|
282
|
-
const n = "NaN";
|
|
283
|
-
export function toString(v) {
|
|
284
|
-
let x = "" + v;
|
|
285
|
-
if (x === n) {
|
|
286
|
-
x = "null";
|
|
287
|
-
}
|
|
288
|
-
return x;
|
|
289
|
-
}
|