@proofkit/fmodata 0.1.0-alpha.8 → 0.1.0-beta.23

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 (163) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +651 -449
  3. package/dist/esm/client/batch-builder.d.ts +10 -9
  4. package/dist/esm/client/batch-builder.js +119 -56
  5. package/dist/esm/client/batch-builder.js.map +1 -1
  6. package/dist/esm/client/batch-request.js +16 -21
  7. package/dist/esm/client/batch-request.js.map +1 -1
  8. package/dist/esm/client/builders/default-select.d.ts +10 -0
  9. package/dist/esm/client/builders/default-select.js +41 -0
  10. package/dist/esm/client/builders/default-select.js.map +1 -0
  11. package/dist/esm/client/builders/expand-builder.d.ts +45 -0
  12. package/dist/esm/client/builders/expand-builder.js +185 -0
  13. package/dist/esm/client/builders/expand-builder.js.map +1 -0
  14. package/dist/esm/client/builders/index.d.ts +9 -0
  15. package/dist/esm/client/builders/query-string-builder.d.ts +18 -0
  16. package/dist/esm/client/builders/query-string-builder.js +21 -0
  17. package/dist/esm/client/builders/query-string-builder.js.map +1 -0
  18. package/dist/esm/client/builders/response-processor.d.ts +43 -0
  19. package/dist/esm/client/builders/response-processor.js +175 -0
  20. package/dist/esm/client/builders/response-processor.js.map +1 -0
  21. package/dist/esm/client/builders/select-mixin.d.ts +25 -0
  22. package/dist/esm/client/builders/select-mixin.js +28 -0
  23. package/dist/esm/client/builders/select-mixin.js.map +1 -0
  24. package/dist/esm/client/builders/select-utils.d.ts +18 -0
  25. package/dist/esm/client/builders/select-utils.js +30 -0
  26. package/dist/esm/client/builders/select-utils.js.map +1 -0
  27. package/dist/esm/client/builders/shared-types.d.ts +40 -0
  28. package/dist/esm/client/builders/table-utils.d.ts +35 -0
  29. package/dist/esm/client/builders/table-utils.js +44 -0
  30. package/dist/esm/client/builders/table-utils.js.map +1 -0
  31. package/dist/esm/client/database.d.ts +34 -22
  32. package/dist/esm/client/database.js +48 -84
  33. package/dist/esm/client/database.js.map +1 -1
  34. package/dist/esm/client/delete-builder.d.ts +25 -30
  35. package/dist/esm/client/delete-builder.js +45 -30
  36. package/dist/esm/client/delete-builder.js.map +1 -1
  37. package/dist/esm/client/entity-set.d.ts +35 -43
  38. package/dist/esm/client/entity-set.js +110 -52
  39. package/dist/esm/client/entity-set.js.map +1 -1
  40. package/dist/esm/client/error-parser.d.ts +12 -0
  41. package/dist/esm/client/error-parser.js +25 -0
  42. package/dist/esm/client/error-parser.js.map +1 -0
  43. package/dist/esm/client/filemaker-odata.d.ts +26 -7
  44. package/dist/esm/client/filemaker-odata.js +65 -42
  45. package/dist/esm/client/filemaker-odata.js.map +1 -1
  46. package/dist/esm/client/insert-builder.d.ts +19 -24
  47. package/dist/esm/client/insert-builder.js +94 -58
  48. package/dist/esm/client/insert-builder.js.map +1 -1
  49. package/dist/esm/client/query/expand-builder.d.ts +35 -0
  50. package/dist/esm/client/query/index.d.ts +4 -0
  51. package/dist/esm/client/query/query-builder.d.ts +132 -0
  52. package/dist/esm/client/query/query-builder.js +456 -0
  53. package/dist/esm/client/query/query-builder.js.map +1 -0
  54. package/dist/esm/client/query/response-processor.d.ts +25 -0
  55. package/dist/esm/client/query/types.d.ts +77 -0
  56. package/dist/esm/client/query/url-builder.d.ts +71 -0
  57. package/dist/esm/client/query/url-builder.js +100 -0
  58. package/dist/esm/client/query/url-builder.js.map +1 -0
  59. package/dist/esm/client/query-builder.d.ts +2 -115
  60. package/dist/esm/client/record-builder.d.ts +108 -36
  61. package/dist/esm/client/record-builder.js +284 -119
  62. package/dist/esm/client/record-builder.js.map +1 -1
  63. package/dist/esm/client/response-processor.d.ts +4 -9
  64. package/dist/esm/client/sanitize-json.d.ts +35 -0
  65. package/dist/esm/client/sanitize-json.js +27 -0
  66. package/dist/esm/client/sanitize-json.js.map +1 -0
  67. package/dist/esm/client/schema-manager.d.ts +5 -5
  68. package/dist/esm/client/schema-manager.js +45 -31
  69. package/dist/esm/client/schema-manager.js.map +1 -1
  70. package/dist/esm/client/update-builder.d.ts +34 -40
  71. package/dist/esm/client/update-builder.js +99 -58
  72. package/dist/esm/client/update-builder.js.map +1 -1
  73. package/dist/esm/client/webhook-builder.d.ts +126 -0
  74. package/dist/esm/client/webhook-builder.js +189 -0
  75. package/dist/esm/client/webhook-builder.js.map +1 -0
  76. package/dist/esm/errors.d.ts +19 -2
  77. package/dist/esm/errors.js +39 -4
  78. package/dist/esm/errors.js.map +1 -1
  79. package/dist/esm/index.d.ts +10 -8
  80. package/dist/esm/index.js +40 -10
  81. package/dist/esm/index.js.map +1 -1
  82. package/dist/esm/logger.d.ts +47 -0
  83. package/dist/esm/logger.js +69 -0
  84. package/dist/esm/logger.js.map +1 -0
  85. package/dist/esm/logger.test.d.ts +1 -0
  86. package/dist/esm/orm/column.d.ts +62 -0
  87. package/dist/esm/orm/column.js +63 -0
  88. package/dist/esm/orm/column.js.map +1 -0
  89. package/dist/esm/orm/field-builders.d.ts +164 -0
  90. package/dist/esm/orm/field-builders.js +158 -0
  91. package/dist/esm/orm/field-builders.js.map +1 -0
  92. package/dist/esm/orm/index.d.ts +5 -0
  93. package/dist/esm/orm/operators.d.ts +173 -0
  94. package/dist/esm/orm/operators.js +260 -0
  95. package/dist/esm/orm/operators.js.map +1 -0
  96. package/dist/esm/orm/table.d.ts +355 -0
  97. package/dist/esm/orm/table.js +202 -0
  98. package/dist/esm/orm/table.js.map +1 -0
  99. package/dist/esm/transform.d.ts +20 -21
  100. package/dist/esm/transform.js +44 -45
  101. package/dist/esm/transform.js.map +1 -1
  102. package/dist/esm/types.d.ts +96 -30
  103. package/dist/esm/types.js +7 -0
  104. package/dist/esm/types.js.map +1 -0
  105. package/dist/esm/validation.d.ts +22 -12
  106. package/dist/esm/validation.js +132 -85
  107. package/dist/esm/validation.js.map +1 -1
  108. package/package.json +28 -20
  109. package/src/client/batch-builder.ts +153 -89
  110. package/src/client/batch-request.ts +25 -41
  111. package/src/client/builders/default-select.ts +75 -0
  112. package/src/client/builders/expand-builder.ts +246 -0
  113. package/src/client/builders/index.ts +11 -0
  114. package/src/client/builders/query-string-builder.ts +46 -0
  115. package/src/client/builders/response-processor.ts +279 -0
  116. package/src/client/builders/select-mixin.ts +65 -0
  117. package/src/client/builders/select-utils.ts +59 -0
  118. package/src/client/builders/shared-types.ts +45 -0
  119. package/src/client/builders/table-utils.ts +83 -0
  120. package/src/client/database.ts +89 -183
  121. package/src/client/delete-builder.ts +74 -84
  122. package/src/client/entity-set.ts +266 -293
  123. package/src/client/error-parser.ts +41 -0
  124. package/src/client/filemaker-odata.ts +98 -66
  125. package/src/client/insert-builder.ts +157 -118
  126. package/src/client/query/expand-builder.ts +160 -0
  127. package/src/client/query/index.ts +14 -0
  128. package/src/client/query/query-builder.ts +729 -0
  129. package/src/client/query/response-processor.ts +226 -0
  130. package/src/client/query/types.ts +126 -0
  131. package/src/client/query/url-builder.ts +151 -0
  132. package/src/client/query-builder.ts +10 -1455
  133. package/src/client/record-builder.ts +575 -240
  134. package/src/client/response-processor.ts +15 -42
  135. package/src/client/sanitize-json.ts +64 -0
  136. package/src/client/schema-manager.ts +61 -76
  137. package/src/client/update-builder.ts +161 -143
  138. package/src/client/webhook-builder.ts +265 -0
  139. package/src/errors.ts +49 -16
  140. package/src/index.ts +99 -54
  141. package/src/logger.test.ts +34 -0
  142. package/src/logger.ts +116 -0
  143. package/src/orm/column.ts +106 -0
  144. package/src/orm/field-builders.ts +250 -0
  145. package/src/orm/index.ts +61 -0
  146. package/src/orm/operators.ts +473 -0
  147. package/src/orm/table.ts +741 -0
  148. package/src/transform.ts +90 -70
  149. package/src/types.ts +154 -113
  150. package/src/validation.ts +200 -115
  151. package/dist/esm/client/base-table.d.ts +0 -125
  152. package/dist/esm/client/base-table.js +0 -57
  153. package/dist/esm/client/base-table.js.map +0 -1
  154. package/dist/esm/client/query-builder.js +0 -896
  155. package/dist/esm/client/query-builder.js.map +0 -1
  156. package/dist/esm/client/table-occurrence.d.ts +0 -72
  157. package/dist/esm/client/table-occurrence.js +0 -74
  158. package/dist/esm/client/table-occurrence.js.map +0 -1
  159. package/dist/esm/filter-types.d.ts +0 -76
  160. package/src/client/base-table.ts +0 -166
  161. package/src/client/query-builder.ts.bak +0 -1457
  162. package/src/client/table-occurrence.ts +0 -175
  163. package/src/filter-types.ts +0 -97
