@travetto/model-sqlite 8.0.0-alpha.24 → 8.0.0-alpha.26
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 +24 -43
- package/__index__.ts +3 -2
- package/package.json +6 -6
- package/src/config.ts +30 -0
- package/src/connection.ts +127 -66
- package/src/dialect.ts +133 -110
- package/src/service.ts +28 -0
package/README.md
CHANGED
|
@@ -13,79 +13,60 @@ npm install @travetto/model-sqlite
|
|
|
13
13
|
yarn add @travetto/model-sqlite
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
This module provides a [SQLite](https://www.sqlite.org/)-based implementation for the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module.
|
|
16
|
+
This module provides a [SQLite](https://www.sqlite.org/)-based implementation for the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module. This source allows the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module to read, write and query against [SQL](https://en.wikipedia.org/wiki/SQL) databases. In development mode, the [SqliteModelService](https://github.com/travetto/travetto/tree/main/module/model-sqlite/src/service.ts#L12) will also modify the database schema in real time to minimize impact to development.
|
|
17
17
|
|
|
18
|
-
The schema generated will not generally map to existing tables as it is attempting to produce a document store like experience on top of a [SQL](https://en.wikipedia.org/wiki/SQL) database.
|
|
18
|
+
The schema generated will not generally map to existing tables as it is attempting to produce a document store like experience on top of a [SQL](https://en.wikipedia.org/wiki/SQL) database. Every table generated maps to a model, with simple fields mapped as individual columns and complex fields/arrays mapped as serialized `TEXT` columns.
|
|
19
19
|
|
|
20
20
|
Supported features:
|
|
21
|
-
* [
|
|
22
|
-
* [
|
|
23
|
-
* [
|
|
21
|
+
* [Bulk](https://github.com/travetto/travetto/tree/main/module/model/src/types/bulk.ts#L60)
|
|
22
|
+
* [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L10)
|
|
23
|
+
* [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
|
|
24
|
+
* [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L21)
|
|
24
25
|
* [Query Crud](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/crud.ts#L11)
|
|
25
26
|
* [Facet](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/facet.ts#L14)
|
|
26
|
-
* [Query](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/query.ts#L10)
|
|
27
27
|
* [Suggest](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/suggest.ts#L12)
|
|
28
|
+
* [Query](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/query.ts#L10)
|
|
28
29
|
|
|
29
30
|
Out of the box, by installing the module, everything should be wired up by default.If you need to customize any aspect of the source or config, you can override and register it with the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module.
|
|
30
31
|
|
|
31
32
|
**Code: Wiring up a custom Model Source**
|
|
32
33
|
```typescript
|
|
33
|
-
import type { AsyncContext } from '@travetto/context';
|
|
34
34
|
import { InjectableFactory } from '@travetto/di';
|
|
35
|
-
|
|
36
|
-
import { SQLModelService, type SQLModelConfig } from '@travetto/model-sql';
|
|
37
|
-
import { SqliteDialect } from '@travetto/model-sqlite';
|
|
35
|
+
import { type SqliteConnection, SqliteModelService } from '@travetto/model-sqlite';
|
|
38
36
|
|
|
39
37
|
export class Init {
|
|
40
38
|
@InjectableFactory({ primary: true })
|
|
41
|
-
static getModelService(
|
|
42
|
-
return new
|
|
39
|
+
static getModelService(connection: SqliteConnection) {
|
|
40
|
+
return new SqliteModelService(connection);
|
|
43
41
|
}
|
|
44
42
|
}
|
|
45
43
|
```
|
|
46
44
|
|
|
47
|
-
where the [
|
|
45
|
+
where the [SqliteModelConfig](https://github.com/travetto/travetto/tree/main/module/model-sqlite/src/config.ts#L10) is defined by:
|
|
48
46
|
|
|
49
|
-
**Code: Structure of
|
|
47
|
+
**Code: Structure of SqliteModelConfig**
|
|
50
48
|
```typescript
|
|
51
|
-
@Config('model.
|
|
52
|
-
export class
|
|
53
|
-
/**
|
|
54
|
-
* Host to connect to
|
|
55
|
-
*/
|
|
56
|
-
host = '127.0.0.1';
|
|
57
|
-
/**
|
|
58
|
-
* Default port
|
|
59
|
-
*/
|
|
60
|
-
port = 0;
|
|
61
|
-
/**
|
|
62
|
-
* Username
|
|
63
|
-
*/
|
|
64
|
-
user = Runtime.production ? '' : 'travetto';
|
|
65
|
-
/**
|
|
66
|
-
* Password
|
|
67
|
-
*/
|
|
68
|
-
password = Runtime.production ? '' : 'travetto';
|
|
49
|
+
@Config('model.sqlite')
|
|
50
|
+
export class SqliteModelConfig {
|
|
69
51
|
/**
|
|
70
|
-
*
|
|
52
|
+
* Namespace/schema prefix for table names
|
|
71
53
|
*/
|
|
72
54
|
namespace = '';
|
|
55
|
+
|
|
73
56
|
/**
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
database = 'app';
|
|
77
|
-
/**
|
|
78
|
-
* Allow storage modification at runtime
|
|
57
|
+
* Allow storage modifications (like table auto-creation and schema updates) at runtime
|
|
79
58
|
*/
|
|
80
|
-
modifyStorage
|
|
59
|
+
modifyStorage = !Runtime.production;
|
|
60
|
+
|
|
81
61
|
/**
|
|
82
|
-
*
|
|
62
|
+
* SQLite file location
|
|
83
63
|
*/
|
|
84
|
-
|
|
64
|
+
file?: string;
|
|
65
|
+
|
|
85
66
|
/**
|
|
86
|
-
*
|
|
67
|
+
* Custom options
|
|
87
68
|
*/
|
|
88
|
-
options
|
|
69
|
+
options?: DatabaseSyncOptions;
|
|
89
70
|
}
|
|
90
71
|
```
|
|
91
72
|
|
package/__index__.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from './src/
|
|
2
|
-
export * from './src/connection.ts';
|
|
1
|
+
export * from './src/config.ts';
|
|
2
|
+
export * from './src/connection.ts';
|
|
3
|
+
export * from './src/service.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sqlite",
|
|
3
|
-
"version": "8.0.0-alpha.
|
|
3
|
+
"version": "8.0.0-alpha.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "SQLite backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
6
6
|
"keywords": [
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"directory": "module/model-sqlite"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/config": "^8.0.0-alpha.
|
|
31
|
-
"@travetto/context": "^8.0.0-alpha.
|
|
32
|
-
"@travetto/model": "^8.0.0-alpha.
|
|
33
|
-
"@travetto/model-query": "^8.0.0-alpha.
|
|
34
|
-
"@travetto/model-sql": "^8.0.0-alpha.
|
|
30
|
+
"@travetto/config": "^8.0.0-alpha.22",
|
|
31
|
+
"@travetto/context": "^8.0.0-alpha.20",
|
|
32
|
+
"@travetto/model": "^8.0.0-alpha.23",
|
|
33
|
+
"@travetto/model-query": "^8.0.0-alpha.24",
|
|
34
|
+
"@travetto/model-sql": "^8.0.0-alpha.26"
|
|
35
35
|
},
|
|
36
36
|
"travetto": {
|
|
37
37
|
"displayName": "SQLite Model Service"
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { DatabaseSyncOptions } from 'node:sqlite';
|
|
2
|
+
|
|
3
|
+
import { Config } from '@travetto/config';
|
|
4
|
+
import { Runtime } from '@travetto/runtime';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* SQLite Model Configuration
|
|
8
|
+
*/
|
|
9
|
+
@Config('model.sqlite')
|
|
10
|
+
export class SqliteModelConfig {
|
|
11
|
+
/**
|
|
12
|
+
* Namespace/schema prefix for table names
|
|
13
|
+
*/
|
|
14
|
+
namespace = '';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Allow storage modifications (like table auto-creation and schema updates) at runtime
|
|
18
|
+
*/
|
|
19
|
+
modifyStorage = !Runtime.production;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* SQLite file location
|
|
23
|
+
*/
|
|
24
|
+
file?: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Custom options
|
|
28
|
+
*/
|
|
29
|
+
options?: DatabaseSyncOptions;
|
|
30
|
+
}
|
package/src/connection.ts
CHANGED
|
@@ -1,38 +1,51 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { DatabaseSync, type
|
|
3
|
+
import { DatabaseSync, type SQLInputValue } from 'node:sqlite';
|
|
4
4
|
|
|
5
|
-
import { type Pool
|
|
5
|
+
import { createPool, type Pool } from 'generic-pool';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import type { AsyncContext } from '@travetto/context';
|
|
8
|
+
import { Injectable } from '@travetto/di';
|
|
9
9
|
import { ExistsError } from '@travetto/model';
|
|
10
|
-
import {
|
|
10
|
+
import { SQLConnection } from '@travetto/model-sql';
|
|
11
|
+
import { castTo, JSONUtil, Runtime, RuntimeError, ShutdownManager, Util } from '@travetto/runtime';
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
import type { SqliteModelConfig } from './config.ts';
|
|
14
|
+
import { SqliteDialect } from './dialect.ts';
|
|
13
15
|
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
+
const RECOVERABLE_MESSAGE = /database( table| schema)? is (locked|busy)/;
|
|
17
|
+
const isRecoverableError = (error: unknown): error is Error => error instanceof Error && RECOVERABLE_MESSAGE.test(error.message);
|
|
18
|
+
|
|
19
|
+
const normalizeParameter = (val: unknown) => {
|
|
20
|
+
if (val === null) {
|
|
21
|
+
return val;
|
|
22
|
+
} else if (val instanceof Date) {
|
|
23
|
+
return val.toISOString();
|
|
24
|
+
} else if (typeof val === 'object') {
|
|
25
|
+
return JSONUtil.toUTF8(val);
|
|
26
|
+
} else {
|
|
27
|
+
return castTo<SQLInputValue>(val);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
16
30
|
|
|
17
31
|
/**
|
|
18
|
-
* Connection
|
|
32
|
+
* SQLite Connection Manager.
|
|
33
|
+
* Operates on node:sqlite DatabaseSync using a pool with max=1.
|
|
19
34
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
35
|
+
@Injectable()
|
|
36
|
+
export class SqliteConnection extends SQLConnection<DatabaseSync> {
|
|
37
|
+
readonly dialect = new SqliteDialect();
|
|
22
38
|
isolatedTransactions = false;
|
|
23
39
|
|
|
24
|
-
#config: SQLModelConfig<DatabaseSyncOptions & { file?: string }>;
|
|
25
40
|
#pool: Pool<DatabaseSync>;
|
|
41
|
+
readonly config: SqliteModelConfig;
|
|
26
42
|
|
|
27
|
-
constructor(
|
|
28
|
-
context: AsyncContext,
|
|
29
|
-
config: SQLModelConfig<DatabaseSyncOptions & { file?: string }>
|
|
30
|
-
) {
|
|
43
|
+
constructor(context: AsyncContext, config: SqliteModelConfig) {
|
|
31
44
|
super(context);
|
|
32
|
-
this
|
|
45
|
+
this.config = config;
|
|
33
46
|
}
|
|
34
47
|
|
|
35
|
-
async #withRetries<T>(operation: () => Promise<T>, retries = 10, delay =
|
|
48
|
+
async #withRetries<T>(operation: () => Promise<T>, retries = 10, delay = 300): Promise<T> {
|
|
36
49
|
for (; retries > 1; retries -= 1) {
|
|
37
50
|
try {
|
|
38
51
|
return await operation();
|
|
@@ -48,57 +61,118 @@ export class SqliteConnection extends Connection<DatabaseSync> {
|
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
async #create(): Promise<DatabaseSync> {
|
|
51
|
-
const file = path.resolve(this
|
|
64
|
+
const file = path.resolve(this.config.file ?? Runtime.toolPath('@', 'sqlite_db'));
|
|
52
65
|
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
53
|
-
const db = new DatabaseSync(file, this
|
|
54
|
-
for (const q of [
|
|
55
|
-
'PRAGMA foreign_keys = ON',
|
|
56
|
-
'PRAGMA journal_mode = WAL',
|
|
57
|
-
'PRAGMA synchronous = NORMAL',
|
|
58
|
-
]) {
|
|
66
|
+
const db = new DatabaseSync(file, this.config.options ?? {});
|
|
67
|
+
for (const q of ['PRAGMA foreign_keys = ON', 'PRAGMA journal_mode = WAL', 'PRAGMA synchronous = NORMAL']) {
|
|
59
68
|
await this.#withRetries(async () => db.exec(q));
|
|
60
69
|
}
|
|
61
|
-
|
|
70
|
+
// Register custom regex function for SQL regex support
|
|
71
|
+
db.function('regexp', (pattern, value) => {
|
|
72
|
+
const patStr = String(pattern);
|
|
73
|
+
const valStr = String(value);
|
|
74
|
+
if (patStr.startsWith('(?i)')) {
|
|
75
|
+
return new RegExp(patStr.slice(4), 'i').test(valStr) ? 1 : 0;
|
|
76
|
+
}
|
|
77
|
+
return new RegExp(patStr).test(valStr) ? 1 : 0;
|
|
78
|
+
});
|
|
79
|
+
// Register custom json_contains function for JSON containment checks
|
|
80
|
+
db.function('json_contains', (target, candidate) => {
|
|
81
|
+
try {
|
|
82
|
+
const tgt = JSON.parse(String(target));
|
|
83
|
+
const cand = JSON.parse(String(candidate));
|
|
84
|
+
|
|
85
|
+
const matches = (t: unknown, c: unknown): boolean => {
|
|
86
|
+
if (c === null) {
|
|
87
|
+
return t === null;
|
|
88
|
+
}
|
|
89
|
+
if (typeof c === 'object') {
|
|
90
|
+
if (typeof t !== 'object' || t === null) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (Array.isArray(c)) {
|
|
94
|
+
if (!Array.isArray(t)) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return c.every(cv => t.some(tv => matches(tv, cv)));
|
|
98
|
+
} else {
|
|
99
|
+
// @ts-expect-error
|
|
100
|
+
return Object.keys(c).every(k => matches(t[k], c[k]));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return t === c;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return matches(tgt, cand) ? 1 : 0;
|
|
107
|
+
} catch {
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
62
111
|
return db;
|
|
63
112
|
}
|
|
64
113
|
|
|
65
114
|
/**
|
|
66
|
-
* Initializes
|
|
115
|
+
* Initializes the pool and sets connection pragma configuration
|
|
67
116
|
*/
|
|
68
|
-
|
|
69
|
-
override async init(): Promise<void> {
|
|
70
|
-
this.transactionDialect = { ...this.transactionDialect, begin: 'BEGIN IMMEDIATE;' };
|
|
71
|
-
|
|
117
|
+
async init(): Promise<void> {
|
|
72
118
|
await this.#create();
|
|
73
119
|
|
|
74
|
-
this.#pool = createPool<DatabaseSync>(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
120
|
+
this.#pool = createPool<DatabaseSync>(
|
|
121
|
+
{
|
|
122
|
+
create: () => this.#withRetries(() => this.#create()),
|
|
123
|
+
destroy: async db => {
|
|
124
|
+
db.close();
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
{ max: 1 }
|
|
128
|
+
);
|
|
78
129
|
|
|
79
|
-
// Close postgres
|
|
80
130
|
ShutdownManager.signal.addEventListener('abort', () => this.#pool.clear());
|
|
81
131
|
}
|
|
82
132
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Acquires a DB connection
|
|
135
|
+
*/
|
|
136
|
+
acquire(): Promise<DatabaseSync> {
|
|
137
|
+
return this.#withRetries(() => this.#pool.acquire());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Releases a DB connection back to pool
|
|
142
|
+
*/
|
|
143
|
+
release(connection: DatabaseSync): void {
|
|
144
|
+
this.#pool.release(connection);
|
|
145
|
+
}
|
|
87
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Executes a query on the active client or pool directly
|
|
149
|
+
*/
|
|
150
|
+
async execute<Type = unknown>(query: string, values?: unknown[]): Promise<{ count: number; records: Type[] }> {
|
|
151
|
+
const isSelect =
|
|
152
|
+
query.trim().startsWith('SELECT') ||
|
|
153
|
+
query.trim().startsWith('PRAGMA') ||
|
|
154
|
+
query.trim().startsWith('EXISTS') ||
|
|
155
|
+
query.includes('RETURNING');
|
|
156
|
+
|
|
157
|
+
const normalized = (values ?? []).map(normalizeParameter);
|
|
158
|
+
|
|
159
|
+
return this.#withRetries(async () => {
|
|
160
|
+
console.debug('Executing SQLite query', { query, values });
|
|
161
|
+
const client = this.active ?? (await this.acquire());
|
|
88
162
|
try {
|
|
89
|
-
const prepared =
|
|
163
|
+
const prepared = client.prepare(query);
|
|
90
164
|
prepared.setReadBigInts(true);
|
|
91
165
|
if (isSelect) {
|
|
92
|
-
const out = prepared.all(...
|
|
93
|
-
const records:
|
|
166
|
+
const out = prepared.all(...normalized);
|
|
167
|
+
const records: Type[] = out.map(item => ({ ...castTo<Type>(item) }));
|
|
94
168
|
return { count: out.length, records };
|
|
95
169
|
} else {
|
|
96
|
-
const out = prepared.run(...
|
|
170
|
+
const out = prepared.run(...normalized);
|
|
97
171
|
return { count: typeof out.changes === 'number' ? out.changes : +out.changes.toString(), records: [] };
|
|
98
172
|
}
|
|
99
173
|
} catch (error) {
|
|
100
174
|
const code = error && typeof error === 'object' && 'code' in error ? error.code : undefined;
|
|
101
|
-
const message =
|
|
175
|
+
const message = error instanceof Error ? error.message : undefined;
|
|
102
176
|
switch (code) {
|
|
103
177
|
case 'ERR_SQLITE_ERROR': {
|
|
104
178
|
if (message?.startsWith('UNIQUE')) {
|
|
@@ -108,31 +182,18 @@ export class SqliteConnection extends Connection<DatabaseSync> {
|
|
|
108
182
|
}
|
|
109
183
|
case 'SQLITE_CONSTRAINT_PRIMARYKEY':
|
|
110
184
|
case 'SQLITE_CONSTRAINT_UNIQUE':
|
|
111
|
-
case 'SQLITE_CONSTRAINT_INDEX':
|
|
112
|
-
|
|
185
|
+
case 'SQLITE_CONSTRAINT_INDEX':
|
|
186
|
+
throw new ExistsError('query', query);
|
|
187
|
+
}
|
|
113
188
|
if (/index.*?already exists/.test(message ?? '')) {
|
|
114
189
|
throw new ExistsError('index', query);
|
|
115
190
|
}
|
|
116
191
|
throw error;
|
|
192
|
+
} finally {
|
|
193
|
+
if (!this.active) {
|
|
194
|
+
this.release(client);
|
|
195
|
+
}
|
|
117
196
|
}
|
|
118
197
|
});
|
|
119
198
|
}
|
|
120
|
-
|
|
121
|
-
async acquire(): Promise<DatabaseSync> {
|
|
122
|
-
return await this.#pool.acquire();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async release(db: DatabaseSync): Promise<void> {
|
|
126
|
-
return this.#pool.release(db);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async pragma<T>(query: string): Promise<T> {
|
|
130
|
-
const db = await this.acquire();
|
|
131
|
-
try {
|
|
132
|
-
const result = this.#withRetries(async () => db.prepare(`PRAGMA ${query}`).get());
|
|
133
|
-
return castTo<T>(result);
|
|
134
|
-
} finally {
|
|
135
|
-
await this.release(db);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
199
|
+
}
|
package/src/dialect.ts
CHANGED
|
@@ -1,123 +1,146 @@
|
|
|
1
|
+
import { AbstractANSI99Dialect, type TableContext, type TransactionStatements } from '@travetto/model-sql';
|
|
2
|
+
import { type Class, castTo, JSONUtil } from '@travetto/runtime';
|
|
1
3
|
import type { SchemaFieldConfig } from '@travetto/schema';
|
|
2
|
-
import { Injectable } from '@travetto/di';
|
|
3
|
-
import type { AsyncContext } from '@travetto/context';
|
|
4
|
-
import type { WhereClause } from '@travetto/model-query';
|
|
5
|
-
import { castTo } from '@travetto/runtime';
|
|
6
|
-
|
|
7
|
-
import { type SQLModelConfig, SQLDialect, type VisitStack, type SQLTableDescription } from '@travetto/model-sql';
|
|
8
|
-
|
|
9
|
-
import { SqliteConnection } from './connection.ts';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Sqlite Dialect for the SQL Model Source
|
|
13
|
-
*/
|
|
14
|
-
@Injectable()
|
|
15
|
-
export class SqliteDialect extends SQLDialect {
|
|
16
|
-
|
|
17
|
-
connection: SqliteConnection;
|
|
18
|
-
config: SQLModelConfig;
|
|
19
|
-
|
|
20
|
-
constructor(context: AsyncContext, config: SQLModelConfig) {
|
|
21
|
-
super(config.namespace);
|
|
22
|
-
this.connection = new SqliteConnection(context, config);
|
|
23
|
-
this.config = config;
|
|
24
|
-
|
|
25
|
-
// Special operators
|
|
26
|
-
Object.assign(this.SQL_OPS, {
|
|
27
|
-
$regex: 'REGEXP',
|
|
28
|
-
$ilike: undefined
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// Special types
|
|
32
|
-
Object.assign(this.COLUMN_TYPES, {
|
|
33
|
-
JSON: 'TEXT',
|
|
34
|
-
TIMESTAMP: 'INTEGER'
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
override resolveDateValue(value: Date): string {
|
|
39
|
-
return `${value.getTime()}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* How to hash
|
|
44
|
-
*/
|
|
45
|
-
hash(value: string): string {
|
|
46
|
-
return `hex('${value}')`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async describeTable(table: string): Promise<SQLTableDescription | undefined> {
|
|
50
|
-
const IGNORE_FIELDS = [this.pathField.name, this.parentPathField.name, this.idxField.name].map(field => `'${field}'`);
|
|
51
|
-
|
|
52
|
-
const [columns, foreignKeys, indices] = await Promise.all([
|
|
53
|
-
this.executeSQL<{ name: string, type: string, is_not_null: 1 | 0 }>(`
|
|
54
|
-
SELECT
|
|
55
|
-
name,
|
|
56
|
-
type,
|
|
57
|
-
${this.identifier('notnull')} <> 0 AS is_not_null
|
|
58
|
-
FROM PRAGMA_TABLE_INFO('${table}')
|
|
59
|
-
WHERE name NOT IN (${IGNORE_FIELDS.join(',')})
|
|
60
|
-
`),
|
|
61
|
-
this.executeSQL<{ name: string, to_table: string, from_column: string, to_column: string }>(`
|
|
62
|
-
SELECT
|
|
63
|
-
'fk_' || '${table}' || '_' || ${this.identifier('from')} AS name,
|
|
64
|
-
${this.identifier('from')} as from_column,
|
|
65
|
-
${this.identifier('to')} as to_column,
|
|
66
|
-
${this.identifier('table')} as to_table
|
|
67
|
-
FROM PRAGMA_FOREIGN_KEY_LIST('${table}')
|
|
68
|
-
`),
|
|
69
|
-
this.executeSQL<{ name: string, is_unique: boolean, columns: string }>(`
|
|
70
|
-
SELECT
|
|
71
|
-
il.name as name,
|
|
72
|
-
il.${this.identifier('unique')} = 1 as is_unique,
|
|
73
|
-
GROUP_CONCAT(ii.seqno || ' ' || ii.name || ' ' || ii.desc) AS columns
|
|
74
|
-
FROM PRAGMA_INDEX_LIST('${table}') il
|
|
75
|
-
JOIN PRAGMA_INDEX_XINFO(il.name) ii
|
|
76
|
-
WHERE il.name NOT LIKE 'sqlite_%'
|
|
77
|
-
GROUP BY 1, 2
|
|
78
|
-
`)
|
|
79
|
-
]);
|
|
80
4
|
|
|
5
|
+
export class SqliteDialect extends AbstractANSI99Dialect {
|
|
6
|
+
returningSupport = true;
|
|
7
|
+
transactionStatements: TransactionStatements = {
|
|
8
|
+
...AbstractANSI99Dialect.TRANSACTION_STATEMENTS,
|
|
9
|
+
begin: 'BEGIN IMMEDIATE;'
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
getComplexColumnType(field: SchemaFieldConfig): string {
|
|
13
|
+
return 'TEXT';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getColumnType(fieldConfiguration: SchemaFieldConfig): string {
|
|
17
|
+
if (fieldConfiguration.type === castTo(BigInt)) {
|
|
18
|
+
return 'INTEGER';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (fieldConfiguration.type === Number) {
|
|
22
|
+
return 'NUMERIC';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (fieldConfiguration.type === Date) {
|
|
26
|
+
return 'TEXT';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (fieldConfiguration.type === Boolean) {
|
|
30
|
+
return 'INTEGER';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (fieldConfiguration.type === String) {
|
|
34
|
+
return 'TEXT';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return 'TEXT';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
compileJsonIndexPath(columnName: string, jsonPath: string[]): string {
|
|
41
|
+
return `json_extract(${columnName}, '$.${jsonPath.join('.')}')`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
compileArrayAll(sqlPath: string, identifier: string, value: unknown[]): { sql: string; formatted: unknown } {
|
|
45
|
+
return {
|
|
46
|
+
sql: `NOT EXISTS (SELECT 1 FROM json_each(${identifier}) AS req WHERE req.value NOT IN (SELECT value FROM json_each(${sqlPath})))`,
|
|
47
|
+
formatted: JSONUtil.toUTF8(value)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
compileArrayEquals(sqlPath: string, identifier: string, values: unknown): { sql: string; formatted: unknown } {
|
|
52
|
+
if (Array.isArray(values)) {
|
|
53
|
+
return {
|
|
54
|
+
sql: `NOT EXISTS (SELECT 1 FROM json_each(${identifier}) AS req WHERE req.value NOT IN (SELECT value FROM json_each(${sqlPath})))`,
|
|
55
|
+
formatted: JSONUtil.toUTF8(values)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (typeof values === 'object' && values !== null) {
|
|
59
|
+
return {
|
|
60
|
+
sql: `EXISTS (SELECT 1 FROM json_each(${sqlPath}) AS elem WHERE NOT EXISTS (SELECT 1 FROM json_each(${identifier}) AS req WHERE json_extract(elem.value, '$.' || req.key) IS NOT req.value))`,
|
|
61
|
+
formatted: JSONUtil.toUTF8(values)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
sql: `EXISTS (SELECT 1 FROM json_each(${sqlPath}) WHERE json_each.value = ${identifier})`,
|
|
66
|
+
formatted: values
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
compileArrayAny(sqlPath: string, identifier: string, values: unknown[]): { sql: string; formatted: unknown } {
|
|
71
|
+
return {
|
|
72
|
+
sql: `EXISTS (SELECT 1 FROM json_each(${sqlPath}) AS elem WHERE elem.value IN (SELECT value FROM json_each(${identifier})))`,
|
|
73
|
+
formatted: JSONUtil.toUTF8(values)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
compileArrayExists(sqlPath: string): { sql: string } {
|
|
78
|
+
return { sql: `(${sqlPath} IS NOT NULL AND json_array_length(${sqlPath}) > 0)` };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getRegexOperator(caseInsensitive: boolean): string {
|
|
82
|
+
return 'REGEXP';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
formatRegex(source: string, caseInsensitive: boolean): string {
|
|
86
|
+
return caseInsensitive ? `(?i)${source}` : source;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
castColumn(sqlPath: string, type: Class): string {
|
|
90
|
+
if (type === Number) {
|
|
91
|
+
return `CAST(${sqlPath} AS NUMERIC)`;
|
|
92
|
+
}
|
|
93
|
+
return sqlPath;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getTableExistsQuery(context: TableContext): { sql: string; parameters?: unknown[] } {
|
|
81
97
|
return {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
indices: indices.records.map(idx => ({
|
|
88
|
-
name: idx.name,
|
|
89
|
-
is_unique: idx.is_unique,
|
|
90
|
-
columns: idx.columns.split(',')
|
|
91
|
-
.map(col => col.split(' '))
|
|
92
|
-
.map(([order, name, desc]) => [+order, { name, desc: desc === '1' }] as const)
|
|
93
|
-
.sort((a, b) => a[0] - b[0])
|
|
94
|
-
.map(([, item]) => item)
|
|
95
|
-
}))
|
|
98
|
+
sql: `
|
|
99
|
+
SELECT name
|
|
100
|
+
FROM sqlite_master
|
|
101
|
+
WHERE type='table' AND name=?;`,
|
|
102
|
+
parameters: [context.tableName]
|
|
96
103
|
};
|
|
97
104
|
}
|
|
98
105
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
parseTableExistsResult(records: unknown[]): boolean {
|
|
107
|
+
return records.length > 0;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
getExistingColumnsQuery(context: TableContext): { sql: string; parameters?: unknown[] } {
|
|
111
|
+
return {
|
|
112
|
+
sql: `PRAGMA table_info('${this.escapeLiteral(context.tableName)}');`
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
parseExistingColumns(records: unknown[]): Map<string, string> {
|
|
117
|
+
return new Map(castTo<{ name: string; type: string }[]>(records).map(record => [record.name, record.type.toUpperCase()]));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getExistingIndexesQuery(context: TableContext): { sql: string; parameters?: unknown[] } {
|
|
121
|
+
return {
|
|
122
|
+
sql: `
|
|
123
|
+
SELECT name, sql
|
|
124
|
+
FROM sqlite_master
|
|
125
|
+
WHERE type='index' AND tbl_name=?;
|
|
126
|
+
`,
|
|
127
|
+
parameters: [context.tableName]
|
|
128
|
+
};
|
|
107
129
|
}
|
|
108
130
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
131
|
+
parseExistingIndexes(records: unknown[]): Map<string, string> {
|
|
132
|
+
return new Map(
|
|
133
|
+
castTo<{ name: string; sql: string }[]>(records)
|
|
134
|
+
.filter(record => record.sql && !record.name.startsWith('sqlite_'))
|
|
135
|
+
.map(record => [record.name, record.sql])
|
|
136
|
+
);
|
|
114
137
|
}
|
|
115
138
|
|
|
116
|
-
|
|
117
|
-
return
|
|
139
|
+
getDropIndexSQL(context: TableContext, indexName: string): string {
|
|
140
|
+
return `DROP INDEX IF EXISTS ${this.escapeIdentifier(indexName)};`;
|
|
118
141
|
}
|
|
119
142
|
|
|
120
|
-
|
|
121
|
-
return
|
|
143
|
+
getTruncateTableSQL(context: TableContext): string {
|
|
144
|
+
return `DELETE FROM ${this.escapeIdentifier(context.tableName)};`;
|
|
122
145
|
}
|
|
123
|
-
}
|
|
146
|
+
}
|
package/src/service.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
|
|
3
|
+
import { Injectable, PostConstruct } from '@travetto/di';
|
|
4
|
+
import { BaseSQLModelService } from '@travetto/model-sql';
|
|
5
|
+
|
|
6
|
+
import type { SqliteConnection } from './connection.ts';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A SQLite JSON-based document store model service
|
|
10
|
+
*/
|
|
11
|
+
@Injectable()
|
|
12
|
+
export class SqliteModelService extends BaseSQLModelService {
|
|
13
|
+
connection: SqliteConnection;
|
|
14
|
+
|
|
15
|
+
constructor(connection: SqliteConnection) {
|
|
16
|
+
super();
|
|
17
|
+
this.connection = connection;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get client(): DatabaseSync {
|
|
21
|
+
return this.connection.active!;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@PostConstruct()
|
|
25
|
+
override async initialize(): Promise<void> {
|
|
26
|
+
await super.initialize();
|
|
27
|
+
}
|
|
28
|
+
}
|