@travetto/model-sqlite 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 +6 -6
- package/src/connection.ts +11 -11
- package/src/dialect.ts +4 -4
package/README.md
CHANGED
|
@@ -37,8 +37,8 @@ import { SqliteDialect } from '@travetto/model-sqlite';
|
|
|
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 SqliteDialect(ctx, config));
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sqlite",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.2",
|
|
4
4
|
"description": "SQLite backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sql",
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
"directory": "module/model-sqlite"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@travetto/config": "^7.0.0-rc.
|
|
30
|
-
"@travetto/context": "^7.0.0-rc.
|
|
31
|
-
"@travetto/model": "^7.0.0-rc.
|
|
32
|
-
"@travetto/model-query": "^7.0.0-rc.
|
|
33
|
-
"@travetto/model-sql": "^7.0.0-rc.
|
|
29
|
+
"@travetto/config": "^7.0.0-rc.2",
|
|
30
|
+
"@travetto/context": "^7.0.0-rc.2",
|
|
31
|
+
"@travetto/model": "^7.0.0-rc.2",
|
|
32
|
+
"@travetto/model-query": "^7.0.0-rc.2",
|
|
33
|
+
"@travetto/model-sql": "^7.0.0-rc.2",
|
|
34
34
|
"@types/better-sqlite3": "^7.6.13",
|
|
35
35
|
"better-sqlite3": "^12.4.6"
|
|
36
36
|
},
|
package/src/connection.ts
CHANGED
|
@@ -27,17 +27,17 @@ export class SqliteConnection extends Connection<Database> {
|
|
|
27
27
|
this.#config = config;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
async #withRetries<T>(
|
|
30
|
+
async #withRetries<T>(operation: () => Promise<T>, retries = 10, delay = 250): Promise<T> {
|
|
31
31
|
for (; ;) {
|
|
32
32
|
try {
|
|
33
|
-
return await
|
|
34
|
-
} catch (
|
|
35
|
-
if (
|
|
33
|
+
return await operation();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error instanceof Error && retries > 1 && error.message.includes('database is locked')) {
|
|
36
36
|
console.error('Failed, and waiting', retries);
|
|
37
37
|
await Util.blockingTimeout(delay);
|
|
38
38
|
retries -= 1;
|
|
39
39
|
} else {
|
|
40
|
-
throw
|
|
40
|
+
throw error;
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -72,22 +72,22 @@ export class SqliteConnection extends Connection<Database> {
|
|
|
72
72
|
ShutdownManager.onGracefulShutdown(() => this.#pool.clear());
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
async execute<T = unknown>(
|
|
75
|
+
async execute<T = unknown>(connection: Database, query: string, values?: unknown[]): Promise<{ count: number, records: T[] }> {
|
|
76
76
|
return this.#withRetries(async () => {
|
|
77
77
|
console.debug('Executing query', { query });
|
|
78
78
|
try {
|
|
79
|
-
const out = await
|
|
79
|
+
const out = await connection.prepare<unknown[], T>(query)[query.trim().startsWith('SELECT') ? 'all' : 'run'](...values ?? []);
|
|
80
80
|
if (Array.isArray(out)) {
|
|
81
|
-
const records: T[] = out.map(
|
|
81
|
+
const records: T[] = out.map(item => ({ ...item }));
|
|
82
82
|
return { count: out.length, records };
|
|
83
83
|
} else {
|
|
84
84
|
return { count: out.changes, records: [] };
|
|
85
85
|
}
|
|
86
|
-
} catch (
|
|
87
|
-
if (
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (error instanceof Error && error.message.includes('UNIQUE constraint failed')) {
|
|
88
88
|
throw new ExistsError('query', query);
|
|
89
89
|
} else {
|
|
90
|
-
throw
|
|
90
|
+
throw error;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
});
|
package/src/dialect.ts
CHANGED
|
@@ -14,12 +14,12 @@ import { SqliteConnection } from './connection.ts';
|
|
|
14
14
|
@Injectable()
|
|
15
15
|
export class SqliteDialect extends SQLDialect {
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
connection: SqliteConnection;
|
|
18
18
|
config: SQLModelConfig;
|
|
19
19
|
|
|
20
20
|
constructor(context: AsyncContext, config: SQLModelConfig) {
|
|
21
21
|
super(config.namespace);
|
|
22
|
-
this.
|
|
22
|
+
this.connection = new SqliteConnection(context, config);
|
|
23
23
|
this.config = config;
|
|
24
24
|
|
|
25
25
|
// Special operators
|
|
@@ -52,8 +52,8 @@ export class SqliteDialect extends SQLDialect {
|
|
|
52
52
|
getModifyColumnSQL(stack: VisitStack[]): string {
|
|
53
53
|
const field: SchemaFieldConfig = castTo(stack.at(-1));
|
|
54
54
|
const type = this.getColumnType(field);
|
|
55
|
-
const
|
|
56
|
-
return `ALTER TABLE ${this.parentTable(stack)} ALTER COLUMN ${
|
|
55
|
+
const identifier = this.identifier(field.name);
|
|
56
|
+
return `ALTER TABLE ${this.parentTable(stack)} ALTER COLUMN ${identifier} TYPE ${type} USING (${identifier}::${type});`;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|