@travetto/model-postgres 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 +11 -11
- package/src/dialect.ts +4 -4
package/README.md
CHANGED
|
@@ -37,8 +37,8 @@ import { PostgreSQLDialect } from '@travetto/model-postgres';
|
|
|
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 PostgreSQLDialect(ctx, config));
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-postgres",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.2",
|
|
4
4
|
"description": "PostgreSQL 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-postgres"
|
|
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
|
"@types/pg": "^8.15.6",
|
|
37
37
|
"pg": "^8.16.3"
|
|
38
38
|
},
|
package/src/connection.ts
CHANGED
|
@@ -40,9 +40,9 @@ export class PostgreSQLConnection extends Connection<PoolClient> {
|
|
|
40
40
|
|
|
41
41
|
await this.runWithActive(() =>
|
|
42
42
|
this.runWithTransaction('required', () =>
|
|
43
|
-
this.execute(this.active!, 'CREATE EXTENSION IF NOT EXISTS pgcrypto;').catch(
|
|
44
|
-
if (!(
|
|
45
|
-
throw
|
|
43
|
+
this.execute(this.active!, 'CREATE EXTENSION IF NOT EXISTS pgcrypto;').catch(error => {
|
|
44
|
+
if (!(error instanceof Error && error.message.includes('already exists'))) {
|
|
45
|
+
throw error;
|
|
46
46
|
}
|
|
47
47
|
})
|
|
48
48
|
)
|
|
@@ -52,17 +52,17 @@ export class PostgreSQLConnection extends Connection<PoolClient> {
|
|
|
52
52
|
ShutdownManager.onGracefulShutdown(() => this.#pool.end());
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
async execute<T = unknown>(
|
|
55
|
+
async execute<T = unknown>(pool: PoolClient, query: string, values?: unknown[]): Promise<{ count: number, records: T[] }> {
|
|
56
56
|
console.debug('Executing query', { query });
|
|
57
57
|
try {
|
|
58
|
-
const out = await
|
|
59
|
-
const records: T[] = [...out.rows].map(
|
|
58
|
+
const out = await pool.query(query, values);
|
|
59
|
+
const records: T[] = [...out.rows].map(value => ({ ...value }));
|
|
60
60
|
return { count: out.rowCount!, records };
|
|
61
|
-
} catch (
|
|
62
|
-
if (
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof Error && error.message.includes('duplicate key value')) {
|
|
63
63
|
throw new ExistsError('query', query);
|
|
64
64
|
} else {
|
|
65
|
-
throw
|
|
65
|
+
throw error;
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
}
|
|
@@ -71,7 +71,7 @@ export class PostgreSQLConnection extends Connection<PoolClient> {
|
|
|
71
71
|
return this.#pool.connect();
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
release(
|
|
75
|
-
|
|
74
|
+
release(pool: PoolClient): void {
|
|
75
|
+
pool.release();
|
|
76
76
|
}
|
|
77
77
|
}
|
package/src/dialect.ts
CHANGED
|
@@ -14,11 +14,11 @@ import { PostgreSQLConnection } from './connection.ts';
|
|
|
14
14
|
@Injectable()
|
|
15
15
|
export class PostgreSQLDialect extends SQLDialect {
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
connection: PostgreSQLConnection;
|
|
18
18
|
|
|
19
19
|
constructor(context: AsyncContext, config: SQLModelConfig) {
|
|
20
20
|
super(config.namespace);
|
|
21
|
-
this.
|
|
21
|
+
this.connection = new PostgreSQLConnection(context, config);
|
|
22
22
|
this.ID_AFFIX = '"';
|
|
23
23
|
|
|
24
24
|
// Special operators
|
|
@@ -50,8 +50,8 @@ export class PostgreSQLDialect extends SQLDialect {
|
|
|
50
50
|
getModifyColumnSQL(stack: VisitStack[]): string {
|
|
51
51
|
const field: SchemaFieldConfig = castTo(stack.at(-1));
|
|
52
52
|
const type = this.getColumnType(field);
|
|
53
|
-
const
|
|
54
|
-
return `ALTER TABLE ${this.parentTable(stack)} ALTER COLUMN ${
|
|
53
|
+
const identifier = this.identifier(field.name);
|
|
54
|
+
return `ALTER TABLE ${this.parentTable(stack)} ALTER COLUMN ${identifier} TYPE ${type} USING (${identifier}::${type});`;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|