mysql2-core 0.3.6 → 0.3.8

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 CHANGED
@@ -93,10 +93,10 @@ without hiding SQL from developers.
93
93
  Executor Interface
94
94
  │
95
95
  ▼
96
- MySQL Adapter
96
+ MySQL Adapter
97
97
  ┌──────────────┼──────────────┐
98
98
  ▼ ▼ ▼
99
- Connection Transaction Stream
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
- query:
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
- query:
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
- # License
548
+ # Contributing
599
549
 
600
- MIT License
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
- # Contributing
554
+ ---
605
555
 
606
- Contributions are welcome!
556
+ # License
607
557
 
608
- Feel free to submit issues, feature requests, or pull requests to improve the project.
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 ? timeout : 4200;
1278
- this.service = service ? service : "mysql";
1279
- this.check = this.check.bind(this);
1280
- this.name = this.name.bind(this);
1281
- this.build = this.build.bind(this);
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 obj = {};
1286
- const promise = new Promise((resolve, reject) => {
1287
- this.pool.query("select current_time", (err, result) => {
1288
- if (err) {
1289
- return reject(err);
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
- else {
1300
- return promise;
1300
+ catch (err) {
1301
+ return this.build({
1302
+ latency: Date.now() - started,
1303
+ }, err);
1301
1304
  }
1302
1305
  });
1303
1306
  }
1304
- name() {
1305
- return this.service;
1306
- }
1307
- build(data, err) {
1308
- if (err) {
1309
- if (!data) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysql2-core",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "mysql2-core",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Connection, createPool as createPool2, FieldPacket, Pool, PoolConnection, ResultSetHeader } from "mysql2"
2
+ import { Pool as PromisePool } from "mysql2/promise"
2
3
  import { buildToSave, buildToSaveBatch } from "./build"
3
4
  import { Attribute, Attributes, DB, Statement, StringMap, Transaction } from "./metadata"
4
5
 
@@ -985,61 +986,71 @@ export interface AnyMap {
985
986
  [key: string]: any
986
987
  }
987
988
  // tslint:disable-next-line:max-classes-per-file
988
- export class MySQLChecker {
989
- timeout: number
990
- service: string
989
+ export interface HealthChecker {
990
+ name(): string
991
+ build(data: AnyMap, error: any): AnyMap
992
+ check(): Promise<AnyMap>
993
+ }
994
+ export class MySQLChecker implements HealthChecker {
995
+ protected readonly service: string
991
996
  constructor(
992
- private pool: Pool,
997
+ protected readonly pool: PromisePool,
993
998
  service?: string,
994
- timeout?: number,
999
+ protected readonly timeout = 4500,
995
1000
  ) {
996
- this.timeout = timeout ? timeout : 4200
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
- }
1001
+ this.service = service || "mysql"
1018
1002
  }
1003
+
1019
1004
  name(): string {
1020
1005
  return this.service
1021
1006
  }
1022
- build(data: AnyMap, err: any): AnyMap {
1023
- if (err) {
1024
- if (!data) {
1025
- data = {} as AnyMap
1026
- }
1027
- data["error"] = err
1007
+
1008
+ build(data: AnyMap, error: any): AnyMap {
1009
+ return {
1010
+ name: this.name(),
1011
+ healthy: error == null,
1012
+ timestamp: new Date().toISOString(),
1013
+ ...data,
1014
+ ...(error && {
1015
+ error: {
1016
+ message: error.message,
1017
+ code: error.code,
1018
+ },
1019
+ }),
1020
+ }
1021
+ }
1022
+
1023
+ async check(): Promise<AnyMap> {
1024
+ const started = Date.now()
1025
+ try {
1026
+ await Promise.race([this.pool.query("SELECT 1"), this.timeoutPromise()])
1027
+ return this.build(
1028
+ {
1029
+ latency: Date.now() - started,
1030
+ },
1031
+ null,
1032
+ )
1033
+ } catch (err) {
1034
+ return this.build(
1035
+ {
1036
+ latency: Date.now() - started,
1037
+ },
1038
+ err,
1039
+ )
1028
1040
  }
1029
- return data
1030
1041
  }
1031
- }
1032
1042
 
1033
- function promiseTimeOut(timeoutInMilliseconds: number, promise: Promise<any>): Promise<any> {
1034
- return Promise.race([
1035
- promise,
1036
- new Promise((resolve, reject) => {
1043
+ private timeoutPromise(): Promise<never> {
1044
+ return new Promise((_, reject) =>
1037
1045
  setTimeout(() => {
1038
- reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`)
1039
- }, timeoutInMilliseconds)
1040
- }),
1041
- ])
1046
+ const error: any = new Error("Health check timeout")
1047
+ error.code = "TIMEOUT"
1048
+ reject(error)
1049
+ }, this.timeout),
1050
+ )
1051
+ }
1042
1052
  }
1053
+
1043
1054
  export interface Formatter<T> {
1044
1055
  format: (row: T) => string
1045
1056
  }
package/tsconfig.json CHANGED
@@ -13,7 +13,9 @@
13
13
  "experimentalDecorators": false,
14
14
  "removeComments": true,
15
15
  "lib": [
16
- "es6"
16
+ "es6",
17
+ "ES2016",
18
+ "DOM"
17
19
  ]
18
20
  },
19
21
  "include": [