@travetto/model-mysql 7.0.0-rc.1 → 7.0.0-rc.2
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 +2 -2
- package/package.json +7 -7
- package/src/connection.ts +13 -13
- package/src/dialect.ts +4 -4
package/README.md
CHANGED
|
@@ -37,8 +37,8 @@ import { MySQLDialect } from '@travetto/model-mysql';
|
|
|
37
37
|
|
|
38
38
|
export class Init {
|
|
39
39
|
@InjectableFactory({ primary: true })
|
|
40
|
-
static getModelService(ctx: AsyncContext,
|
|
41
|
-
return new SQLModelService(ctx,
|
|
40
|
+
static getModelService(ctx: AsyncContext, config: SQLModelConfig) {
|
|
41
|
+
return new SQLModelService(ctx, config, new MySQLDialect(ctx, config));
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-mysql",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.2",
|
|
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,12 +27,12 @@
|
|
|
27
27
|
"directory": "module/model-mysql"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/cli": "^7.0.0-rc.
|
|
31
|
-
"@travetto/config": "^7.0.0-rc.
|
|
32
|
-
"@travetto/context": "^7.0.0-rc.
|
|
33
|
-
"@travetto/model": "^7.0.0-rc.
|
|
34
|
-
"@travetto/model-query": "^7.0.0-rc.
|
|
35
|
-
"@travetto/model-sql": "^7.0.0-rc.
|
|
30
|
+
"@travetto/cli": "^7.0.0-rc.2",
|
|
31
|
+
"@travetto/config": "^7.0.0-rc.2",
|
|
32
|
+
"@travetto/context": "^7.0.0-rc.2",
|
|
33
|
+
"@travetto/model": "^7.0.0-rc.2",
|
|
34
|
+
"@travetto/model-query": "^7.0.0-rc.2",
|
|
35
|
+
"@travetto/model-sql": "^7.0.0-rc.2",
|
|
36
36
|
"mysql2": "^3.15.3"
|
|
37
37
|
},
|
|
38
38
|
"travetto": {
|
package/src/connection.ts
CHANGED
|
@@ -6,9 +6,9 @@ import { AsyncContext } from '@travetto/context';
|
|
|
6
6
|
import { ExistsError } from '@travetto/model';
|
|
7
7
|
import { Connection, SQLModelConfig } from '@travetto/model-sql';
|
|
8
8
|
|
|
9
|
-
function isSimplePacket(
|
|
10
|
-
return
|
|
11
|
-
|
|
9
|
+
function isSimplePacket(value: unknown): value is OkPacket | ResultSetHeader {
|
|
10
|
+
return value !== null && value !== undefined && typeof value === 'object' && 'constructor' in value && (
|
|
11
|
+
value.constructor.name === 'OkPacket' || value.constructor.name === 'ResultSetHeader'
|
|
12
12
|
);
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -59,27 +59,27 @@ export class MySQLConnection extends Connection<PoolConnection> {
|
|
|
59
59
|
return result;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
async execute<T = unknown>(
|
|
62
|
+
async execute<T = unknown>(pool: PoolConnection, query: string, values?: unknown[]): Promise<{ count: number, records: T[] }> {
|
|
63
63
|
console.debug('Executing query', { query });
|
|
64
64
|
let prepared;
|
|
65
65
|
try {
|
|
66
|
-
prepared = (values?.length ?? 0) > 0 ? await
|
|
67
|
-
const [results,] = await (prepared ? prepared.execute(values) :
|
|
66
|
+
prepared = (values?.length ?? 0) > 0 ? await pool.prepare(query) : undefined;
|
|
67
|
+
const [results,] = await (prepared ? prepared.execute(values) : pool.query(query));
|
|
68
68
|
if (isSimplePacket(results)) {
|
|
69
69
|
return { records: [], count: results.affectedRows };
|
|
70
70
|
} else {
|
|
71
71
|
if (isSimplePacket(results[0])) {
|
|
72
72
|
return { records: [], count: results[0].affectedRows };
|
|
73
73
|
}
|
|
74
|
-
const records: T[] = [...results].map(
|
|
74
|
+
const records: T[] = [...results].map(value => castTo({ ...value }));
|
|
75
75
|
return { records, count: records.length };
|
|
76
76
|
}
|
|
77
|
-
} catch (
|
|
78
|
-
console.debug('Failed query', { error
|
|
79
|
-
if (
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.debug('Failed query', { error, query });
|
|
79
|
+
if (error instanceof Error && error.message.startsWith('Duplicate entry')) {
|
|
80
80
|
throw new ExistsError('query', query);
|
|
81
81
|
} else {
|
|
82
|
-
throw
|
|
82
|
+
throw error;
|
|
83
83
|
}
|
|
84
84
|
} finally {
|
|
85
85
|
try {
|
|
@@ -92,7 +92,7 @@ export class MySQLConnection extends Connection<PoolConnection> {
|
|
|
92
92
|
return this.#pool.getConnection();
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
release(
|
|
96
|
-
|
|
95
|
+
release(pool: PoolConnection): void {
|
|
96
|
+
pool.release();
|
|
97
97
|
}
|
|
98
98
|
}
|
package/src/dialect.ts
CHANGED
|
@@ -14,12 +14,12 @@ import { MySQLConnection } from './connection.ts';
|
|
|
14
14
|
@Injectable()
|
|
15
15
|
export class MySQLDialect extends SQLDialect {
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
connection: MySQLConnection;
|
|
18
18
|
tablePostfix = 'COLLATE=utf8mb4_bin ENGINE=InnoDB';
|
|
19
19
|
|
|
20
20
|
constructor(context: AsyncContext, config: SQLModelConfig) {
|
|
21
21
|
super(config.namespace);
|
|
22
|
-
this.
|
|
22
|
+
this.connection = new MySQLConnection(context, config);
|
|
23
23
|
|
|
24
24
|
// Custom types
|
|
25
25
|
Object.assign(this.COLUMN_TYPES, {
|
|
@@ -31,9 +31,9 @@ export class MySQLDialect extends SQLDialect {
|
|
|
31
31
|
* Set string length limit based on version
|
|
32
32
|
*/
|
|
33
33
|
if (/^5[.][56]/.test(config.version)) {
|
|
34
|
-
this.
|
|
34
|
+
this.DEFAULT_STRING_LENGTH = 191; // Mysql limitation with utf8 and keys
|
|
35
35
|
} else {
|
|
36
|
-
this.
|
|
36
|
+
this.DEFAULT_STRING_LENGTH = 3072 / 4 - 1;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
if (/^5[.].*/.test(config.version)) {
|