@travetto/model-mysql 5.0.0-rc.3 → 5.0.0-rc.5
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/package.json +7 -7
- package/src/connection.ts +31 -34
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-mysql",
|
|
3
|
-
"version": "5.0.0-rc.
|
|
3
|
+
"version": "5.0.0-rc.5",
|
|
4
4
|
"description": "MySQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sql",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"directory": "module/model-mysql"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/config": "^5.0.0-rc.
|
|
31
|
-
"@travetto/context": "^5.0.0-rc.
|
|
32
|
-
"@travetto/model": "^5.0.0-rc.
|
|
33
|
-
"@travetto/model-query": "^5.0.0-rc.
|
|
34
|
-
"@travetto/model-sql": "^5.0.0-rc.
|
|
30
|
+
"@travetto/config": "^5.0.0-rc.5",
|
|
31
|
+
"@travetto/context": "^5.0.0-rc.5",
|
|
32
|
+
"@travetto/model": "^5.0.0-rc.5",
|
|
33
|
+
"@travetto/model-query": "^5.0.0-rc.5",
|
|
34
|
+
"@travetto/model-sql": "^5.0.0-rc.5",
|
|
35
35
|
"mysql2": "^3.10.2"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@travetto/command": "^5.0.0-rc.
|
|
38
|
+
"@travetto/command": "^5.0.0-rc.5"
|
|
39
39
|
},
|
|
40
40
|
"peerDependenciesMeta": {
|
|
41
41
|
"@travetto/command": {
|
package/src/connection.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createPool } from 'mysql2';
|
|
2
2
|
|
|
3
3
|
import { ShutdownManager } from '@travetto/runtime';
|
|
4
4
|
import { AsyncContext } from '@travetto/context';
|
|
5
5
|
import { ExistsError } from '@travetto/model';
|
|
6
6
|
import { Connection, SQLModelConfig } from '@travetto/model-sql';
|
|
7
|
+
import { PoolConnection, Pool, OkPacket, ResultSetHeader } from 'mysql2/promise';
|
|
7
8
|
|
|
8
9
|
function isSimplePacket(o: unknown): o is OkPacket | ResultSetHeader {
|
|
9
10
|
return o !== null && o !== undefined && typeof o === 'object' && 'constructor' in o && (
|
|
@@ -14,9 +15,9 @@ function isSimplePacket(o: unknown): o is OkPacket | ResultSetHeader {
|
|
|
14
15
|
/**
|
|
15
16
|
* Connection support for mysql
|
|
16
17
|
*/
|
|
17
|
-
export class MySQLConnection extends Connection<
|
|
18
|
+
export class MySQLConnection extends Connection<PoolConnection> {
|
|
18
19
|
|
|
19
|
-
#pool:
|
|
20
|
+
#pool: Pool;
|
|
20
21
|
#config: SQLModelConfig;
|
|
21
22
|
|
|
22
23
|
constructor(
|
|
@@ -28,7 +29,7 @@ export class MySQLConnection extends Connection<mysql.PoolConnection> {
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
async init(): Promise<void> {
|
|
31
|
-
this.#pool =
|
|
32
|
+
this.#pool = createPool({
|
|
32
33
|
user: this.#config.user,
|
|
33
34
|
password: this.#config.password,
|
|
34
35
|
database: this.#config.database,
|
|
@@ -37,10 +38,10 @@ export class MySQLConnection extends Connection<mysql.PoolConnection> {
|
|
|
37
38
|
timezone: '+00:00',
|
|
38
39
|
typeCast: this.typeCast.bind(this),
|
|
39
40
|
...(this.#config.options || {})
|
|
40
|
-
});
|
|
41
|
+
}).promise();
|
|
41
42
|
|
|
42
43
|
// Close mysql
|
|
43
|
-
ShutdownManager.onGracefulShutdown(() =>
|
|
44
|
+
ShutdownManager.onGracefulShutdown(() => this.#pool.end(), this);
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
/**
|
|
@@ -58,39 +59,35 @@ export class MySQLConnection extends Connection<mysql.PoolConnection> {
|
|
|
58
59
|
return res;
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
async execute<T = unknown>(conn:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
conn.query(query
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
rej(err);
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
if (isSimplePacket(results)) {
|
|
74
|
-
return res({ records: [], count: results.affectedRows });
|
|
75
|
-
} else {
|
|
76
|
-
if (isSimplePacket(results[0])) {
|
|
77
|
-
return res({ records: [], count: results[0].affectedRows });
|
|
78
|
-
}
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
80
|
-
const records: T[] = [...results].map(v => ({ ...v }) as T);
|
|
81
|
-
return res({ records, count: records.length });
|
|
82
|
-
}
|
|
62
|
+
async execute<T = unknown>(conn: PoolConnection, query: string): Promise<{ count: number, records: T[] }> {
|
|
63
|
+
console.debug('Executing Query', { query });
|
|
64
|
+
try {
|
|
65
|
+
const [results,] = await conn.query(query);
|
|
66
|
+
if (isSimplePacket(results)) {
|
|
67
|
+
return { records: [], count: results.affectedRows };
|
|
68
|
+
} else {
|
|
69
|
+
if (isSimplePacket(results[0])) {
|
|
70
|
+
return { records: [], count: results[0].affectedRows };
|
|
83
71
|
}
|
|
84
|
-
|
|
85
|
-
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
73
|
+
const records: T[] = [...results].map(v => ({ ...v }) as T);
|
|
74
|
+
return { records, count: records.length };
|
|
75
|
+
}
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.debug('Failed query', { error: err, query });
|
|
78
|
+
if (err instanceof Error && err.message.startsWith('Duplicate entry')) {
|
|
79
|
+
throw new ExistsError('query', query);
|
|
80
|
+
} else {
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
86
84
|
}
|
|
87
85
|
|
|
88
|
-
acquire(): Promise<
|
|
89
|
-
return
|
|
90
|
-
this.#pool.getConnection((err, conn) => err ? rej(err) : res(conn)));
|
|
86
|
+
acquire(): Promise<PoolConnection> {
|
|
87
|
+
return this.#pool.getConnection();
|
|
91
88
|
}
|
|
92
89
|
|
|
93
|
-
release(conn:
|
|
90
|
+
release(conn: PoolConnection): void {
|
|
94
91
|
conn.release();
|
|
95
92
|
}
|
|
96
93
|
}
|