@travetto/model-mysql 5.0.0-rc.0 → 5.0.0-rc.10

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
@@ -42,11 +42,12 @@ export class Init {
42
42
  }
43
43
  ```
44
44
 
45
- where the [SQLModelConfig](https://github.com/travetto/travetto/tree/main/module/model-sql/src/config.ts#L7) is defined by:
45
+ where the [SQLModelConfig](https://github.com/travetto/travetto/tree/main/module/model-sql/src/config.ts#L8) is defined by:
46
46
 
47
47
  **Code: Structure of SQLModelConfig**
48
48
  ```typescript
49
49
  import { Config } from '@travetto/config';
50
+ import { asFull } from '@travetto/runtime';
50
51
 
51
52
  /**
52
53
  * SQL Model Config
@@ -88,8 +89,7 @@ export class SQLModelConfig<T extends {} = {}> {
88
89
  /**
89
90
  * Raw client options
90
91
  */
91
-
92
- options: T = {} as T;
92
+ options: T = asFull({});
93
93
  }
94
94
  ```
95
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mysql",
3
- "version": "5.0.0-rc.0",
3
+ "version": "5.0.0-rc.10",
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.0",
31
- "@travetto/context": "^5.0.0-rc.0",
32
- "@travetto/model": "^5.0.0-rc.0",
33
- "@travetto/model-query": "^5.0.0-rc.0",
34
- "@travetto/model-sql": "^5.0.0-rc.0",
30
+ "@travetto/config": "^5.0.0-rc.10",
31
+ "@travetto/context": "^5.0.0-rc.9",
32
+ "@travetto/model": "^5.0.0-rc.10",
33
+ "@travetto/model-query": "^5.0.0-rc.10",
34
+ "@travetto/model-sql": "^5.0.0-rc.10",
35
35
  "mysql2": "^3.10.2"
36
36
  },
37
37
  "peerDependencies": {
38
- "@travetto/command": "^5.0.0-rc.0"
38
+ "@travetto/command": "^5.0.0-rc.9"
39
39
  },
40
40
  "peerDependenciesMeta": {
41
41
  "@travetto/command": {
package/src/connection.ts CHANGED
@@ -1,9 +1,10 @@
1
- import mysql, { OkPacket, ResultSetHeader } from 'mysql2';
1
+ import { createPool } from 'mysql2';
2
2
 
3
- import { ShutdownManager } from '@travetto/base';
3
+ import { castTo, 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<mysql.PoolConnection> {
18
+ export class MySQLConnection extends Connection<PoolConnection> {
18
19
 
19
- #pool: mysql.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 = mysql.createPool({
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(() => new Promise<void>(r => this.#pool.end(() => r())), this);
44
+ ShutdownManager.onGracefulShutdown(() => this.#pool.end(), this);
44
45
  }
45
46
 
46
47
  /**
@@ -58,39 +59,34 @@ export class MySQLConnection extends Connection<mysql.PoolConnection> {
58
59
  return res;
59
60
  }
60
61
 
61
- async execute<T = unknown>(conn: mysql.Connection, query: string): Promise<{ count: number, records: T[] }> {
62
- return new Promise<{ count: number, records: T[] }>((res, rej) => {
63
- console.debug('Executing Query', { query });
64
- conn.query(query, (err, results, fields) => {
65
- if (err) {
66
- console.debug('Failed query', { error: err, query });
67
- if (err.message.startsWith('Duplicate entry')) {
68
- rej(new ExistsError('query', query));
69
- } else {
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
+ const records: T[] = [...results].map(v => castTo({ ...v }));
73
+ return { records, count: records.length };
74
+ }
75
+ } catch (err) {
76
+ console.debug('Failed query', { error: err, query });
77
+ if (err instanceof Error && err.message.startsWith('Duplicate entry')) {
78
+ throw new ExistsError('query', query);
79
+ } else {
80
+ throw err;
81
+ }
82
+ }
86
83
  }
87
84
 
88
- acquire(): Promise<mysql.PoolConnection> {
89
- return new Promise<mysql.PoolConnection>((res, rej) =>
90
- this.#pool.getConnection((err, conn) => err ? rej(err) : res(conn)));
85
+ acquire(): Promise<PoolConnection> {
86
+ return this.#pool.getConnection();
91
87
  }
92
88
 
93
- release(conn: mysql.PoolConnection): void {
89
+ release(conn: PoolConnection): void {
94
90
  conn.release();
95
91
  }
96
92
  }
package/src/dialect.ts CHANGED
@@ -2,7 +2,7 @@ import { FieldConfig } from '@travetto/schema';
2
2
  import { Injectable } from '@travetto/di';
3
3
  import { AsyncContext } from '@travetto/context';
4
4
  import { WhereClause } from '@travetto/model-query';
5
- import { Class } from '@travetto/base';
5
+ import { castTo, Class } from '@travetto/runtime';
6
6
  import { ModelType } from '@travetto/model';
7
7
  import { SQLModelConfig, SQLDialect } from '@travetto/model-sql';
8
8
  import { VisitStack } from '@travetto/model-sql/src/internal/util';
@@ -80,8 +80,7 @@ export class MySQLDialect extends SQLDialect {
80
80
  * Define column modification
81
81
  */
82
82
  getModifyColumnSQL(stack: VisitStack[]): string {
83
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
84
- const field = stack[stack.length - 1] as FieldConfig;
83
+ const field: FieldConfig = castTo(stack[stack.length - 1]);
85
84
  return `ALTER TABLE ${this.parentTable(stack)} MODIFY COLUMN ${this.getColumnDefinition(field)};`;
86
85
  }
87
86