@@ -1,896 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import buildQuery from "odata-query";
5
- import { validateSingleResponse, validateListResponse } from "../validation.js";
6
- import { RecordCountMismatchError } from "../errors.js";
7
- import { getTableIdentifiers, transformFieldName, transformOrderByField, transformFieldNamesArray, transformResponseFields } from "../transform.js";
8
- class QueryBuilder {
9
- constructor(config) {
10
- __publicField(this, "queryOptions", {});
11
- __publicField(this, "expandConfigs", []);
12
- __publicField(this, "singleMode", false);
13
- __publicField(this, "isCountMode", false);
14
- __publicField(this, "occurrence");
15
- __publicField(this, "tableName");
16
- __publicField(this, "databaseName");
17
- __publicField(this, "context");
18
- __publicField(this, "isNavigate");
19
- __publicField(this, "navigateRecordId");
20
- __publicField(this, "navigateRelation");
21
- __publicField(this, "navigateSourceTableName");
22
- __publicField(this, "navigateBaseRelation");
23
- __publicField(this, "databaseUseEntityIds");
24
- this.occurrence = config.occurrence;
25
- this.tableName = config.tableName;
26
- this.databaseName = config.databaseName;
27
- this.context = config.context;
28
- this.databaseUseEntityIds = config.databaseUseEntityIds ?? false;
29
- }
30
- /**
31
- * Helper to merge database-level useEntityIds with per-request options
32
- */
33
- mergeExecuteOptions(options) {
34
- return {
35
- ...options,
36
- useEntityIds: (options == null ? void 0 : options.useEntityIds) === void 0 ? this.databaseUseEntityIds : options.useEntityIds
37
- };
38
- }
39
- /**
40
- * Helper to conditionally strip OData annotations based on options
41
- */
42
- stripODataAnnotationsIfNeeded(data, options) {
43
- if ((options == null ? void 0 : options.includeODataAnnotations) === true) {
44
- return data;
45
- }
46
- const { "@id": _id, "@editLink": _editLink, ...rest } = data;
47
- return rest;
48
- }
49
- /**
50
- * Gets the table ID (FMTID) if using entity IDs, otherwise returns the table name
51
- * @param useEntityIds - Optional override for entity ID usage
52
- */
53
- getTableId(useEntityIds) {
54
- var _a, _b;
55
- if (!this.occurrence) {
56
- return this.tableName;
57
- }
58
- const contextDefault = ((_b = (_a = this.context)._getUseEntityIds) == null ? void 0 : _b.call(_a)) ?? false;
59
- const shouldUseIds = useEntityIds ?? contextDefault;
60
- if (shouldUseIds) {
61
- const identifiers = getTableIdentifiers(this.occurrence);
62
- if (!identifiers.id) {
63
- throw new Error(
64
- `useEntityIds is true but TableOccurrence "${identifiers.name}" does not have an fmtId defined`
65
- );
66
- }
67
- return identifiers.id;
68
- }
69
- return this.occurrence.getTableName();
70
- }
71
- select(...fields) {
72
- const uniqueFields = [...new Set(fields)];
73
- const newBuilder = new QueryBuilder({
74
- occurrence: this.occurrence,
75
- tableName: this.tableName,
76
- databaseName: this.databaseName,
77
- context: this.context,
78
- databaseUseEntityIds: this.databaseUseEntityIds
79
- });
80
- newBuilder.queryOptions = {
81
- ...this.queryOptions,
82
- select: uniqueFields
83
- };
84
- newBuilder.expandConfigs = [...this.expandConfigs];
85
- newBuilder.singleMode = this.singleMode;
86
- newBuilder.isCountMode = this.isCountMode;
87
- newBuilder.isNavigate = this.isNavigate;
88
- newBuilder.navigateRecordId = this.navigateRecordId;
89
- newBuilder.navigateRelation = this.navigateRelation;
90
- newBuilder.navigateSourceTableName = this.navigateSourceTableName;
91
- newBuilder.navigateBaseRelation = this.navigateBaseRelation;
92
- return newBuilder;
93
- }
94
- /**
95
- * Transforms our filter format to odata-query's expected format
96
- * - Arrays of operators are converted to AND conditions
97
- * - Single operator objects pass through as-is
98
- * - Shorthand values are handled by odata-query
99
- */
100
- transformFilter(filter) {
101
- var _a;
102
- if (typeof filter === "string") {
103
- return filter;
104
- }
105
- if (Array.isArray(filter)) {
106
- return filter.map((f) => this.transformFilter(f));
107
- }
108
- if ("and" in filter || "or" in filter || "not" in filter) {
109
- const result2 = {};
110
- if ("and" in filter && Array.isArray(filter.and)) {
111
- result2.and = filter.and.map((f) => this.transformFilter(f));
112
- }
113
- if ("or" in filter && Array.isArray(filter.or)) {
114
- result2.or = filter.or.map((f) => this.transformFilter(f));
115
- }
116
- if ("not" in filter && filter.not) {
117
- result2.not = this.transformFilter(filter.not);
118
- }
119
- return result2;
120
- }
121
- const result = {};
122
- const andConditions = [];
123
- for (const [field, value] of Object.entries(filter)) {
124
- const fieldId = ((_a = this.occurrence) == null ? void 0 : _a.baseTable) ? transformFieldName(field, this.occurrence.baseTable) : field;
125
- if (Array.isArray(value)) {
126
- if (value.length === 1) {
127
- result[fieldId] = value[0];
128
- } else {
129
- for (const op of value) {
130
- andConditions.push({ [fieldId]: op });
131
- }
132
- }
133
- } else if (value && typeof value === "object" && !(value instanceof Date) && !Array.isArray(value)) {
134
- const operatorKeys = [
135
- "eq",
136
- "ne",
137
- "gt",
138
- "ge",
139
- "lt",
140
- "le",
141
- "contains",
142
- "startswith",
143
- "endswith",
144
- "in"
145
- ];
146
- const isOperatorObject = operatorKeys.some((key) => key in value);
147
- if (isOperatorObject) {
148
- result[fieldId] = value;
149
- } else {
150
- result[fieldId] = value;
151
- }
152
- } else {
153
- result[fieldId] = value;
154
- }
155
- }
156
- if (andConditions.length > 0) {
157
- if (Object.keys(result).length > 0) {
158
- return { and: [...andConditions, result] };
159
- } else {
160
- return { and: andConditions };
161
- }
162
- }
163
- return result;
164
- }
165
- filter(filter) {
166
- this.queryOptions.filter = this.transformFilter(filter);
167
- return this;
168
- }
169
- orderBy(orderBy) {
170
- var _a;
171
- if (((_a = this.occurrence) == null ? void 0 : _a.baseTable) && orderBy) {
172
- if (Array.isArray(orderBy)) {
173
- this.queryOptions.orderBy = orderBy.map(
174
- (field) => transformOrderByField(String(field), this.occurrence.baseTable)
175
- );
176
- } else {
177
- this.queryOptions.orderBy = transformOrderByField(
178
- String(orderBy),
179
- this.occurrence.baseTable
180
- );
181
- }
182
- } else {
183
- this.queryOptions.orderBy = orderBy;
184
- }
185
- return this;
186
- }
187
- top(count) {
188
- this.queryOptions.top = count;
189
- return this;
190
- }
191
- skip(count) {
192
- this.queryOptions.skip = count;
193
- return this;
194
- }
195
- /**
196
- * Formats select fields for use in query strings.
197
- * - Transforms field names to FMFIDs if using entity IDs
198
- * - Wraps "id" fields in double quotes
199
- * - URL-encodes special characters but preserves spaces
200
- */
201
- formatSelectFields(select, baseTable) {
202
- if (!select) return "";
203
- const selectFieldsArray = Array.isArray(select) ? select : [select];
204
- const transformedFields = baseTable ? transformFieldNamesArray(
205
- selectFieldsArray.map((f) => String(f)),
206
- baseTable
207
- ) : selectFieldsArray.map((f) => String(f));
208
- return transformedFields.map((field) => {
209
- if (field === "id") return `"id"`;
210
- const encodedField = encodeURIComponent(String(field));
211
- return encodedField.replace(/%20/g, " ");
212
- }).join(",");
213
- }
214
- /**
215
- * Builds expand validation configs from internal expand configurations.
216
- * These are used to validate expanded navigation properties.
217
- */
218
- buildExpandValidationConfigs(configs) {
219
- return configs.map((config) => {
220
- var _a, _b, _c;
221
- const targetOccurrence = (_a = this.occurrence) == null ? void 0 : _a.navigation[config.relation];
222
- const targetSchema = (_b = targetOccurrence == null ? void 0 : targetOccurrence.baseTable) == null ? void 0 : _b.schema;
223
- const selectedFields = ((_c = config.options) == null ? void 0 : _c.select) ? Array.isArray(config.options.select) ? config.options.select.map((f) => String(f)) : [String(config.options.select)] : void 0;
224
- return {
225
- relation: config.relation,
226
- targetSchema,
227
- targetOccurrence,
228
- targetBaseTable: targetOccurrence == null ? void 0 : targetOccurrence.baseTable,
229
- occurrence: targetOccurrence,
230
- // Add occurrence for transformation
231
- selectedFields,
232
- nestedExpands: void 0
233
- // TODO: Handle nested expands if needed
234
- };
235
- });
236
- }
237
- /**
238
- * Builds OData expand query string from expand configurations.
239
- * Handles nested expands recursively.
240
- * Transforms relation names to FMTIDs if using entity IDs.
241
- */
242
- buildExpandString(configs) {
243
- if (configs.length === 0) {
244
- return "";
245
- }
246
- return configs.map((config) => {
247
- var _a;
248
- const targetOccurrence = (_a = this.occurrence) == null ? void 0 : _a.navigation[config.relation];
249
- const relationName = targetOccurrence && targetOccurrence.isUsingTableId() ? targetOccurrence.getTableId() : config.relation;
250
- if (!config.options || Object.keys(config.options).length === 0) {
251
- return relationName;
252
- }
253
- const parts = [];
254
- if (config.options.select) {
255
- const selectFields = this.formatSelectFields(
256
- config.options.select,
257
- targetOccurrence == null ? void 0 : targetOccurrence.baseTable
258
- );
259
- parts.push(`$select=${selectFields}`);
260
- }
261
- if (config.options.filter) {
262
- const filterQuery = buildQuery({ filter: config.options.filter });
263
- const filterMatch = filterQuery.match(/\$filter=([^&]+)/);
264
- if (filterMatch) {
265
- parts.push(`$filter=${filterMatch[1]}`);
266
- }
267
- }
268
- if (config.options.orderBy) {
269
- const orderByValue = Array.isArray(config.options.orderBy) ? config.options.orderBy.join(",") : config.options.orderBy;
270
- parts.push(`$orderby=${String(orderByValue)}`);
271
- }
272
- if (config.options.top !== void 0) {
273
- parts.push(`$top=${config.options.top}`);
274
- }
275
- if (config.options.skip !== void 0) {
276
- parts.push(`$skip=${config.options.skip}`);
277
- }
278
- if (config.options.expand) {
279
- if (typeof config.options.expand === "string") {
280
- parts.push(`$expand=${config.options.expand}`);
281
- }
282
- }
283
- if (parts.length === 0) {
284
- return relationName;
285
- }
286
- return `${relationName}(${parts.join(";")})`;
287
- }).join(",");
288
- }
289
- expand(relation, callback) {
290
- var _a;
291
- const targetOccurrence = (_a = this.occurrence) == null ? void 0 : _a.navigation[relation];
292
- if (callback) {
293
- const targetBuilder = new QueryBuilder({
294
- occurrence: targetOccurrence,
295
- tableName: (targetOccurrence == null ? void 0 : targetOccurrence.name) ?? relation,
296
- databaseName: this.databaseName,
297
- context: this.context,
298
- databaseUseEntityIds: this.databaseUseEntityIds
299
- });
300
- const typedBuilder = targetBuilder;
301
- const configuredBuilder = callback(typedBuilder);
302
- const expandOptions = {
303
- ...configuredBuilder.queryOptions
304
- };
305
- if (configuredBuilder.expandConfigs.length > 0) {
306
- const nestedExpandString = this.buildExpandString(
307
- configuredBuilder.expandConfigs
308
- );
309
- if (nestedExpandString) {
310
- expandOptions.expand = nestedExpandString;
311
- }
312
- }
313
- const expandConfig = {
314
- relation,
315
- options: expandOptions
316
- };
317
- this.expandConfigs.push(expandConfig);
318
- } else {
319
- this.expandConfigs.push({ relation });
320
- }
321
- return this;
322
- }
323
- single() {
324
- const newBuilder = new QueryBuilder({
325
- occurrence: this.occurrence,
326
- tableName: this.tableName,
327
- databaseName: this.databaseName,
328
- context: this.context,
329
- databaseUseEntityIds: this.databaseUseEntityIds
330
- });
331
- newBuilder.queryOptions = { ...this.queryOptions };
332
- newBuilder.expandConfigs = [...this.expandConfigs];
333
- newBuilder.singleMode = "exact";
334
- newBuilder.isCountMode = this.isCountMode;
335
- newBuilder.isNavigate = this.isNavigate;
336
- newBuilder.navigateRecordId = this.navigateRecordId;
337
- newBuilder.navigateRelation = this.navigateRelation;
338
- newBuilder.navigateSourceTableName = this.navigateSourceTableName;
339
- newBuilder.navigateBaseRelation = this.navigateBaseRelation;
340
- return newBuilder;
341
- }
342
- maybeSingle() {
343
- const newBuilder = new QueryBuilder({
344
- occurrence: this.occurrence,
345
- tableName: this.tableName,
346
- databaseName: this.databaseName,
347
- context: this.context,
348
- databaseUseEntityIds: this.databaseUseEntityIds
349
- });
350
- newBuilder.queryOptions = { ...this.queryOptions };
351
- newBuilder.expandConfigs = [...this.expandConfigs];
352
- newBuilder.singleMode = "maybe";
353
- newBuilder.isCountMode = this.isCountMode;
354
- newBuilder.isNavigate = this.isNavigate;
355
- newBuilder.navigateRecordId = this.navigateRecordId;
356
- newBuilder.navigateRelation = this.navigateRelation;
357
- newBuilder.navigateSourceTableName = this.navigateSourceTableName;
358
- newBuilder.navigateBaseRelation = this.navigateBaseRelation;
359
- return newBuilder;
360
- }
361
- count() {
362
- const newBuilder = new QueryBuilder({
363
- occurrence: this.occurrence,
364
- tableName: this.tableName,
365
- databaseName: this.databaseName,
366
- context: this.context,
367
- databaseUseEntityIds: this.databaseUseEntityIds
368
- });
369
- newBuilder.queryOptions = { ...this.queryOptions, count: true };
370
- newBuilder.expandConfigs = [...this.expandConfigs];
371
- newBuilder.singleMode = this.singleMode;
372
- newBuilder.isCountMode = true;
373
- newBuilder.isNavigate = this.isNavigate;
374
- newBuilder.navigateRecordId = this.navigateRecordId;
375
- newBuilder.navigateRelation = this.navigateRelation;
376
- newBuilder.navigateSourceTableName = this.navigateSourceTableName;
377
- newBuilder.navigateBaseRelation = this.navigateBaseRelation;
378
- return newBuilder;
379
- }
380
- async execute(options) {
381
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
382
- const queryOptionsWithoutExpand = { ...this.queryOptions };
383
- delete queryOptionsWithoutExpand.expand;
384
- const mergedOptions = this.mergeExecuteOptions(options);
385
- if (queryOptionsWithoutExpand.select) {
386
- queryOptionsWithoutExpand.select = this.formatSelectFields(
387
- queryOptionsWithoutExpand.select,
388
- (_a = this.occurrence) == null ? void 0 : _a.baseTable
389
- );
390
- }
391
- let queryString = buildQuery(queryOptionsWithoutExpand);
392
- const expandString = this.buildExpandString(this.expandConfigs);
393
- if (expandString) {
394
- const separator = queryString.includes("?") ? "&" : "?";
395
- queryString = `${queryString}${separator}$expand=${expandString}`;
396
- }
397
- if (this.isNavigate && this.navigateRecordId && this.navigateRelation && this.navigateSourceTableName) {
398
- let url;
399
- if (this.navigateBaseRelation) {
400
- url = `/${this.databaseName}/${this.navigateSourceTableName}/${this.navigateBaseRelation}('${this.navigateRecordId}')/${this.navigateRelation}${queryString}`;
401
- } else {
402
- url = `/${this.databaseName}/${this.navigateSourceTableName}('${this.navigateRecordId}')/${this.navigateRelation}${queryString}`;
403
- }
404
- const result2 = await this.context._makeRequest(url, mergedOptions);
405
- if (result2.error) {
406
- return { data: void 0, error: result2.error };
407
- }
408
- let response2 = result2.data;
409
- const shouldUseIds2 = mergedOptions.useEntityIds ?? false;
410
- if (((_b = this.occurrence) == null ? void 0 : _b.baseTable) && shouldUseIds2) {
411
- const expandValidationConfigs3 = this.buildExpandValidationConfigs(
412
- this.expandConfigs
413
- );
414
- response2 = transformResponseFields(
415
- response2,
416
- this.occurrence.baseTable,
417
- expandValidationConfigs3
418
- );
419
- }
420
- if ((options == null ? void 0 : options.skipValidation) === true) {
421
- const resp = response2;
422
- if (this.singleMode !== false) {
423
- const records = resp.value ?? [resp];
424
- const count = Array.isArray(records) ? records.length : 1;
425
- if (count > 1) {
426
- return {
427
- data: void 0,
428
- error: new RecordCountMismatchError(
429
- this.singleMode === "exact" ? "one" : "at-most-one",
430
- count
431
- )
432
- };
433
- }
434
- if (count === 0) {
435
- if (this.singleMode === "exact") {
436
- return {
437
- data: void 0,
438
- error: new RecordCountMismatchError("one", 0)
439
- };
440
- }
441
- return { data: null, error: void 0 };
442
- }
443
- const record = Array.isArray(records) ? records[0] : records;
444
- const stripped = this.stripODataAnnotationsIfNeeded(record, options);
445
- return { data: stripped, error: void 0 };
446
- } else {
447
- const records = resp.value ?? [];
448
- const stripped = records.map(
449
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
450
- );
451
- return { data: stripped, error: void 0 };
452
- }
453
- }
454
- const schema2 = (_d = (_c = this.occurrence) == null ? void 0 : _c.baseTable) == null ? void 0 : _d.schema;
455
- const selectedFields2 = this.queryOptions.select;
456
- const expandValidationConfigs2 = this.buildExpandValidationConfigs(
457
- this.expandConfigs
458
- );
459
- if (this.singleMode !== false) {
460
- const validation = await validateSingleResponse(
461
- response2,
462
- schema2,
463
- selectedFields2,
464
- expandValidationConfigs2,
465
- this.singleMode
466
- );
467
- if (!validation.valid) {
468
- return { data: void 0, error: validation.error };
469
- }
470
- const stripped = validation.data ? this.stripODataAnnotationsIfNeeded(validation.data, options) : null;
471
- return { data: stripped, error: void 0 };
472
- } else {
473
- const validation = await validateListResponse(
474
- response2,
475
- schema2,
476
- selectedFields2,
477
- expandValidationConfigs2
478
- );
479
- if (!validation.valid) {
480
- return { data: void 0, error: validation.error };
481
- }
482
- const stripped = validation.data.map(
483
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
484
- );
485
- return { data: stripped, error: void 0 };
486
- }
487
- }
488
- if (this.isNavigate && !this.navigateRecordId && this.navigateRelation && this.navigateSourceTableName) {
489
- const result2 = await this.context._makeRequest(
490
- `/${this.databaseName}/${this.navigateSourceTableName}/${this.navigateRelation}${queryString}`,
491
- mergedOptions
492
- );
493
- if (result2.error) {
494
- return { data: void 0, error: result2.error };
495
- }
496
- let response2 = result2.data;
497
- const shouldUseIds2 = mergedOptions.useEntityIds ?? false;
498
- if (((_e = this.occurrence) == null ? void 0 : _e.baseTable) && shouldUseIds2) {
499
- const expandValidationConfigs3 = this.buildExpandValidationConfigs(
500
- this.expandConfigs
501
- );
502
- response2 = transformResponseFields(
503
- response2,
504
- this.occurrence.baseTable,
505
- expandValidationConfigs3
506
- );
507
- }
508
- if ((options == null ? void 0 : options.skipValidation) === true) {
509
- const resp = response2;
510
- if (this.singleMode !== false) {
511
- const records = resp.value ?? [resp];
512
- const count = Array.isArray(records) ? records.length : 1;
513
- if (count > 1) {
514
- return {
515
- data: void 0,
516
- error: new RecordCountMismatchError(
517
- this.singleMode === "exact" ? "one" : "at-most-one",
518
- count
519
- )
520
- };
521
- }
522
- if (count === 0) {
523
- if (this.singleMode === "exact") {
524
- return {
525
- data: void 0,
526
- error: new RecordCountMismatchError("one", 0)
527
- };
528
- }
529
- return { data: null, error: void 0 };
530
- }
531
- const record = Array.isArray(records) ? records[0] : records;
532
- const stripped = this.stripODataAnnotationsIfNeeded(record, options);
533
- return { data: stripped, error: void 0 };
534
- } else {
535
- const records = resp.value ?? [];
536
- const stripped = records.map(
537
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
538
- );
539
- return { data: stripped, error: void 0 };
540
- }
541
- }
542
- const schema2 = (_g = (_f = this.occurrence) == null ? void 0 : _f.baseTable) == null ? void 0 : _g.schema;
543
- const selectedFields2 = this.queryOptions.select;
544
- const expandValidationConfigs2 = this.buildExpandValidationConfigs(
545
- this.expandConfigs
546
- );
547
- if (this.singleMode !== false) {
548
- const validation = await validateSingleResponse(
549
- response2,
550
- schema2,
551
- selectedFields2,
552
- expandValidationConfigs2,
553
- this.singleMode
554
- );
555
- if (!validation.valid) {
556
- return { data: void 0, error: validation.error };
557
- }
558
- const stripped = validation.data ? this.stripODataAnnotationsIfNeeded(validation.data, options) : null;
559
- return { data: stripped, error: void 0 };
560
- } else {
561
- const validation = await validateListResponse(
562
- response2,
563
- schema2,
564
- selectedFields2,
565
- expandValidationConfigs2
566
- );
567
- if (!validation.valid) {
568
- return { data: void 0, error: validation.error };
569
- }
570
- const stripped = validation.data.map(
571
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
572
- );
573
- return { data: stripped, error: void 0 };
574
- }
575
- }
576
- if (this.isCountMode) {
577
- const tableId2 = this.getTableId(mergedOptions.useEntityIds);
578
- const result2 = await this.context._makeRequest(
579
- `/${this.databaseName}/${tableId2}/$count${queryString}`,
580
- mergedOptions
581
- );
582
- if (result2.error) {
583
- return { data: void 0, error: result2.error };
584
- }
585
- const count = typeof result2.data === "string" ? Number(result2.data) : result2.data;
586
- return { data: count, error: void 0 };
587
- }
588
- const tableId = this.getTableId(mergedOptions.useEntityIds);
589
- const result = await this.context._makeRequest(
590
- `/${this.databaseName}/${tableId}${queryString}`,
591
- mergedOptions
592
- );
593
- if (result.error) {
594
- return { data: void 0, error: result.error };
595
- }
596
- let response = result.data;
597
- const shouldUseIds = mergedOptions.useEntityIds ?? false;
598
- if (((_h = this.occurrence) == null ? void 0 : _h.baseTable) && shouldUseIds) {
599
- const expandValidationConfigs2 = this.buildExpandValidationConfigs(
600
- this.expandConfigs
601
- );
602
- response = transformResponseFields(
603
- response,
604
- this.occurrence.baseTable,
605
- expandValidationConfigs2
606
- );
607
- }
608
- if ((options == null ? void 0 : options.skipValidation) === true) {
609
- const resp = response;
610
- if (this.singleMode !== false) {
611
- const records = resp.value ?? [resp];
612
- const count = Array.isArray(records) ? records.length : 1;
613
- if (count > 1) {
614
- return {
615
- data: void 0,
616
- error: new RecordCountMismatchError(
617
- this.singleMode === "exact" ? "one" : "at-most-one",
618
- count
619
- )
620
- };
621
- }
622
- if (count === 0) {
623
- if (this.singleMode === "exact") {
624
- return {
625
- data: void 0,
626
- error: new RecordCountMismatchError("one", 0)
627
- };
628
- }
629
- return { data: null, error: void 0 };
630
- }
631
- const record = Array.isArray(records) ? records[0] : records;
632
- const stripped = this.stripODataAnnotationsIfNeeded(record, options);
633
- return { data: stripped, error: void 0 };
634
- } else {
635
- const records = resp.value ?? [];
636
- const stripped = records.map(
637
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
638
- );
639
- return { data: stripped, error: void 0 };
640
- }
641
- }
642
- const schema = (_j = (_i = this.occurrence) == null ? void 0 : _i.baseTable) == null ? void 0 : _j.schema;
643
- const selectedFields = this.queryOptions.select;
644
- const expandValidationConfigs = this.buildExpandValidationConfigs(
645
- this.expandConfigs
646
- );
647
- if (this.singleMode !== false) {
648
- const validation = await validateSingleResponse(
649
- response,
650
- schema,
651
- selectedFields,
652
- expandValidationConfigs,
653
- this.singleMode
654
- );
655
- if (!validation.valid) {
656
- return { data: void 0, error: validation.error };
657
- }
658
- const stripped = validation.data ? this.stripODataAnnotationsIfNeeded(validation.data, options) : null;
659
- return {
660
- data: stripped,
661
- error: void 0
662
- };
663
- } else {
664
- const validation = await validateListResponse(
665
- response,
666
- schema,
667
- selectedFields,
668
- expandValidationConfigs
669
- );
670
- if (!validation.valid) {
671
- return { data: void 0, error: validation.error };
672
- }
673
- const stripped = validation.data.map(
674
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
675
- );
676
- return {
677
- data: stripped,
678
- error: void 0
679
- };
680
- }
681
- }
682
- getQueryString() {
683
- var _a;
684
- const queryOptionsWithoutExpand = { ...this.queryOptions };
685
- delete queryOptionsWithoutExpand.expand;
686
- if (queryOptionsWithoutExpand.select) {
687
- queryOptionsWithoutExpand.select = this.formatSelectFields(
688
- queryOptionsWithoutExpand.select,
689
- (_a = this.occurrence) == null ? void 0 : _a.baseTable
690
- );
691
- }
692
- let queryParams = buildQuery(queryOptionsWithoutExpand);
693
- if (this.queryOptions.select) {
694
- queryParams = queryParams.replace(
695
- /\$select=([^&]*)/,
696
- (match, selectValue) => {
697
- return `$select=${selectValue.replace(/%20/g, " ")}`;
698
- }
699
- );
700
- }
701
- const expandString = this.buildExpandString(this.expandConfigs);
702
- if (expandString) {
703
- const separator = queryParams.includes("?") ? "&" : "?";
704
- queryParams = `${queryParams}${separator}$expand=${expandString}`;
705
- }
706
- if (this.isNavigate && this.navigateRecordId && this.navigateRelation && this.navigateSourceTableName) {
707
- let path;
708
- if (this.navigateBaseRelation) {
709
- path = `/${this.navigateSourceTableName}/${this.navigateBaseRelation}('${this.navigateRecordId}')/${this.navigateRelation}`;
710
- } else {
711
- path = `/${this.navigateSourceTableName}('${this.navigateRecordId}')/${this.navigateRelation}`;
712
- }
713
- return queryParams ? `${path}${queryParams}` : path;
714
- }
715
- if (this.isNavigate && !this.navigateRecordId && this.navigateRelation && this.navigateSourceTableName) {
716
- const path = `/${this.navigateSourceTableName}/${this.navigateRelation}`;
717
- return queryParams ? `${path}${queryParams}` : path;
718
- }
719
- return `/${this.tableName}${queryParams}`;
720
- }
721
- getRequestConfig() {
722
- var _a;
723
- const queryOptionsWithoutExpand = { ...this.queryOptions };
724
- delete queryOptionsWithoutExpand.expand;
725
- if (queryOptionsWithoutExpand.select) {
726
- queryOptionsWithoutExpand.select = this.formatSelectFields(
727
- queryOptionsWithoutExpand.select,
728
- (_a = this.occurrence) == null ? void 0 : _a.baseTable
729
- );
730
- }
731
- let queryString = buildQuery(queryOptionsWithoutExpand);
732
- const expandString = this.buildExpandString(this.expandConfigs);
733
- if (expandString) {
734
- const separator = queryString.includes("?") ? "&" : "?";
735
- queryString = `${queryString}${separator}$expand=${expandString}`;
736
- }
737
- let url;
738
- if (this.isNavigate && this.navigateRecordId && this.navigateRelation && this.navigateSourceTableName) {
739
- if (this.navigateBaseRelation) {
740
- url = `/${this.databaseName}/${this.navigateSourceTableName}/${this.navigateBaseRelation}('${this.navigateRecordId}')/${this.navigateRelation}${queryString}`;
741
- } else {
742
- url = `/${this.databaseName}/${this.navigateSourceTableName}('${this.navigateRecordId}')/${this.navigateRelation}${queryString}`;
743
- }
744
- } else if (this.isNavigate && !this.navigateRecordId && this.navigateRelation && this.navigateSourceTableName) {
745
- url = `/${this.databaseName}/${this.navigateSourceTableName}/${this.navigateRelation}${queryString}`;
746
- } else if (this.isCountMode) {
747
- url = `/${this.databaseName}/${this.tableName}/$count${queryString}`;
748
- } else {
749
- url = `/${this.databaseName}/${this.tableName}${queryString}`;
750
- }
751
- return {
752
- method: "GET",
753
- url
754
- };
755
- }
756
- toRequest(baseUrl) {
757
- const config = this.getRequestConfig();
758
- const fullUrl = `${baseUrl}${config.url}`;
759
- return new Request(fullUrl, {
760
- method: config.method,
761
- headers: {
762
- "Content-Type": "application/json",
763
- Accept: "application/json"
764
- }
765
- });
766
- }
767
- async processResponse(response, options) {
768
- var _a, _b, _c;
769
- if (response.status === 204) {
770
- if (this.singleMode !== false) {
771
- if (this.singleMode === "maybe") {
772
- return { data: null, error: void 0 };
773
- }
774
- return {
775
- data: void 0,
776
- error: new RecordCountMismatchError("one", 0)
777
- };
778
- }
779
- return { data: [], error: void 0 };
780
- }
781
- let rawData;
782
- try {
783
- rawData = await response.json();
784
- } catch (err) {
785
- if (err instanceof SyntaxError && response.status === 204) {
786
- return { data: [], error: void 0 };
787
- }
788
- return {
789
- data: void 0,
790
- error: {
791
- name: "ResponseParseError",
792
- message: `Failed to parse response JSON: ${err instanceof Error ? err.message : "Unknown error"}`,
793
- timestamp: /* @__PURE__ */ new Date()
794
- }
795
- };
796
- }
797
- if (!rawData) {
798
- return {
799
- data: void 0,
800
- error: {
801
- name: "ResponseError",
802
- message: "Response body was empty or null",
803
- timestamp: /* @__PURE__ */ new Date()
804
- }
805
- };
806
- }
807
- const shouldUseIds = (options == null ? void 0 : options.useEntityIds) ?? this.databaseUseEntityIds;
808
- let transformedData = rawData;
809
- if (((_a = this.occurrence) == null ? void 0 : _a.baseTable) && shouldUseIds) {
810
- const expandValidationConfigs2 = this.buildExpandValidationConfigs(
811
- this.expandConfigs
812
- );
813
- transformedData = transformResponseFields(
814
- rawData,
815
- this.occurrence.baseTable,
816
- expandValidationConfigs2
817
- );
818
- }
819
- if ((options == null ? void 0 : options.skipValidation) === true) {
820
- const resp = transformedData;
821
- if (this.singleMode !== false) {
822
- const records = resp.value ?? [resp];
823
- const count = Array.isArray(records) ? records.length : 1;
824
- if (count > 1) {
825
- return {
826
- data: void 0,
827
- error: new RecordCountMismatchError(
828
- this.singleMode === "exact" ? "one" : "at-most-one",
829
- count
830
- )
831
- };
832
- }
833
- if (count === 0) {
834
- if (this.singleMode === "exact") {
835
- return {
836
- data: void 0,
837
- error: new RecordCountMismatchError("one", 0)
838
- };
839
- }
840
- return { data: null, error: void 0 };
841
- }
842
- const record = Array.isArray(records) ? records[0] : records;
843
- const stripped2 = this.stripODataAnnotationsIfNeeded(record, options);
844
- return { data: stripped2, error: void 0 };
845
- } else {
846
- const records = resp.value ?? [];
847
- const stripped2 = records.map(
848
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
849
- );
850
- return { data: stripped2, error: void 0 };
851
- }
852
- }
853
- const schema = (_c = (_b = this.occurrence) == null ? void 0 : _b.baseTable) == null ? void 0 : _c.schema;
854
- const selectedFields = this.queryOptions.select;
855
- const expandValidationConfigs = this.buildExpandValidationConfigs(
856
- this.expandConfigs
857
- );
858
- if (this.singleMode !== false) {
859
- const validation2 = await validateSingleResponse(
860
- transformedData,
861
- schema,
862
- selectedFields,
863
- expandValidationConfigs,
864
- this.singleMode
865
- );
866
- if (!validation2.valid) {
867
- return { data: void 0, error: validation2.error };
868
- }
869
- if (validation2.data === null) {
870
- return { data: null, error: void 0 };
871
- }
872
- const stripped2 = this.stripODataAnnotationsIfNeeded(
873
- validation2.data,
874
- options
875
- );
876
- return { data: stripped2, error: void 0 };
877
- }
878
- const validation = await validateListResponse(
879
- transformedData,
880
- schema,
881
- selectedFields,
882
- expandValidationConfigs
883
- );
884
- if (!validation.valid) {
885
- return { data: void 0, error: validation.error };
886
- }
887
- const stripped = validation.data.map(
888
- (record) => this.stripODataAnnotationsIfNeeded(record, options)
889
- );
890
- return { data: stripped, error: void 0 };
891
- }
892
- }
893
- export {
894
- QueryBuilder
895
- };
896
- //# sourceMappingURL=query-builder.js.map