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

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 +3 -3
  2. package/src/dialect.ts +36 -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.28",
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": [
@@ -31,8 +31,8 @@
31
31
  "@travetto/config": "^8.0.0-alpha.23",
32
32
  "@travetto/context": "^8.0.0-alpha.21",
33
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",
34
+ "@travetto/model-query": "^8.0.0-alpha.26",
35
+ "@travetto/model-sql": "^8.0.0-alpha.28",
36
36
  "@types/pg": "^8.20.0",
37
37
  "pg": "^8.22.0"
38
38
  },
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) {