@travetto/model-sql 3.0.0-rc.2 → 3.0.0-rc.21
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 +4 -4
- package/package.json +21 -14
- package/src/config.ts +3 -2
- package/src/connection/base.ts +2 -2
- package/src/connection/decorator.ts +1 -1
- package/src/dialect/base.ts +11 -9
- package/src/internal/util.ts +7 -7
- package/src/service.ts +2 -2
- package/{test-support → support/test}/query.ts +4 -4
- /package/{index.ts → __index__.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
|
-
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-sql/
|
|
2
|
+
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-sql/DOC.ts and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# SQL Model Service
|
|
4
4
|
## SQL backing for the travetto model module, with real-time modeling support for SQL schemas.
|
|
5
5
|
|
|
@@ -29,9 +29,9 @@ npm install @travetto/model-sqlite
|
|
|
29
29
|
|
|
30
30
|
The current SQL client support stands at:
|
|
31
31
|
|
|
32
|
-
* [MySQL](https://www.mysql.com/) -
|
|
33
|
-
* [Postgres](https://postgresql.org) -
|
|
34
|
-
* [SQLite](https://www.sqlite.org/) - (bettersqlite
|
|
32
|
+
* [MySQL](https://www.mysql.com/) - 8.6+
|
|
33
|
+
* [Postgres](https://postgresql.org) - 14+
|
|
34
|
+
* [SQLite](https://www.sqlite.org/) - (bettersqlite 8.0+)
|
|
35
35
|
* `SQL Server` - Currently unsupported
|
|
36
36
|
* `Oracle` - Currently unsupported
|
|
37
37
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-sql",
|
|
3
|
-
"
|
|
4
|
-
"version": "3.0.0-rc.2",
|
|
3
|
+
"version": "3.0.0-rc.21",
|
|
5
4
|
"description": "SQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
|
|
6
5
|
"keywords": [
|
|
7
6
|
"sql",
|
|
@@ -18,27 +17,35 @@
|
|
|
18
17
|
"name": "Travetto Framework"
|
|
19
18
|
},
|
|
20
19
|
"files": [
|
|
21
|
-
"
|
|
20
|
+
"__index__.ts",
|
|
22
21
|
"src",
|
|
23
|
-
"support"
|
|
24
|
-
"test-support"
|
|
22
|
+
"support"
|
|
25
23
|
],
|
|
26
|
-
"main": "
|
|
24
|
+
"main": "__index__.ts",
|
|
27
25
|
"repository": {
|
|
28
26
|
"url": "https://github.com/travetto/travetto.git",
|
|
29
27
|
"directory": "module/model-sql"
|
|
30
28
|
},
|
|
31
29
|
"dependencies": {
|
|
32
|
-
"@travetto/config": "^3.0.0-rc.
|
|
33
|
-
"@travetto/context": "^3.0.0-rc.
|
|
34
|
-
"@travetto/model": "^3.0.0-rc.
|
|
35
|
-
"@travetto/model-query": "3.0.0-rc.
|
|
30
|
+
"@travetto/config": "^3.0.0-rc.21",
|
|
31
|
+
"@travetto/context": "^3.0.0-rc.20",
|
|
32
|
+
"@travetto/model": "^3.0.0-rc.21",
|
|
33
|
+
"@travetto/model-query": "^3.0.0-rc.21"
|
|
36
34
|
},
|
|
37
|
-
"
|
|
38
|
-
"@travetto/
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@travetto/command": "^3.0.0-rc.17",
|
|
37
|
+
"@travetto/test": "^3.0.0-rc.23"
|
|
39
38
|
},
|
|
40
|
-
"
|
|
41
|
-
"@travetto/
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"@travetto/command": {
|
|
41
|
+
"optional": true
|
|
42
|
+
},
|
|
43
|
+
"@travetto/test": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"travetto": {
|
|
48
|
+
"displayName": "SQL Model Service"
|
|
42
49
|
},
|
|
43
50
|
"publishConfig": {
|
|
44
51
|
"access": "public"
|
package/src/config.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Config } from '@travetto/config';
|
|
|
4
4
|
* SQL Model Config
|
|
5
5
|
*/
|
|
6
6
|
@Config('model.sql')
|
|
7
|
-
export class SQLModelConfig {
|
|
7
|
+
export class SQLModelConfig<T extends {} = {}> {
|
|
8
8
|
/**
|
|
9
9
|
* Host to connect to
|
|
10
10
|
*/
|
|
@@ -40,5 +40,6 @@ export class SQLModelConfig {
|
|
|
40
40
|
/**
|
|
41
41
|
* Raw client options
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
44
|
+
options: T = {} as T;
|
|
44
45
|
}
|
package/src/connection/base.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Util } from '@travetto/base';
|
|
2
2
|
import { AsyncContext } from '@travetto/context';
|
|
3
3
|
|
|
4
|
-
const ContextActiveⲐ: unique symbol = Symbol.for('@
|
|
5
|
-
const TxActiveⲐ: unique symbol = Symbol.for('@
|
|
4
|
+
const ContextActiveⲐ: unique symbol = Symbol.for('@travetto/model:sql-active');
|
|
5
|
+
const TxActiveⲐ: unique symbol = Symbol.for('@travetto/model:sql-transaction');
|
|
6
6
|
|
|
7
7
|
export type TransactionType = 'required' | 'isolated' | 'force';
|
|
8
8
|
|
package/src/dialect/base.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { RootIndex } from '@travetto/manifest';
|
|
1
2
|
import { SchemaRegistry, FieldConfig, Schema } from '@travetto/schema';
|
|
2
|
-
import { Class,
|
|
3
|
+
import { Class, ObjectUtil, AppError, TypedObject, DataUtil } from '@travetto/base';
|
|
3
4
|
import { SelectClause, Query, SortClause, WhereClause, RetainFields } from '@travetto/model-query';
|
|
4
5
|
import { BulkResponse, IndexConfig } from '@travetto/model';
|
|
5
6
|
import { PointImpl } from '@travetto/model-query/src/internal/model/point';
|
|
6
7
|
import { ModelType } from '@travetto/model/src/types/model';
|
|
7
8
|
import { ModelQueryUtil } from '@travetto/model-query/src/internal/service/query';
|
|
9
|
+
import { ModelUtil } from '@travetto/model/src/internal/util';
|
|
8
10
|
|
|
9
11
|
import { SQLUtil, VisitStack } from '../internal/util';
|
|
10
12
|
import { DeleteWrapper, InsertWrapper, DialectState } from '../internal/types';
|
|
@@ -186,7 +188,7 @@ export abstract class SQLDialect implements DialectState {
|
|
|
186
188
|
return 'NULL';
|
|
187
189
|
} else if (conf.type === String) {
|
|
188
190
|
if (value instanceof RegExp) {
|
|
189
|
-
const src =
|
|
191
|
+
const src = DataUtil.toRegex(value).source.replace(/\\b/g, this.regexWordBoundary);
|
|
190
192
|
return this.quote(src);
|
|
191
193
|
} else {
|
|
192
194
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
@@ -309,7 +311,7 @@ export abstract class SQLDialect implements DialectState {
|
|
|
309
311
|
* Generate a UUID
|
|
310
312
|
*/
|
|
311
313
|
generateId(): string {
|
|
312
|
-
return
|
|
314
|
+
return ModelUtil.uuid(this.KEY_LEN);
|
|
313
315
|
}
|
|
314
316
|
|
|
315
317
|
/**
|
|
@@ -424,7 +426,7 @@ export abstract class SQLDialect implements DialectState {
|
|
|
424
426
|
}
|
|
425
427
|
const sPath = this.resolveName(sStack);
|
|
426
428
|
|
|
427
|
-
if (
|
|
429
|
+
if (ObjectUtil.isPlainObject(top)) {
|
|
428
430
|
const subKey = Object.keys(top)[0];
|
|
429
431
|
if (!subKey.startsWith('$')) {
|
|
430
432
|
const inner = this.getWhereFieldSQL(sStack, top);
|
|
@@ -456,7 +458,7 @@ export abstract class SQLDialect implements DialectState {
|
|
|
456
458
|
}
|
|
457
459
|
case '$regex': {
|
|
458
460
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
459
|
-
const re =
|
|
461
|
+
const re = DataUtil.toRegex(v as string);
|
|
460
462
|
const src = re.source;
|
|
461
463
|
const ins = re.flags && re.flags.includes('i');
|
|
462
464
|
|
|
@@ -490,7 +492,7 @@ export abstract class SQLDialect implements DialectState {
|
|
|
490
492
|
}
|
|
491
493
|
case '$lt': case '$gt': case '$gte': case '$lte': {
|
|
492
494
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
493
|
-
const subItems =
|
|
495
|
+
const subItems = TypedObject.keys(top as typeof SQL_OPS)
|
|
494
496
|
.map(ssk => `${sPath} ${SQL_OPS[ssk]} ${resolve(top[ssk])}`);
|
|
495
497
|
items.push(subItems.length > 1 ? `(${subItems.join(` ${SQL_OPS.$and} `)})` : subItems[0]);
|
|
496
498
|
break;
|
|
@@ -630,7 +632,7 @@ ${this.getLimitSQL(cls, query)}`;
|
|
|
630
632
|
const parent = stack.length > 1;
|
|
631
633
|
const array = parent && config.array;
|
|
632
634
|
|
|
633
|
-
if (config.type && config.type
|
|
635
|
+
if (config.type && RootIndex.getFunctionMetadata(config.type)?.synthetic) {
|
|
634
636
|
throw new AppError(`Cannot create SQL tables for synthetic types, please convert ${SQLUtil.buildPath(stack)} to a concrete class`);
|
|
635
637
|
}
|
|
636
638
|
|
|
@@ -710,9 +712,9 @@ CREATE TABLE IF NOT EXISTS ${this.table(stack)} (
|
|
|
710
712
|
getCreateIndexSQL<T extends ModelType>(cls: Class<T>, idx: IndexConfig<T>): string {
|
|
711
713
|
const table = this.namespace(SQLUtil.classToStack(cls));
|
|
712
714
|
const fields: [string, boolean][] = idx.fields.map(x => {
|
|
713
|
-
const key =
|
|
715
|
+
const key = TypedObject.keys(x)[0];
|
|
714
716
|
const val = x[key];
|
|
715
|
-
if (
|
|
717
|
+
if (ObjectUtil.isPlainObject(val)) {
|
|
716
718
|
throw new Error('Unable to supported nested fields for indices');
|
|
717
719
|
}
|
|
718
720
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
package/src/internal/util.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Class,
|
|
1
|
+
import { Class, TypedObject, ObjectUtil } from '@travetto/base';
|
|
2
2
|
import { SelectClause, SortClause } from '@travetto/model-query';
|
|
3
3
|
import { ModelRegistry, ModelType } from '@travetto/model';
|
|
4
4
|
import { SchemaRegistry, ClassConfig, FieldConfig } from '@travetto/schema';
|
|
@@ -6,7 +6,7 @@ import { AllViewⲐ } from '@travetto/schema/src/internal/types';
|
|
|
6
6
|
|
|
7
7
|
import { DialectState, InsertWrapper, VisitHandler, VisitState, VisitInstanceNode, OrderBy } from './types';
|
|
8
8
|
|
|
9
|
-
const TableⲐ = Symbol.for('@
|
|
9
|
+
const TableⲐ = Symbol.for('@travetto/model-sql:table');
|
|
10
10
|
|
|
11
11
|
export type VisitStack = {
|
|
12
12
|
[TableⲐ]?: string;
|
|
@@ -45,8 +45,8 @@ export class SQLUtil {
|
|
|
45
45
|
static cleanResults<T, U = T>(dct: DialectState, o: T | T[]): U | U[] {
|
|
46
46
|
if (Array.isArray(o)) {
|
|
47
47
|
return o.filter(x => x !== null && x !== undefined).map(x => this.cleanResults(dct, x));
|
|
48
|
-
} else if (!
|
|
49
|
-
for (const k of
|
|
48
|
+
} else if (!ObjectUtil.isSimple(o)) {
|
|
49
|
+
for (const k of TypedObject.keys(o)) {
|
|
50
50
|
if (o[k] === null || o[k] === undefined || k === dct.parentPathField.name || k === dct.pathField.name || k === dct.idxField.name) {
|
|
51
51
|
delete o[k];
|
|
52
52
|
} else {
|
|
@@ -228,10 +228,10 @@ export class SQLUtil {
|
|
|
228
228
|
|
|
229
229
|
let toGet = new Set<string>();
|
|
230
230
|
|
|
231
|
-
for (const [k, v] of
|
|
231
|
+
for (const [k, v] of TypedObject.entries(select)) {
|
|
232
232
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
233
233
|
const sk = k as string;
|
|
234
|
-
if (!
|
|
234
|
+
if (!ObjectUtil.isPlainObject(select[k]) && localMap[sk]) {
|
|
235
235
|
if (!v) {
|
|
236
236
|
if (toGet.size === 0) {
|
|
237
237
|
toGet = new Set(SchemaRegistry.get(cls).views[AllViewⲐ].fields);
|
|
@@ -257,7 +257,7 @@ export class SQLUtil {
|
|
|
257
257
|
const key = Object.keys(cl)[0];
|
|
258
258
|
const val = cl[key];
|
|
259
259
|
const field = { ...schema.views[AllViewⲐ].schema[key] };
|
|
260
|
-
if (
|
|
260
|
+
if (ObjectUtil.isPrimitive(val)) {
|
|
261
261
|
stack.push(field);
|
|
262
262
|
found = { stack, asc: val === 1 };
|
|
263
263
|
} else {
|
package/src/service.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
BulkOp, BulkResponse, ModelCrudSupport, ModelStorageSupport, ModelBulkSupport,
|
|
4
4
|
NotFoundError, ModelRegistry, ExistsError, OptionalId
|
|
5
5
|
} from '@travetto/model';
|
|
6
|
-
import {
|
|
6
|
+
import { DataUtil, Class } from '@travetto/base';
|
|
7
7
|
import { SchemaChange } from '@travetto/schema';
|
|
8
8
|
import { AsyncContext } from '@travetto/context';
|
|
9
9
|
import { Injectable } from '@travetto/di';
|
|
@@ -315,7 +315,7 @@ export class SQLModelService implements
|
|
|
315
315
|
|
|
316
316
|
const results = await this.#exec<{ key: string, count: number }>(q.join('\n'));
|
|
317
317
|
return results.records.map(x => {
|
|
318
|
-
x.count =
|
|
318
|
+
x.count = DataUtil.coerceType(x.count, Number);
|
|
319
319
|
return x;
|
|
320
320
|
});
|
|
321
321
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import assert from 'assert';
|
|
2
2
|
|
|
3
3
|
import { Schema, FieldConfig } from '@travetto/schema';
|
|
4
4
|
import { Suite, Test } from '@travetto/test';
|
|
5
|
-
import { BaseModelSuite } from '@travetto/model/
|
|
5
|
+
import { BaseModelSuite } from '@travetto/model/support/test/base';
|
|
6
6
|
|
|
7
|
-
import { VisitStack } from '
|
|
8
|
-
import { SQLModelService } from '
|
|
7
|
+
import { VisitStack } from '../../src/internal/util';
|
|
8
|
+
import { SQLModelService } from '../../src/service';
|
|
9
9
|
|
|
10
10
|
@Schema()
|
|
11
11
|
class User {
|
|
File without changes
|