@proofkit/fmodata 0.1.0-alpha.13 → 0.1.0-alpha.15
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 +489 -334
- package/dist/esm/client/batch-builder.d.ts +7 -5
- package/dist/esm/client/batch-builder.js +84 -25
- package/dist/esm/client/batch-builder.js.map +1 -1
- package/dist/esm/client/builders/default-select.d.ts +7 -0
- package/dist/esm/client/builders/default-select.js +42 -0
- package/dist/esm/client/builders/default-select.js.map +1 -0
- package/dist/esm/client/builders/expand-builder.d.ts +43 -0
- package/dist/esm/client/builders/expand-builder.js +173 -0
- package/dist/esm/client/builders/expand-builder.js.map +1 -0
- package/dist/esm/client/builders/index.d.ts +8 -0
- package/dist/esm/client/builders/query-string-builder.d.ts +15 -0
- package/dist/esm/client/builders/query-string-builder.js +25 -0
- package/dist/esm/client/builders/query-string-builder.js.map +1 -0
- package/dist/esm/client/builders/response-processor.d.ts +39 -0
- package/dist/esm/client/builders/response-processor.js +170 -0
- package/dist/esm/client/builders/response-processor.js.map +1 -0
- package/dist/esm/client/builders/select-mixin.d.ts +31 -0
- package/dist/esm/client/builders/select-mixin.js +30 -0
- package/dist/esm/client/builders/select-mixin.js.map +1 -0
- package/dist/esm/client/builders/select-utils.d.ts +8 -0
- package/dist/esm/client/builders/select-utils.js +15 -0
- package/dist/esm/client/builders/select-utils.js.map +1 -0
- package/dist/esm/client/builders/shared-types.d.ts +39 -0
- package/dist/esm/client/builders/table-utils.d.ts +35 -0
- package/dist/esm/client/builders/table-utils.js +45 -0
- package/dist/esm/client/builders/table-utils.js.map +1 -0
- package/dist/esm/client/database.d.ts +3 -22
- package/dist/esm/client/database.js +14 -76
- package/dist/esm/client/database.js.map +1 -1
- package/dist/esm/client/delete-builder.d.ts +12 -19
- package/dist/esm/client/delete-builder.js +26 -26
- package/dist/esm/client/delete-builder.js.map +1 -1
- package/dist/esm/client/entity-set.d.ts +32 -32
- package/dist/esm/client/entity-set.js +92 -69
- package/dist/esm/client/entity-set.js.map +1 -1
- package/dist/esm/client/error-parser.d.ts +12 -0
- package/dist/esm/client/error-parser.js +30 -0
- package/dist/esm/client/error-parser.js.map +1 -0
- package/dist/esm/client/filemaker-odata.d.ts +2 -4
- package/dist/esm/client/filemaker-odata.js +1 -5
- package/dist/esm/client/filemaker-odata.js.map +1 -1
- package/dist/esm/client/insert-builder.d.ts +9 -12
- package/dist/esm/client/insert-builder.js +70 -24
- package/dist/esm/client/insert-builder.js.map +1 -1
- package/dist/esm/client/query/expand-builder.d.ts +35 -0
- package/dist/esm/client/query/index.d.ts +3 -0
- package/dist/esm/client/query/query-builder.d.ts +133 -0
- package/dist/esm/client/query/query-builder.js +505 -0
- package/dist/esm/client/query/query-builder.js.map +1 -0
- package/dist/esm/client/query/response-processor.d.ts +22 -0
- package/dist/esm/client/query/types.d.ts +52 -0
- package/dist/esm/client/query/url-builder.d.ts +71 -0
- package/dist/esm/client/query/url-builder.js +107 -0
- package/dist/esm/client/query/url-builder.js.map +1 -0
- package/dist/esm/client/query-builder.d.ts +1 -111
- package/dist/esm/client/record-builder.d.ts +56 -64
- package/dist/esm/client/record-builder.js +158 -297
- package/dist/esm/client/record-builder.js.map +1 -1
- package/dist/esm/client/response-processor.d.ts +3 -3
- package/dist/esm/client/update-builder.d.ts +17 -25
- package/dist/esm/client/update-builder.js +56 -30
- package/dist/esm/client/update-builder.js.map +1 -1
- package/dist/esm/errors.d.ts +8 -1
- package/dist/esm/errors.js +17 -0
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.d.ts +3 -7
- package/dist/esm/index.js +37 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/orm/column.d.ts +45 -0
- package/dist/esm/orm/column.js +59 -0
- package/dist/esm/orm/column.js.map +1 -0
- package/dist/esm/orm/field-builders.d.ts +154 -0
- package/dist/esm/orm/field-builders.js +152 -0
- package/dist/esm/orm/field-builders.js.map +1 -0
- package/dist/esm/orm/index.d.ts +4 -0
- package/dist/esm/orm/operators.d.ts +175 -0
- package/dist/esm/orm/operators.js +221 -0
- package/dist/esm/orm/operators.js.map +1 -0
- package/dist/esm/orm/table.d.ts +341 -0
- package/dist/esm/orm/table.js +211 -0
- package/dist/esm/orm/table.js.map +1 -0
- package/dist/esm/transform.d.ts +20 -21
- package/dist/esm/transform.js +34 -34
- package/dist/esm/transform.js.map +1 -1
- package/dist/esm/types.d.ts +73 -12
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/validation.d.ts +14 -4
- package/dist/esm/validation.js +45 -1
- package/dist/esm/validation.js.map +1 -1
- package/package.json +22 -17
- package/src/client/batch-builder.ts +102 -33
- package/src/client/builders/default-select.ts +69 -0
- package/src/client/builders/expand-builder.ts +236 -0
- package/src/client/builders/index.ts +11 -0
- package/src/client/builders/query-string-builder.ts +41 -0
- package/src/client/builders/response-processor.ts +273 -0
- package/src/client/builders/select-mixin.ts +74 -0
- package/src/client/builders/select-utils.ts +34 -0
- package/src/client/builders/shared-types.ts +41 -0
- package/src/client/builders/table-utils.ts +87 -0
- package/src/client/database.ts +19 -160
- package/src/client/delete-builder.ts +48 -52
- package/src/client/entity-set.ts +227 -302
- package/src/client/error-parser.ts +59 -0
- package/src/client/filemaker-odata.ts +3 -14
- package/src/client/insert-builder.ts +126 -44
- package/src/client/query/expand-builder.ts +164 -0
- package/src/client/query/index.ts +13 -0
- package/src/client/query/query-builder.ts +826 -0
- package/src/client/query/response-processor.ts +244 -0
- package/src/client/query/types.ts +102 -0
- package/src/client/query/url-builder.ts +179 -0
- package/src/client/query-builder.ts +8 -1454
- package/src/client/record-builder.ts +336 -586
- package/src/client/response-processor.ts +4 -5
- package/src/client/update-builder.ts +113 -75
- package/src/errors.ts +22 -1
- package/src/index.ts +58 -5
- package/src/orm/column.ts +78 -0
- package/src/orm/field-builders.ts +296 -0
- package/src/orm/index.ts +60 -0
- package/src/orm/operators.ts +428 -0
- package/src/orm/table.ts +759 -0
- package/src/transform.ts +62 -48
- package/src/types.ts +88 -63
- package/src/validation.ts +76 -4
- package/LICENSE.md +0 -21
- package/dist/esm/client/base-table.d.ts +0 -128
- package/dist/esm/client/base-table.js +0 -57
- package/dist/esm/client/base-table.js.map +0 -1
- package/dist/esm/client/build-occurrences.d.ts +0 -74
- package/dist/esm/client/build-occurrences.js +0 -31
- package/dist/esm/client/build-occurrences.js.map +0 -1
- package/dist/esm/client/query-builder.js +0 -900
- package/dist/esm/client/query-builder.js.map +0 -1
- package/dist/esm/client/table-occurrence.d.ts +0 -86
- package/dist/esm/client/table-occurrence.js +0 -58
- package/dist/esm/client/table-occurrence.js.map +0 -1
- package/src/client/base-table.ts +0 -178
- package/src/client/build-occurrences.ts +0 -155
- package/src/client/query-builder.ts.bak +0 -1457
- package/src/client/table-occurrence.ts +0 -156
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { QueryOptions } from "odata-query";
|
|
2
|
+
import buildQuery from "odata-query";
|
|
3
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
4
|
+
import { FMTable } from "../../orm/table";
|
|
5
|
+
import {
|
|
6
|
+
getBaseTableConfig,
|
|
7
|
+
getTableName,
|
|
8
|
+
getNavigationPaths,
|
|
9
|
+
} from "../../orm/table";
|
|
10
|
+
import type { ExpandValidationConfig } from "../../validation";
|
|
11
|
+
import type { ExpandConfig } from "./shared-types";
|
|
12
|
+
import { formatSelectFields } from "./select-utils";
|
|
13
|
+
import { getDefaultSelectFields } from "./default-select";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Builds OData expand query strings and validation configs.
|
|
17
|
+
* Handles nested expands recursively and transforms relation names to FMTIDs
|
|
18
|
+
* when using entity IDs.
|
|
19
|
+
*/
|
|
20
|
+
export class ExpandBuilder {
|
|
21
|
+
constructor(private useEntityIds: boolean) {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Builds OData $expand query string from expand configurations.
|
|
25
|
+
*/
|
|
26
|
+
buildExpandString(configs: ExpandConfig[]): string {
|
|
27
|
+
if (configs.length === 0) return "";
|
|
28
|
+
|
|
29
|
+
return configs.map((config) => this.buildSingleExpand(config)).join(",");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds validation configs for expanded navigation properties.
|
|
34
|
+
*/
|
|
35
|
+
buildValidationConfigs(configs: ExpandConfig[]): ExpandValidationConfig[] {
|
|
36
|
+
return configs.map((config) => {
|
|
37
|
+
const targetTable = config.targetTable;
|
|
38
|
+
|
|
39
|
+
let targetSchema: Record<string, StandardSchemaV1> | undefined;
|
|
40
|
+
if (targetTable) {
|
|
41
|
+
const baseTableConfig = getBaseTableConfig(targetTable);
|
|
42
|
+
const containerFields = baseTableConfig.containerFields || [];
|
|
43
|
+
|
|
44
|
+
// Filter out container fields from schema
|
|
45
|
+
const schema = { ...baseTableConfig.schema };
|
|
46
|
+
for (const containerField of containerFields) {
|
|
47
|
+
delete schema[containerField as string];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
targetSchema = schema;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const selectedFields = config.options?.select
|
|
54
|
+
? Array.isArray(config.options.select)
|
|
55
|
+
? config.options.select.map(String)
|
|
56
|
+
: [String(config.options.select)]
|
|
57
|
+
: undefined;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
relation: config.relation,
|
|
61
|
+
targetSchema,
|
|
62
|
+
targetTable,
|
|
63
|
+
table: targetTable,
|
|
64
|
+
selectedFields,
|
|
65
|
+
nestedExpands: undefined,
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Process an expand() call and return the expand config.
|
|
72
|
+
* Used by both QueryBuilder and RecordBuilder to eliminate duplication.
|
|
73
|
+
*
|
|
74
|
+
* @param targetTable - The target table to expand to
|
|
75
|
+
* @param sourceTable - The source table (for validation)
|
|
76
|
+
* @param callback - Optional callback to configure the expand query
|
|
77
|
+
* @param builderFactory - Function that creates a QueryBuilder for the target table
|
|
78
|
+
* @returns ExpandConfig to add to the builder's expandConfigs array
|
|
79
|
+
*/
|
|
80
|
+
processExpand<TargetTable extends FMTable<any, any>, Builder = any>(
|
|
81
|
+
targetTable: TargetTable,
|
|
82
|
+
sourceTable: FMTable<any, any> | undefined,
|
|
83
|
+
callback?: (builder: Builder) => Builder,
|
|
84
|
+
builderFactory?: () => Builder,
|
|
85
|
+
): ExpandConfig {
|
|
86
|
+
// Extract name and validate
|
|
87
|
+
const relationName = getTableName(targetTable);
|
|
88
|
+
|
|
89
|
+
// Runtime validation: Check if relation name is in navigationPaths
|
|
90
|
+
if (sourceTable) {
|
|
91
|
+
const navigationPaths = getNavigationPaths(sourceTable);
|
|
92
|
+
if (navigationPaths && !navigationPaths.includes(relationName)) {
|
|
93
|
+
console.warn(
|
|
94
|
+
`Cannot expand to "${relationName}". Valid navigation paths: ${navigationPaths.length > 0 ? navigationPaths.join(", ") : "none"}`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (callback && builderFactory) {
|
|
100
|
+
// Create a new QueryBuilder for the target table
|
|
101
|
+
const targetBuilder = builderFactory();
|
|
102
|
+
|
|
103
|
+
// Pass to callback and get configured builder
|
|
104
|
+
const configuredBuilder = callback(targetBuilder);
|
|
105
|
+
|
|
106
|
+
// Extract the builder's query options
|
|
107
|
+
const expandOptions: Partial<QueryOptions<any>> = {
|
|
108
|
+
...(configuredBuilder as any).queryOptions,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// If callback didn't provide select, apply defaultSelect from target table
|
|
112
|
+
if (!expandOptions.select) {
|
|
113
|
+
const defaultFields = getDefaultSelectFields(targetTable);
|
|
114
|
+
if (defaultFields) {
|
|
115
|
+
expandOptions.select = defaultFields;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If the configured builder has nested expands, we need to include them
|
|
120
|
+
if ((configuredBuilder as any).expandConfigs?.length > 0) {
|
|
121
|
+
// Build nested expand string from the configured builder's expand configs
|
|
122
|
+
const nestedExpandString = this.buildExpandString(
|
|
123
|
+
(configuredBuilder as any).expandConfigs,
|
|
124
|
+
);
|
|
125
|
+
if (nestedExpandString) {
|
|
126
|
+
// Add nested expand to options
|
|
127
|
+
expandOptions.expand = nestedExpandString as any;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
relation: relationName,
|
|
133
|
+
options: expandOptions,
|
|
134
|
+
targetTable,
|
|
135
|
+
};
|
|
136
|
+
} else {
|
|
137
|
+
// Simple expand without callback - apply defaultSelect if available
|
|
138
|
+
const defaultFields = getDefaultSelectFields(targetTable);
|
|
139
|
+
if (defaultFields) {
|
|
140
|
+
return {
|
|
141
|
+
relation: relationName,
|
|
142
|
+
options: { select: defaultFields },
|
|
143
|
+
targetTable,
|
|
144
|
+
};
|
|
145
|
+
} else {
|
|
146
|
+
return {
|
|
147
|
+
relation: relationName,
|
|
148
|
+
targetTable,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Builds a single expand string with its options.
|
|
156
|
+
*/
|
|
157
|
+
private buildSingleExpand(config: ExpandConfig): string {
|
|
158
|
+
const relationName = this.resolveRelationName(config);
|
|
159
|
+
const parts = this.buildExpandParts(config);
|
|
160
|
+
|
|
161
|
+
if (parts.length === 0) {
|
|
162
|
+
return relationName;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return `${relationName}(${parts.join(";")})`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Resolves relation name, using FMTID if entity IDs are enabled.
|
|
170
|
+
*/
|
|
171
|
+
private resolveRelationName(config: ExpandConfig): string {
|
|
172
|
+
if (!this.useEntityIds) {
|
|
173
|
+
return config.relation;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const targetTable = config.targetTable;
|
|
177
|
+
if (targetTable && FMTable.Symbol.EntityId in targetTable) {
|
|
178
|
+
const tableId = (targetTable as any)[FMTable.Symbol.EntityId] as
|
|
179
|
+
| `FMTID:${string}`
|
|
180
|
+
| undefined;
|
|
181
|
+
if (tableId) {
|
|
182
|
+
return tableId;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return config.relation;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Builds expand parts (select, filter, orderBy, etc.) for a single expand.
|
|
191
|
+
*/
|
|
192
|
+
private buildExpandParts(config: ExpandConfig): string[] {
|
|
193
|
+
if (!config.options || Object.keys(config.options).length === 0) {
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const parts: string[] = [];
|
|
198
|
+
const opts = config.options;
|
|
199
|
+
|
|
200
|
+
if (opts.select) {
|
|
201
|
+
const selectArray = Array.isArray(opts.select)
|
|
202
|
+
? opts.select.map(String)
|
|
203
|
+
: [String(opts.select)];
|
|
204
|
+
const selectFields = formatSelectFields(
|
|
205
|
+
selectArray,
|
|
206
|
+
config.targetTable,
|
|
207
|
+
this.useEntityIds,
|
|
208
|
+
);
|
|
209
|
+
parts.push(`$select=${selectFields}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (opts.filter) {
|
|
213
|
+
const filterQuery = buildQuery({ filter: opts.filter });
|
|
214
|
+
const match = filterQuery.match(/\$filter=([^&]+)/);
|
|
215
|
+
if (match) parts.push(`$filter=${match[1]}`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (opts.orderBy) {
|
|
219
|
+
const orderByValue = Array.isArray(opts.orderBy)
|
|
220
|
+
? opts.orderBy.join(",")
|
|
221
|
+
: String(opts.orderBy);
|
|
222
|
+
parts.push(`$orderby=${orderByValue}`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (opts.top !== undefined) parts.push(`$top=${opts.top}`);
|
|
226
|
+
if (opts.skip !== undefined) parts.push(`$skip=${opts.skip}`);
|
|
227
|
+
|
|
228
|
+
if (opts.expand) {
|
|
229
|
+
if (typeof opts.expand === "string") {
|
|
230
|
+
parts.push(`$expand=${opts.expand}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return parts;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Re-export all shared builder utilities
|
|
2
|
+
export * from "./shared-types";
|
|
3
|
+
export * from "./table-utils";
|
|
4
|
+
export * from "./select-utils";
|
|
5
|
+
export * from "./select-mixin";
|
|
6
|
+
export * from "./expand-builder";
|
|
7
|
+
export * from "./response-processor";
|
|
8
|
+
export * from "./default-select";
|
|
9
|
+
export * from "./query-string-builder";
|
|
10
|
+
|
|
11
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { FMTable } from "../../orm/table";
|
|
2
|
+
import { ExpandBuilder } from "./expand-builder";
|
|
3
|
+
import type { ExpandConfig } from "./shared-types";
|
|
4
|
+
import { formatSelectFields } from "./select-utils";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Builds OData query string for $select and $expand parameters.
|
|
8
|
+
* Used by both QueryBuilder and RecordBuilder to eliminate duplication.
|
|
9
|
+
*
|
|
10
|
+
* @param config - Configuration object
|
|
11
|
+
* @returns Query string starting with ? or empty string if no parameters
|
|
12
|
+
*/
|
|
13
|
+
export function buildSelectExpandQueryString(config: {
|
|
14
|
+
selectedFields?: string[];
|
|
15
|
+
expandConfigs: ExpandConfig[];
|
|
16
|
+
table?: FMTable<any, any>;
|
|
17
|
+
useEntityIds: boolean;
|
|
18
|
+
}): string {
|
|
19
|
+
const parts: string[] = [];
|
|
20
|
+
const expandBuilder = new ExpandBuilder(config.useEntityIds);
|
|
21
|
+
|
|
22
|
+
// Build $select
|
|
23
|
+
if (config.selectedFields && config.selectedFields.length > 0) {
|
|
24
|
+
const selectString = formatSelectFields(
|
|
25
|
+
config.selectedFields,
|
|
26
|
+
config.table,
|
|
27
|
+
config.useEntityIds,
|
|
28
|
+
);
|
|
29
|
+
if (selectString) {
|
|
30
|
+
parts.push(`$select=${selectString}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Build $expand
|
|
35
|
+
const expandString = expandBuilder.buildExpandString(config.expandConfigs);
|
|
36
|
+
if (expandString) {
|
|
37
|
+
parts.push(`$expand=${expandString}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return parts.length > 0 ? `?${parts.join("&")}` : "";
|
|
41
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import type { FMTable } from "../../orm/table";
|
|
2
|
+
import type { Result } from "../../types";
|
|
3
|
+
import type { ExpandValidationConfig } from "../../validation";
|
|
4
|
+
import { validateSingleResponse, validateListResponse } from "../../validation";
|
|
5
|
+
import { transformResponseFields } from "../../transform";
|
|
6
|
+
import { RecordCountMismatchError } from "../../errors";
|
|
7
|
+
import { getBaseTableConfig } from "../../orm/table";
|
|
8
|
+
import { ExpandBuilder } from "./expand-builder";
|
|
9
|
+
import type { ExpandConfig } from "./shared-types";
|
|
10
|
+
|
|
11
|
+
export interface ProcessResponseConfig {
|
|
12
|
+
table?: FMTable<any, any>;
|
|
13
|
+
schema?: Record<string, any>;
|
|
14
|
+
singleMode: "exact" | "maybe" | false;
|
|
15
|
+
selectedFields?: string[];
|
|
16
|
+
expandValidationConfigs?: ExpandValidationConfig[];
|
|
17
|
+
skipValidation?: boolean;
|
|
18
|
+
useEntityIds?: boolean;
|
|
19
|
+
// Mapping from field names to output keys (for renamed fields in select)
|
|
20
|
+
fieldMapping?: Record<string, string>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Processes OData response with transformation and validation.
|
|
25
|
+
* Shared by QueryBuilder and RecordBuilder.
|
|
26
|
+
*/
|
|
27
|
+
export async function processODataResponse<T>(
|
|
28
|
+
rawResponse: any,
|
|
29
|
+
config: ProcessResponseConfig,
|
|
30
|
+
): Promise<Result<T>> {
|
|
31
|
+
const {
|
|
32
|
+
table,
|
|
33
|
+
schema,
|
|
34
|
+
singleMode,
|
|
35
|
+
selectedFields,
|
|
36
|
+
expandValidationConfigs,
|
|
37
|
+
skipValidation,
|
|
38
|
+
useEntityIds,
|
|
39
|
+
fieldMapping,
|
|
40
|
+
} = config;
|
|
41
|
+
|
|
42
|
+
// Transform field IDs back to names if using entity IDs
|
|
43
|
+
let response = rawResponse;
|
|
44
|
+
if (table && useEntityIds) {
|
|
45
|
+
response = transformResponseFields(
|
|
46
|
+
response,
|
|
47
|
+
table,
|
|
48
|
+
expandValidationConfigs,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Fast path: skip validation
|
|
53
|
+
if (skipValidation) {
|
|
54
|
+
const result = extractRecords(response, singleMode);
|
|
55
|
+
// Rename fields AFTER extraction (but before returning)
|
|
56
|
+
if (result.data && fieldMapping && Object.keys(fieldMapping).length > 0) {
|
|
57
|
+
if (result.error) {
|
|
58
|
+
return { data: undefined, error: result.error } as Result<T>;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
data: renameFieldsInResponse(result.data, fieldMapping) as T,
|
|
62
|
+
error: undefined,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return result as Result<T>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Validation path
|
|
69
|
+
if (singleMode !== false) {
|
|
70
|
+
const validation = await validateSingleResponse<any>(
|
|
71
|
+
response,
|
|
72
|
+
schema,
|
|
73
|
+
selectedFields as any,
|
|
74
|
+
expandValidationConfigs,
|
|
75
|
+
singleMode,
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
if (!validation.valid) {
|
|
79
|
+
return { data: undefined, error: validation.error };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Rename fields AFTER validation completes
|
|
83
|
+
if (fieldMapping && Object.keys(fieldMapping).length > 0) {
|
|
84
|
+
return {
|
|
85
|
+
data: renameFieldsInResponse(validation.data, fieldMapping) as T,
|
|
86
|
+
error: undefined,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { data: validation.data as T, error: undefined };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const validation = await validateListResponse<any>(
|
|
94
|
+
response,
|
|
95
|
+
schema,
|
|
96
|
+
selectedFields as any,
|
|
97
|
+
expandValidationConfigs,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (!validation.valid) {
|
|
101
|
+
return { data: undefined, error: validation.error };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Rename fields AFTER validation completes
|
|
105
|
+
if (fieldMapping && Object.keys(fieldMapping).length > 0) {
|
|
106
|
+
return {
|
|
107
|
+
data: renameFieldsInResponse(validation.data, fieldMapping) as T,
|
|
108
|
+
error: undefined,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { data: validation.data as T, error: undefined };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Extracts records from response without validation.
|
|
117
|
+
*/
|
|
118
|
+
function extractRecords<T>(
|
|
119
|
+
response: any,
|
|
120
|
+
singleMode: "exact" | "maybe" | false,
|
|
121
|
+
): Result<T> {
|
|
122
|
+
if (singleMode === false) {
|
|
123
|
+
const records = response.value ?? [];
|
|
124
|
+
return { data: records as T, error: undefined };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const records = response.value ?? [response];
|
|
128
|
+
const count = Array.isArray(records) ? records.length : 1;
|
|
129
|
+
|
|
130
|
+
if (count > 1) {
|
|
131
|
+
return {
|
|
132
|
+
data: undefined,
|
|
133
|
+
error: new RecordCountMismatchError(
|
|
134
|
+
singleMode === "exact" ? "one" : "at-most-one",
|
|
135
|
+
count,
|
|
136
|
+
),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (count === 0) {
|
|
141
|
+
if (singleMode === "exact") {
|
|
142
|
+
return { data: undefined, error: new RecordCountMismatchError("one", 0) };
|
|
143
|
+
}
|
|
144
|
+
return { data: null as T, error: undefined };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const record = Array.isArray(records) ? records[0] : records;
|
|
148
|
+
return { data: record as T, error: undefined };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Gets schema from a table occurrence, excluding container fields.
|
|
153
|
+
* Container fields are never returned in regular responses (only via getSingleField).
|
|
154
|
+
*/
|
|
155
|
+
export function getSchemaFromTable(
|
|
156
|
+
table: FMTable<any, any> | undefined,
|
|
157
|
+
): Record<string, any> | undefined {
|
|
158
|
+
if (!table) return undefined;
|
|
159
|
+
const baseTableConfig = getBaseTableConfig(table);
|
|
160
|
+
const containerFields = baseTableConfig.containerFields || [];
|
|
161
|
+
|
|
162
|
+
// Filter out container fields from schema
|
|
163
|
+
const schema = { ...baseTableConfig.schema };
|
|
164
|
+
for (const containerField of containerFields) {
|
|
165
|
+
delete schema[containerField as string];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return schema;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Renames fields in response data according to the field mapping.
|
|
173
|
+
* Used when select() is called with renamed fields (e.g., { userEmail: users.email }).
|
|
174
|
+
*/
|
|
175
|
+
function renameFieldsInResponse(
|
|
176
|
+
data: any,
|
|
177
|
+
fieldMapping: Record<string, string>,
|
|
178
|
+
): any {
|
|
179
|
+
if (!data || typeof data !== "object") {
|
|
180
|
+
return data;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Handle array responses
|
|
184
|
+
if (Array.isArray(data)) {
|
|
185
|
+
return data.map((item) => renameFieldsInResponse(item, fieldMapping));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Handle OData list response structure
|
|
189
|
+
if ("value" in data && Array.isArray(data.value)) {
|
|
190
|
+
return {
|
|
191
|
+
...data,
|
|
192
|
+
value: data.value.map((item: any) =>
|
|
193
|
+
renameFieldsInResponse(item, fieldMapping),
|
|
194
|
+
),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Handle single record
|
|
199
|
+
const renamed: Record<string, any> = {};
|
|
200
|
+
for (const [key, value] of Object.entries(data)) {
|
|
201
|
+
// Check if this field should be renamed
|
|
202
|
+
const outputKey = fieldMapping[key];
|
|
203
|
+
if (outputKey) {
|
|
204
|
+
renamed[outputKey] = value;
|
|
205
|
+
} else {
|
|
206
|
+
renamed[key] = value;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return renamed;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Processes query response with expand configs.
|
|
214
|
+
* This is a convenience wrapper that builds validation configs from expand configs.
|
|
215
|
+
*/
|
|
216
|
+
export async function processQueryResponse<T>(
|
|
217
|
+
response: any,
|
|
218
|
+
config: {
|
|
219
|
+
occurrence?: FMTable<any, any>;
|
|
220
|
+
singleMode: "exact" | "maybe" | false;
|
|
221
|
+
queryOptions: { select?: (keyof T)[] | string[] };
|
|
222
|
+
expandConfigs: ExpandConfig[];
|
|
223
|
+
skipValidation?: boolean;
|
|
224
|
+
useEntityIds?: boolean;
|
|
225
|
+
// Mapping from field names to output keys (for renamed fields in select)
|
|
226
|
+
fieldMapping?: Record<string, string>;
|
|
227
|
+
},
|
|
228
|
+
): Promise<Result<any>> {
|
|
229
|
+
const {
|
|
230
|
+
occurrence,
|
|
231
|
+
singleMode,
|
|
232
|
+
queryOptions,
|
|
233
|
+
expandConfigs,
|
|
234
|
+
skipValidation,
|
|
235
|
+
useEntityIds,
|
|
236
|
+
fieldMapping,
|
|
237
|
+
} = config;
|
|
238
|
+
|
|
239
|
+
const expandBuilder = new ExpandBuilder(useEntityIds ?? false);
|
|
240
|
+
const expandValidationConfigs =
|
|
241
|
+
expandBuilder.buildValidationConfigs(expandConfigs);
|
|
242
|
+
|
|
243
|
+
const selectedFields = queryOptions.select
|
|
244
|
+
? Array.isArray(queryOptions.select)
|
|
245
|
+
? queryOptions.select.map(String)
|
|
246
|
+
: [String(queryOptions.select)]
|
|
247
|
+
: undefined;
|
|
248
|
+
|
|
249
|
+
// Process the response first
|
|
250
|
+
let processedResponse = await processODataResponse(response, {
|
|
251
|
+
table: occurrence,
|
|
252
|
+
schema: getSchemaFromTable(occurrence),
|
|
253
|
+
singleMode,
|
|
254
|
+
selectedFields,
|
|
255
|
+
expandValidationConfigs,
|
|
256
|
+
skipValidation,
|
|
257
|
+
useEntityIds,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Rename fields if field mapping is provided (for renamed fields in select)
|
|
261
|
+
if (
|
|
262
|
+
processedResponse.data &&
|
|
263
|
+
fieldMapping &&
|
|
264
|
+
Object.keys(fieldMapping).length > 0
|
|
265
|
+
) {
|
|
266
|
+
processedResponse = {
|
|
267
|
+
...processedResponse,
|
|
268
|
+
data: renameFieldsInResponse(processedResponse.data, fieldMapping),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return processedResponse;
|
|
273
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { isColumn, type Column } from "../../orm/column";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility function for processing select() calls.
|
|
5
|
+
* Used by both QueryBuilder and RecordBuilder to eliminate duplication.
|
|
6
|
+
*
|
|
7
|
+
* @param fields - Field names or Column references
|
|
8
|
+
* @returns Object with selectedFields array
|
|
9
|
+
*/
|
|
10
|
+
export function processSelectFields(
|
|
11
|
+
...fields: (string | Column<any, string>)[]
|
|
12
|
+
): { selectedFields: string[] } {
|
|
13
|
+
const fieldNames = fields.map((field) => {
|
|
14
|
+
if (isColumn(field)) {
|
|
15
|
+
return field.fieldName as string;
|
|
16
|
+
}
|
|
17
|
+
return String(field);
|
|
18
|
+
});
|
|
19
|
+
return { selectedFields: [...new Set(fieldNames)] };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Processes select() calls with field renaming support.
|
|
24
|
+
* Validates columns belong to the correct table and builds field mapping for renamed fields.
|
|
25
|
+
* Used by both QueryBuilder and RecordBuilder to eliminate duplication.
|
|
26
|
+
*
|
|
27
|
+
* @param fields - Object mapping output keys to column references
|
|
28
|
+
* @param tableName - Expected table name for validation
|
|
29
|
+
* @returns Object with selectedFields array and fieldMapping for renamed fields
|
|
30
|
+
*/
|
|
31
|
+
export function processSelectWithRenames<TTableName extends string>(
|
|
32
|
+
fields: Record<string, Column<any, TTableName>>,
|
|
33
|
+
tableName: string,
|
|
34
|
+
): { selectedFields: string[]; fieldMapping: Record<string, string> } {
|
|
35
|
+
const selectedFields: string[] = [];
|
|
36
|
+
const fieldMapping: Record<string, string> = {};
|
|
37
|
+
|
|
38
|
+
for (const [outputKey, column] of Object.entries(fields)) {
|
|
39
|
+
if (!isColumn(column)) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`select() expects column references, but got: ${typeof column}`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Warn (not throw) on table mismatch for consistency
|
|
46
|
+
if (column.tableName !== tableName) {
|
|
47
|
+
console.warn(
|
|
48
|
+
`Column ${column.toString()} is from table "${column.tableName}", but query is for table "${tableName}"`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fieldName = column.fieldName;
|
|
53
|
+
selectedFields.push(fieldName);
|
|
54
|
+
|
|
55
|
+
// Build mapping from field name to output key (only if renamed)
|
|
56
|
+
if (fieldName !== outputKey) {
|
|
57
|
+
fieldMapping[fieldName] = outputKey;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
selectedFields,
|
|
63
|
+
fieldMapping: Object.keys(fieldMapping).length > 0 ? fieldMapping : {},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Legacy class name for backward compatibility.
|
|
69
|
+
* @deprecated Use processSelectFields function instead
|
|
70
|
+
*/
|
|
71
|
+
export class SelectMixin {
|
|
72
|
+
static processSelect = processSelectFields;
|
|
73
|
+
}
|
|
74
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { FMTable } from "../../orm/table";
|
|
2
|
+
import { transformFieldNamesArray } from "../../transform";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats select fields for use in OData query strings.
|
|
6
|
+
* - Transforms field names to FMFIDs if using entity IDs
|
|
7
|
+
* - Wraps "id" fields in double quotes (OData reserved)
|
|
8
|
+
* - URL-encodes special characters but preserves spaces
|
|
9
|
+
*/
|
|
10
|
+
export function formatSelectFields(
|
|
11
|
+
select: string[] | readonly string[] | undefined,
|
|
12
|
+
table?: FMTable<any, any>,
|
|
13
|
+
useEntityIds?: boolean,
|
|
14
|
+
): string {
|
|
15
|
+
if (!select || select.length === 0) return "";
|
|
16
|
+
|
|
17
|
+
const selectArray = Array.isArray(select) ? select : [select];
|
|
18
|
+
|
|
19
|
+
// Transform to field IDs if using entity IDs
|
|
20
|
+
const transformedFields =
|
|
21
|
+
table && useEntityIds
|
|
22
|
+
? transformFieldNamesArray(selectArray.map(String), table)
|
|
23
|
+
: selectArray.map(String);
|
|
24
|
+
|
|
25
|
+
return transformedFields
|
|
26
|
+
.map((field) => {
|
|
27
|
+
if (field === "id") return `"id"`;
|
|
28
|
+
const encoded = encodeURIComponent(field);
|
|
29
|
+
return encoded.replace(/%20/g, " ");
|
|
30
|
+
})
|
|
31
|
+
.join(",");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|