@travetto/model-postgres 8.0.0-alpha.27 → 8.0.0-alpha.29

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 +7 -7
  2. package/src/dialect.ts +61 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-postgres",
3
- "version": "8.0.0-alpha.27",
3
+ "version": "8.0.0-alpha.29",
4
4
  "type": "module",
5
5
  "description": "PostgreSQL backing for the travetto model module, with real-time modeling support for SQL schemas.",
6
6
  "keywords": [
@@ -28,16 +28,16 @@
28
28
  "directory": "module/model-postgres"
29
29
  },
30
30
  "dependencies": {
31
- "@travetto/config": "^8.0.0-alpha.23",
32
- "@travetto/context": "^8.0.0-alpha.21",
33
- "@travetto/model": "^8.0.0-alpha.24",
34
- "@travetto/model-query": "^8.0.0-alpha.25",
35
- "@travetto/model-sql": "^8.0.0-alpha.27",
31
+ "@travetto/config": "^8.0.0-alpha.24",
32
+ "@travetto/context": "^8.0.0-alpha.22",
33
+ "@travetto/model": "^8.0.0-alpha.25",
34
+ "@travetto/model-query": "^8.0.0-alpha.27",
35
+ "@travetto/model-sql": "^8.0.0-alpha.29",
36
36
  "@types/pg": "^8.20.0",
37
37
  "pg": "^8.22.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@travetto/cli": "^8.0.0-alpha.29"
40
+ "@travetto/cli": "^8.0.0-alpha.30"
41
41
  },
42
42
  "peerDependenciesMeta": {
43
43
  "@travetto/cli": {
package/src/dialect.ts CHANGED
@@ -74,26 +74,48 @@ export class PostgresDialect extends AbstractANSI99Dialect {
74
74
  return `$${index}`;
75
75
  }
76
76
 
77
- #buildContainmentPayload(value: unknown, context?: ResolvedPathContext): unknown {
78
- if (!context?.subPath || context.subPath.length === 0) {
77
+ #buildContainmentPayload(value: unknown, context: ResolvedPathContext): unknown {
78
+ if (!context.subPath || context.subPath.length === 0) {
79
79
  return Array.isArray(value) ? value : [value];
80
80
  }
81
81
 
82
+ const isArraySegment = new Array<boolean>(context.subPath.length).fill(false);
83
+ let currentClass: Class | undefined = context.arrayField?.type;
84
+ for (let index = 0; index < context.subPath.length; index++) {
85
+ if (!currentClass) {
86
+ break;
87
+ }
88
+ const segment = context.subPath[index];
89
+ const classConfig = SchemaRegistryIndex.getOptional(currentClass)?.get();
90
+ const fieldConfig = classConfig?.fields[segment];
91
+ if (fieldConfig) {
92
+ if (fieldConfig.array) {
93
+ isArraySegment[index] = true;
94
+ }
95
+ currentClass = fieldConfig.type;
96
+ } else {
97
+ break;
98
+ }
99
+ }
100
+
101
+ const buildPayloadForValue = (item: unknown): unknown => {
102
+ let itemPayload: unknown = item;
103
+ for (let index = context.subPath!.length - 1; index >= 0; index--) {
104
+ const segment = context.subPath![index];
105
+ if (isArraySegment[index]) {
106
+ itemPayload = { [segment]: Array.isArray(itemPayload) ? itemPayload : [itemPayload] };
107
+ } else {
108
+ itemPayload = { [segment]: itemPayload };
109
+ }
110
+ }
111
+ return itemPayload;
112
+ };
113
+
82
114
  let currentPayload: unknown;
83
115
  if (Array.isArray(value)) {
84
- currentPayload = value.map(item => {
85
- let itemPayload: unknown = item;
86
- for (let index = context.subPath!.length - 1; index >= 0; index--) {
87
- itemPayload = { [context.subPath![index]]: itemPayload };
88
- }
89
- return itemPayload;
90
- });
116
+ currentPayload = value.map(item => buildPayloadForValue(item));
91
117
  } else {
92
- currentPayload = value;
93
- for (let index = context.subPath.length - 1; index >= 0; index--) {
94
- currentPayload = { [context.subPath[index]]: currentPayload };
95
- }
96
- currentPayload = [currentPayload];
118
+ currentPayload = [buildPayloadForValue(value)];
97
119
  }
98
120
 
99
121
  if (context.arrayPath && context.arrayPath.length > 1) {
@@ -163,6 +185,31 @@ export class PostgresDialect extends AbstractANSI99Dialect {
163
185
  return { sql: `(${target.jsonbPath} IS NOT NULL AND ${target.jsonbPath} <> '[]'::jsonb)` };
164
186
  }
165
187
 
188
+ compileArrayRegex(context: ResolvedPathContext, identifier: string, value: RegExp | string): { sql: string; formatted: unknown } {
189
+ const target = this.#getPostgresArrayTarget(context);
190
+ const regex = value instanceof RegExp ? value : new RegExp(String(value));
191
+ const caseInsensitive = regex.flags.includes('i');
192
+ const regexOp = this.getRegexOperator(caseInsensitive);
193
+ const regexSource = this.formatRegex(regex.source, caseInsensitive);
194
+
195
+ if (target.isNative) {
196
+ return {
197
+ sql: `EXISTS (SELECT 1 FROM unnest(${target.sqlPath}) AS elem WHERE elem ${regexOp} ${identifier})`,
198
+ formatted: regexSource
199
+ };
200
+ }
201
+
202
+ const subAccessor =
203
+ context.subPath && context.subPath.length > 0
204
+ ? `->${context.subPath.map(segment => `'${this.escapeLiteral(segment)}'`).join('->')}`
205
+ : '';
206
+
207
+ return {
208
+ sql: `EXISTS (SELECT 1 FROM jsonb_array_elements_text((${target.jsonbPath})${subAccessor}) AS elem WHERE elem ${regexOp} ${identifier})`,
209
+ formatted: regexSource
210
+ };
211
+ }
212
+
166
213
  getRegexOperator(caseInsensitive: boolean): string {
167
214
  return caseInsensitive ? '~*' : '~';
168
215
  }