@technicity/data-service-generator 0.16.0 → 0.16.1-next.0

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.
@@ -1,22 +1,25 @@
1
1
  import { RedisConfig } from "./Cache";
2
2
  import type { IRuntime, TMiddleware, TResolveParams, IArtifacts, ISupplementClientOpts } from "./IRuntime";
3
+ import { MySQL } from "./lib/MySQL";
3
4
  export declare class RuntimeMySQL implements IRuntime {
4
5
  #private;
5
- constructor(clientOpts: {
6
- [k: string]: any;
7
- }, otherOpts: {
6
+ constructor(clientOpts: ConstructorParameters<typeof MySQL>[0], otherOpts: {
8
7
  supplementClientOpts?: ISupplementClientOpts;
9
8
  redis?: RedisConfig;
10
9
  }, artifacts: IArtifacts);
11
10
  resolve(input: TResolveParams): Promise<any>;
12
- $queryRaw(sql: string, values?: any[]): Promise<any>;
11
+ $queryRaw(sql: string, values?: any[]): Promise<import("mysql2").OkPacket | import("mysql2").ResultSetHeader | import("mysql2").ResultSetHeader[] | import("mysql2").RowDataPacket[] | import("mysql2").RowDataPacket[][] | import("mysql2").OkPacket[] | [import("mysql2").RowDataPacket[], import("mysql2").ResultSetHeader]>;
13
12
  $use(middleware: TMiddleware): Promise<void>;
14
13
  $whereNeedsProcessing(where: any): boolean;
15
14
  $prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
16
15
  $shutdown(): Promise<void>;
17
16
  $startTransaction(input?: {
18
17
  isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
19
- }): Promise<any>;
18
+ }): Promise<{
19
+ commit: () => Promise<void>;
20
+ rollback: () => Promise<void>;
21
+ dbCall: (q: string) => Promise<import("mysql2").OkPacket | import("mysql2").ResultSetHeader | import("mysql2").ResultSetHeader[] | import("mysql2").RowDataPacket[] | import("mysql2").RowDataPacket[][] | import("mysql2").OkPacket[] | [import("mysql2").RowDataPacket[], import("mysql2").ResultSetHeader]>;
22
+ }>;
20
23
  private dbCall;
21
24
  private formatQuery;
22
25
  }
