mysql2-core 0.3.6 → 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 +30 -42
- package/package.json +1 -1
- package/src/index.ts +53 -43
- package/tsconfig.json +3 -1
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
|
@@ -1272,58 +1272,46 @@ export class MySQLBatchWriter {
|
|
|
1272
1272
|
}
|
|
1273
1273
|
}
|
|
1274
1274
|
export class MySQLChecker {
|
|
1275
|
-
constructor(pool, service, timeout) {
|
|
1275
|
+
constructor(pool, service, timeout = 4500) {
|
|
1276
1276
|
this.pool = pool;
|
|
1277
|
-
this.timeout = timeout
|
|
1278
|
-
this.service = service
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
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
|
+
}));
|
|
1282
1290
|
}
|
|
1283
1291
|
check() {
|
|
1284
1292
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1285
|
-
const
|
|
1286
|
-
|
|
1287
|
-
this.pool.query("
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
else {
|
|
1292
|
-
resolve(obj);
|
|
1293
|
-
}
|
|
1294
|
-
});
|
|
1295
|
-
});
|
|
1296
|
-
if (this.timeout > 0) {
|
|
1297
|
-
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);
|
|
1298
1299
|
}
|
|
1299
|
-
|
|
1300
|
-
return
|
|
1300
|
+
catch (err) {
|
|
1301
|
+
return this.build({
|
|
1302
|
+
latency: Date.now() - started,
|
|
1303
|
+
}, err);
|
|
1301
1304
|
}
|
|
1302
1305
|
});
|
|
1303
1306
|
}
|
|
1304
|
-
|
|
1305
|
-
return
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
data = {};
|
|
1311
|
-
}
|
|
1312
|
-
data["error"] = err;
|
|
1313
|
-
}
|
|
1314
|
-
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));
|
|
1315
1313
|
}
|
|
1316
1314
|
}
|
|
1317
|
-
function promiseTimeOut(timeoutInMilliseconds, promise) {
|
|
1318
|
-
return Promise.race([
|
|
1319
|
-
promise,
|
|
1320
|
-
new Promise((resolve, reject) => {
|
|
1321
|
-
setTimeout(() => {
|
|
1322
|
-
reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`);
|
|
1323
|
-
}, timeoutInMilliseconds);
|
|
1324
|
-
}),
|
|
1325
|
-
]);
|
|
1326
|
-
}
|
|
1327
1315
|
export class Exporter {
|
|
1328
1316
|
constructor(connection, buildQuery, ft, write, end, attributes) {
|
|
1329
1317
|
this.connection = connection;
|
package/package.json
CHANGED
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
|
}
|