@travetto/model-sqlite 4.0.0-rc.3 → 4.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 +6 -6
- package/src/connection.ts +10 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sqlite",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.5",
|
|
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": "^4.0.0-rc.
|
|
30
|
-
"@travetto/context": "^4.0.0-rc.
|
|
31
|
-
"@travetto/model": "^4.0.0-rc.
|
|
32
|
-
"@travetto/model-query": "^4.0.0-rc.
|
|
33
|
-
"@travetto/model-sql": "^4.0.0-rc.
|
|
29
|
+
"@travetto/config": "^4.0.0-rc.5",
|
|
30
|
+
"@travetto/context": "^4.0.0-rc.5",
|
|
31
|
+
"@travetto/model": "^4.0.0-rc.5",
|
|
32
|
+
"@travetto/model-query": "^4.0.0-rc.5",
|
|
33
|
+
"@travetto/model-sql": "^4.0.0-rc.5",
|
|
34
34
|
"@types/better-sqlite3": "^7.6.9",
|
|
35
35
|
"better-sqlite3": "^9.4.0"
|
|
36
36
|
},
|
package/src/connection.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import timers from 'node:timers/promises';
|
|
3
|
-
import sqlDb,
|
|
4
|
-
import
|
|
3
|
+
import sqlDb, { type Database, Options } from 'better-sqlite3';
|
|
4
|
+
import { Pool, createPool } from 'generic-pool';
|
|
5
5
|
|
|
6
6
|
import { RuntimeContext, path } from '@travetto/manifest';
|
|
7
7
|
import { ShutdownManager } from '@travetto/base';
|
|
@@ -12,16 +12,16 @@ import { SQLModelConfig, Connection } from '@travetto/model-sql';
|
|
|
12
12
|
/**
|
|
13
13
|
* Connection support for Sqlite
|
|
14
14
|
*/
|
|
15
|
-
export class SqliteConnection extends Connection<
|
|
15
|
+
export class SqliteConnection extends Connection<Database> {
|
|
16
16
|
|
|
17
17
|
isolatedTransactions = false;
|
|
18
18
|
|
|
19
|
-
#config: SQLModelConfig<
|
|
20
|
-
#pool:
|
|
19
|
+
#config: SQLModelConfig<Options & { file?: string }>;
|
|
20
|
+
#pool: Pool<Database>;
|
|
21
21
|
|
|
22
22
|
constructor(
|
|
23
23
|
context: AsyncContext,
|
|
24
|
-
config: SQLModelConfig<
|
|
24
|
+
config: SQLModelConfig<Options & { file?: string }>
|
|
25
25
|
) {
|
|
26
26
|
super(context);
|
|
27
27
|
this.#config = config;
|
|
@@ -43,7 +43,7 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
async #create(): Promise<
|
|
46
|
+
async #create(): Promise<Database> {
|
|
47
47
|
const file = path.resolve(this.#config.options.file ?? RuntimeContext.toolPath('@', 'sqlite_db'));
|
|
48
48
|
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
49
49
|
const db = new sqlDb(file, this.#config.options);
|
|
@@ -60,7 +60,7 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
|
|
|
60
60
|
override async init(): Promise<void> {
|
|
61
61
|
await this.#create();
|
|
62
62
|
|
|
63
|
-
this.#pool =
|
|
63
|
+
this.#pool = createPool<Database>({
|
|
64
64
|
create: () => this.#withRetries(() => this.#create()),
|
|
65
65
|
destroy: async db => { db.close(); }
|
|
66
66
|
}, { max: 1 });
|
|
@@ -69,7 +69,7 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
|
|
|
69
69
|
ShutdownManager.onGracefulShutdown(() => this.#pool.clear(), this);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
async execute<T = unknown>(conn:
|
|
72
|
+
async execute<T = unknown>(conn: Database, query: string): Promise<{ count: number, records: T[] }> {
|
|
73
73
|
return this.#withRetries(async () => {
|
|
74
74
|
console.debug('Executing query', { query });
|
|
75
75
|
try {
|
|
@@ -95,7 +95,7 @@ export class SqliteConnection extends Connection<sqlite3.Database> {
|
|
|
95
95
|
return await this.#pool.acquire();
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
async release(db:
|
|
98
|
+
async release(db: Database): Promise<void> {
|
|
99
99
|
return this.#pool.release(db);
|
|
100
100
|
}
|
|
101
101
|
}
|