@@ -64,13 +64,11 @@ class RuntimeMySQL {
64
64
  // if (field.type === "DATE") {
65
65
  // return field.string();
66
66
  // }
67
- // if (
68
- // field.type === "TIMESTAMP" ||
69
- // field.type === "DATE" ||
70
- // field.type === "DATETIME"
71
- // ) {
72
- // return new Date(field.string()).toISOString();
73
- // }
67
+ if (field.type === "TIMESTAMP" ||
68
+ field.type === "DATE" ||
69
+ field.type === "DATETIME") {
70
+ return new Date(field.string()).toISOString();
71
+ }
74
72
  if (field.type === "VAR_STRING" || field.type === "STRING") {
75
73
  return field.string();
76
74
  }
@@ -1,8 +1,12 @@
1
+ import * as mysql from "mysql2/promise";
1
2
  export declare class MySQL {
2
- pool: any;
3
- constructor(opts: any);
4
- query(...args: any[]): any;
5
- beginTransaction(isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE"): Promise<any>;
6
- getConnection(): Promise<any>;
3
+ pool: mysql.Pool;
4
+ constructor(opts: Parameters<typeof mysql.createPool>[0]);
5
+ query(...args: any[]): Promise<mysql.OkPacket | mysql.ResultSetHeader | mysql.ResultSetHeader[] | mysql.RowDataPacket[] | mysql.RowDataPacket[][] | mysql.OkPacket[] | [mysql.RowDataPacket[], mysql.ResultSetHeader]>;
6
+ beginTransaction(isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE"): Promise<{
7
+ commit: () => Promise<void>;
8
+ rollback: () => Promise<void>;
9
+ dbCall: (q: string) => Promise<mysql.OkPacket | mysql.ResultSetHeader | mysql.ResultSetHeader[] | mysql.RowDataPacket[] | mysql.RowDataPacket[][] | mysql.OkPacket[] | [mysql.RowDataPacket[], mysql.ResultSetHeader]>;
10
+ }>;
7
11
  endPool(): Promise<void>;
8
12
  }
@@ -1,52 +1,99 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.MySQL = void 0;
4
- const mysql = require("mysql");
5
- const P = require("bluebird");
6
- P.promisifyAll(require("mysql/lib/Connection").prototype);
7
- P.promisifyAll(require("mysql/lib/Pool").prototype);
27
+ const mysql = __importStar(require("mysql2/promise"));
8
28
  class MySQL {
9
29
  constructor(opts) {
10
30
  this.pool = mysql.createPool(opts);
31
+ if (opts.debug) {
32
+ this.pool.on("acquire", (connection) => {
33
+ console.log("Connection %d acquired", connection.threadId);
34
+ });
35
+ this.pool.on("connection", (connection) => {
36
+ console.log("Connection %d connected", connection.threadId);
37
+ });
38
+ this.pool.on("enqueue", () => {
39
+ console.log("Waiting for available connection slot");
40
+ });
41
+ this.pool.on("release", (connection) => {
42
+ console.log("Connection %d released", connection.threadId);
43
+ });
44
+ }
11
45
  }
12
- // http://bluebirdjs.com/docs/api/promise.using.html
13
46
  query(...args) {
14
- return P.using(this.getConnection(), (connection) => connection.queryAsync(...args));
47
+ return this.pool
48
+ .query(
49
+ // @ts-expect-error
50
+ ...args)
51
+ .then(([rows]) => rows);
15
52
  }
16
53
  async beginTransaction(isolationLevel) {
17
- return P.using(this.getConnection(), async (connection) => {
18
- async function handleError(err) {
19
- await connection.rollbackAsync();
20
- throw err;
54
+ const connection = await this.pool.getConnection();
55
+ // https://dev.mysql.com/doc/refman/8.0/en/set-transaction.html#set-transaction-isolation-level
56
+ // Without any SESSION or GLOBAL keyword:
57
+ // The statement applies only to the next single transaction performed within the session.
58
+ if (isolationLevel != null) {
59
+ if (isolationLevel !== "READ UNCOMMITTED" &&
60
+ isolationLevel !== "READ COMMITTED" &&
61
+ isolationLevel !== "REPEATABLE READ" &&
62
+ isolationLevel !== "SERIALIZABLE") {
63
+ throw new Error(`Invalid isolationLevel: ${isolationLevel}`);
21
64
  }
22
- // https://dev.mysql.com/doc/refman/8.0/en/set-transaction.html#set-transaction-isolation-level
23
- // Without any SESSION or GLOBAL keyword:
24
- // The statement applies only to the next single transaction performed within the session.
25
- if (isolationLevel != null) {
26
- if (isolationLevel !== "READ UNCOMMITTED" &&
27
- isolationLevel !== "READ COMMITTED" &&
28
- isolationLevel !== "REPEATABLE READ" &&
29
- isolationLevel !== "SERIALIZABLE") {
30
- throw new Error(`Invalid isolationLevel: ${isolationLevel}`);
65
+ await connection.query(`SET TRANSACTION ISOLATION LEVEL ${isolationLevel};`);
66
+ }
67
+ await connection.beginTransaction();
68
+ return {
69
+ commit: () => connection
70
+ .commit()
71
+ .catch(async (err) => {
72
+ await connection.rollback();
73
+ throw err;
74
+ })
75
+ .finally(() => {
76
+ connection.release();
77
+ }),
78
+ rollback: () => connection.rollback().finally(() => {
79
+ connection.release();
80
+ }),
81
+ dbCall: (q) => connection
82
+ .query(q)
83
+ .then(([rows]) => rows)
84
+ .catch(async (err) => {
85
+ try {
86
+ await connection.rollback();
31
87
  }
32
- await connection.queryAsync(`SET TRANSACTION ISOLATION LEVEL ${isolationLevel};`);
33
- }
34
- await connection.beginTransactionAsync();
35
- return {
36
- commit: () => connection.commitAsync().catch(handleError),
37
- rollback: () => connection.rollbackAsync(),
38
- dbCall: (q) => connection.queryAsync(q).catch(handleError)
39
- };
40
- });
41
- }
42
- // http://bluebirdjs.com/docs/api/disposer.html
43
- async getConnection() {
44
- return this.pool
45
- .getConnectionAsync()
46
- .disposer((connection) => connection.release());
88
+ finally {
89
+ connection.release();
90
+ }
91
+ throw err;
92
+ })
93
+ };
47
94
  }
48
95
  async endPool() {
49
- await this.pool.endAsync();
96
+ await this.pool.end();
50
97
  }
51
98
  }
52
99
  exports.MySQL = MySQL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@technicity/data-service-generator",
3
- "version": "0.16.0",
3
+ "version": "0.16.1-next.0",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist"
@@ -15,7 +15,6 @@
15
15
  "dependencies": {
16
16
  "@types/better-sqlite3": "^7.6.3",
17
17
  "better-sqlite3": "^8.1.0",
18
- "bluebird": "^3.7.2",
19
18
  "change-case": "^4.1.1",
20
19
  "fs-extra": "10.0.0",
21
20
  "graphql": "15.8.0",
@@ -26,7 +25,7 @@
26
25
  "lodash": "^4.17.20",
27
26
  "loglevel": "^1.8.1",
28
27
  "mssql": "^6.3.1",
29
- "mysql": "^2.18.1",
28
+ "mysql2": "^3.10.1",
30
29
  "prettier": "^2.1.2",
31
30
  "sqlstring": "^2.3.2",
32
31
  "sqlstring-sqlite": "^0.1.1",