@travetto/model-sqlite 8.0.0-alpha.29 → 8.0.0-alpha.30

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/dialect.ts +17 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-sqlite",
3
- "version": "8.0.0-alpha.29",
3
+ "version": "8.0.0-alpha.30",
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": [
@@ -31,7 +31,7 @@
31
31
  "@travetto/context": "^8.0.0-alpha.22",
32
32
  "@travetto/model": "^8.0.0-alpha.25",
33
33
  "@travetto/model-query": "^8.0.0-alpha.27",
34
- "@travetto/model-sql": "^8.0.0-alpha.29"
34
+ "@travetto/model-sql": "^8.0.0-alpha.30"
35
35
  },
36
36
  "travetto": {
37
37
  "displayName": "SQLite Model Service"
package/src/dialect.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AbstractANSI99Dialect, type ResolvedPathContext, type TableContext, type TransactionStatements } from '@travetto/model-sql';
2
2
  import { type Class, castTo, JSONUtil } from '@travetto/runtime';
3
- import { type SchemaFieldConfig, SchemaRegistryIndex } from '@travetto/schema';
3
+ import type { SchemaFieldConfig } from '@travetto/schema';
4
4
 
5
5
  export class SqliteDialect extends AbstractANSI99Dialect {
6
6
  returningSupport = true;
@@ -9,6 +9,16 @@ export class SqliteDialect extends AbstractANSI99Dialect {
9
9
  begin: 'BEGIN IMMEDIATE;'
10
10
  };
11
11
 
12
+ override getUpsertSQL(
13
+ context: TableContext,
14
+ columns: string[],
15
+ placeholders: string[],
16
+ conflictTarget: string[],
17
+ updates: string[]
18
+ ): string {
19
+ return `INSERT INTO ${this.escapeIdentifier(context.tableName)} (${columns.join(', ')}) VALUES (${placeholders.join(', ')}) ON CONFLICT (${conflictTarget.join(', ')}) DO UPDATE SET ${updates.join(', ')} RETURNING *;`;
20
+ }
21
+
12
22
  getComplexColumnType(field: SchemaFieldConfig): string {
13
23
  return 'TEXT';
14
24
  }
@@ -38,7 +48,7 @@ export class SqliteDialect extends AbstractANSI99Dialect {
38
48
  }
39
49
 
40
50
  compileJsonIndexPath(columnName: string, jsonPath: string[]): string {
41
- return `json_extract(${columnName}, '$.${jsonPath.join('.')}')`;
51
+ return `json_extract(${columnName}, '$.${this.formatJsonPath(jsonPath)}')`;
42
52
  }
43
53
 
44
54
  #getSqliteArrayExpression(context: ResolvedPathContext): string {
@@ -55,22 +65,10 @@ export class SqliteDialect extends AbstractANSI99Dialect {
55
65
  return onLeaf(parentExpression);
56
66
  }
57
67
 
58
- const arraySegmentIndices: number[] = [];
59
- let currentClass: Class | undefined = context.arrayField?.type;
60
-
61
- for (let index = 0; index < context.subPath.length; index++) {
62
- const segment = context.subPath[index];
63
- if (currentClass) {
64
- const classConfiguration = SchemaRegistryIndex.getOptional(currentClass)?.get();
65
- const fieldConfiguration = classConfiguration?.fields[segment];
66
- if (fieldConfiguration) {
67
- if (fieldConfiguration.array) {
68
- arraySegmentIndices.push(index);
69
- }
70
- currentClass = fieldConfiguration.type;
71
- }
72
- }
73
- }
68
+ const subPathMetadata = this.getSchemaSubPathMetadata(context.arrayField?.type, context.subPath);
69
+ const arraySegmentIndices = subPathMetadata
70
+ .map((metadataItem, metadataIndex) => (metadataItem.isArray ? metadataIndex : -1))
71
+ .filter(itemIndex => itemIndex !== -1);
74
72
 
75
73
  if (arraySegmentIndices.length === 0) {
76
74
  const leafExpression = `json_extract(${parentExpression}, '$.${context.subPath.join('.')}')`;
@@ -135,15 +133,7 @@ EXISTS (
135
133
 
136
134
  if (typeof values === 'object' && values !== null) {
137
135
  return {
138
- sql: this.#buildArrayElementExists(
139
- context,
140
- leafExpression => `
141
- NOT EXISTS (
142
- SELECT 1
143
- FROM json_each(${identifier}) AS req
144
- WHERE json_extract(${leafExpression}, '$.' || req.key) IS NOT req.value
145
- )`
146
- ),
136
+ sql: this.#buildArrayElementExists(context, leafExpression => `json_patch(${leafExpression}, ${identifier}) = ${leafExpression}`),
147
137
  formatted: JSONUtil.toUTF8(values)
148
138
  };
149
139
  }