@proofkit/fmodata 0.1.0-alpha.13 → 0.1.0-alpha.14

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 (143) hide show
  1. package/README.md +489 -334
  2. package/dist/esm/client/batch-builder.d.ts +7 -4
  3. package/dist/esm/client/batch-builder.js +84 -25
  4. package/dist/esm/client/batch-builder.js.map +1 -1
  5. package/dist/esm/client/builders/default-select.d.ts +7 -0
  6. package/dist/esm/client/builders/default-select.js +42 -0
  7. package/dist/esm/client/builders/default-select.js.map +1 -0
  8. package/dist/esm/client/builders/expand-builder.d.ts +43 -0
  9. package/dist/esm/client/builders/expand-builder.js +173 -0
  10. package/dist/esm/client/builders/expand-builder.js.map +1 -0
  11. package/dist/esm/client/builders/index.d.ts +8 -0
  12. package/dist/esm/client/builders/query-string-builder.d.ts +15 -0
  13. package/dist/esm/client/builders/query-string-builder.js +25 -0
  14. package/dist/esm/client/builders/query-string-builder.js.map +1 -0
  15. package/dist/esm/client/builders/response-processor.d.ts +39 -0
  16. package/dist/esm/client/builders/response-processor.js +170 -0
  17. package/dist/esm/client/builders/response-processor.js.map +1 -0
  18. package/dist/esm/client/builders/select-mixin.d.ts +31 -0
  19. package/dist/esm/client/builders/select-mixin.js +30 -0
  20. package/dist/esm/client/builders/select-mixin.js.map +1 -0
  21. package/dist/esm/client/builders/select-utils.d.ts +8 -0
  22. package/dist/esm/client/builders/select-utils.js +15 -0
  23. package/dist/esm/client/builders/select-utils.js.map +1 -0
  24. package/dist/esm/client/builders/shared-types.d.ts +39 -0
  25. package/dist/esm/client/builders/table-utils.d.ts +35 -0
  26. package/dist/esm/client/builders/table-utils.js +45 -0
  27. package/dist/esm/client/builders/table-utils.js.map +1 -0
  28. package/dist/esm/client/database.d.ts +3 -22
  29. package/dist/esm/client/database.js +14 -76
  30. package/dist/esm/client/database.js.map +1 -1
  31. package/dist/esm/client/delete-builder.d.ts +11 -15
  32. package/dist/esm/client/delete-builder.js +26 -26
  33. package/dist/esm/client/delete-builder.js.map +1 -1
  34. package/dist/esm/client/entity-set.d.ts +32 -32
  35. package/dist/esm/client/entity-set.js +92 -69
  36. package/dist/esm/client/entity-set.js.map +1 -1
  37. package/dist/esm/client/error-parser.d.ts +12 -0
  38. package/dist/esm/client/error-parser.js +30 -0
  39. package/dist/esm/client/error-parser.js.map +1 -0
  40. package/dist/esm/client/filemaker-odata.d.ts +2 -4
  41. package/dist/esm/client/filemaker-odata.js +1 -5
  42. package/dist/esm/client/filemaker-odata.js.map +1 -1
  43. package/dist/esm/client/insert-builder.d.ts +7 -9
  44. package/dist/esm/client/insert-builder.js +70 -24
  45. package/dist/esm/client/insert-builder.js.map +1 -1
  46. package/dist/esm/client/query/expand-builder.d.ts +35 -0
  47. package/dist/esm/client/query/index.d.ts +3 -0
  48. package/dist/esm/client/query/query-builder.d.ts +134 -0
  49. package/dist/esm/client/query/query-builder.js +505 -0
  50. package/dist/esm/client/query/query-builder.js.map +1 -0
  51. package/dist/esm/client/query/response-processor.d.ts +22 -0
  52. package/dist/esm/client/query/types.d.ts +52 -0
  53. package/dist/esm/client/query/url-builder.d.ts +71 -0
  54. package/dist/esm/client/query/url-builder.js +107 -0
  55. package/dist/esm/client/query/url-builder.js.map +1 -0
  56. package/dist/esm/client/query-builder.d.ts +1 -111
  57. package/dist/esm/client/record-builder.d.ts +56 -63
  58. package/dist/esm/client/record-builder.js +158 -297
  59. package/dist/esm/client/record-builder.js.map +1 -1
  60. package/dist/esm/client/response-processor.d.ts +3 -3
  61. package/dist/esm/client/update-builder.d.ts +16 -21
  62. package/dist/esm/client/update-builder.js +56 -30
  63. package/dist/esm/client/update-builder.js.map +1 -1
  64. package/dist/esm/errors.d.ts +8 -1
  65. package/dist/esm/errors.js +17 -0
  66. package/dist/esm/errors.js.map +1 -1
  67. package/dist/esm/index.d.ts +3 -7
  68. package/dist/esm/index.js +37 -8
  69. package/dist/esm/index.js.map +1 -1
  70. package/dist/esm/orm/column.d.ts +45 -0
  71. package/dist/esm/orm/column.js +59 -0
  72. package/dist/esm/orm/column.js.map +1 -0
  73. package/dist/esm/orm/field-builders.d.ts +154 -0
  74. package/dist/esm/orm/field-builders.js +152 -0
  75. package/dist/esm/orm/field-builders.js.map +1 -0
  76. package/dist/esm/orm/index.d.ts +4 -0
  77. package/dist/esm/orm/operators.d.ts +175 -0
  78. package/dist/esm/orm/operators.js +221 -0
  79. package/dist/esm/orm/operators.js.map +1 -0
  80. package/dist/esm/orm/table.d.ts +341 -0
  81. package/dist/esm/orm/table.js +211 -0
  82. package/dist/esm/orm/table.js.map +1 -0
  83. package/dist/esm/transform.d.ts +20 -21
  84. package/dist/esm/transform.js +34 -34
  85. package/dist/esm/transform.js.map +1 -1
  86. package/dist/esm/types.d.ts +16 -13
  87. package/dist/esm/types.js.map +1 -1
  88. package/dist/esm/validation.d.ts +14 -4
  89. package/dist/esm/validation.js +45 -1
  90. package/dist/esm/validation.js.map +1 -1
  91. package/package.json +20 -17
  92. package/src/client/batch-builder.ts +100 -32
  93. package/src/client/builders/default-select.ts +69 -0
  94. package/src/client/builders/expand-builder.ts +236 -0
  95. package/src/client/builders/index.ts +11 -0
  96. package/src/client/builders/query-string-builder.ts +41 -0
  97. package/src/client/builders/response-processor.ts +273 -0
  98. package/src/client/builders/select-mixin.ts +74 -0
  99. package/src/client/builders/select-utils.ts +34 -0
  100. package/src/client/builders/shared-types.ts +41 -0
  101. package/src/client/builders/table-utils.ts +87 -0
  102. package/src/client/database.ts +19 -160
  103. package/src/client/delete-builder.ts +46 -51
  104. package/src/client/entity-set.ts +227 -302
  105. package/src/client/error-parser.ts +59 -0
  106. package/src/client/filemaker-odata.ts +3 -14
  107. package/src/client/insert-builder.ts +124 -43
  108. package/src/client/query/expand-builder.ts +164 -0
  109. package/src/client/query/index.ts +13 -0
  110. package/src/client/query/query-builder.ts +816 -0
  111. package/src/client/query/response-processor.ts +244 -0
  112. package/src/client/query/types.ts +102 -0
  113. package/src/client/query/url-builder.ts +179 -0
  114. package/src/client/query-builder.ts +8 -1454
  115. package/src/client/record-builder.ts +325 -585
  116. package/src/client/response-processor.ts +4 -5
  117. package/src/client/update-builder.ts +102 -73
  118. package/src/errors.ts +22 -1
  119. package/src/index.ts +55 -5
  120. package/src/orm/column.ts +78 -0
  121. package/src/orm/field-builders.ts +296 -0
  122. package/src/orm/index.ts +60 -0
  123. package/src/orm/operators.ts +428 -0
  124. package/src/orm/table.ts +759 -0
  125. package/src/transform.ts +62 -48
  126. package/src/types.ts +20 -63
  127. package/src/validation.ts +76 -4
  128. package/LICENSE.md +0 -21
  129. package/dist/esm/client/base-table.d.ts +0 -128
  130. package/dist/esm/client/base-table.js +0 -57
  131. package/dist/esm/client/base-table.js.map +0 -1
  132. package/dist/esm/client/build-occurrences.d.ts +0 -74
  133. package/dist/esm/client/build-occurrences.js +0 -31
  134. package/dist/esm/client/build-occurrences.js.map +0 -1
  135. package/dist/esm/client/query-builder.js +0 -900
  136. package/dist/esm/client/query-builder.js.map +0 -1
  137. package/dist/esm/client/table-occurrence.d.ts +0 -86
  138. package/dist/esm/client/table-occurrence.js +0 -58
  139. package/dist/esm/client/table-occurrence.js.map +0 -1
  140. package/src/client/base-table.ts +0 -178
  141. package/src/client/build-occurrences.ts +0 -155
  142. package/src/client/query-builder.ts.bak +0 -1457
  143. package/src/client/table-occurrence.ts +0 -156
@@ -0,0 +1,759 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import { FieldBuilder, type ContainerDbType } from "./field-builders";
3
+ import type { FieldBuilder as FieldBuilderType } from "./field-builders";
4
+ import { Column } from "./column";
5
+ import { z } from "zod/v4";
6
+
7
+ /**
8
+ * Extract the output type from a FieldBuilder.
9
+ * This is what you get when reading from the database.
10
+ *
11
+ * This type extracts the TOutput type parameter, which is set by readValidator()
12
+ * and represents the transformed/validated output type.
13
+ */
14
+ export type InferFieldOutput<F> =
15
+ F extends FieldBuilder<infer TOutput, any, any, any> ? TOutput : never;
16
+
17
+ /**
18
+ * Extract the input type from a FieldBuilder.
19
+ * This is what you pass when writing to the database.
20
+ *
21
+ * This type extracts the TInput type parameter, which is set by writeValidator()
22
+ * and represents the transformed/validated input type.
23
+ */
24
+ type InferFieldInput<F> =
25
+ F extends FieldBuilder<any, infer TInput, any, any> ? TInput : never;
26
+
27
+ /**
28
+ * Build a schema type from field builders (output/read types).
29
+ */
30
+ type InferSchemaFromFields<
31
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
32
+ > = {
33
+ [K in keyof TFields]: InferFieldOutput<TFields[K]>;
34
+ };
35
+
36
+ /**
37
+ * Build an input schema type from field builders (input/write types).
38
+ * Used for insert and update operations.
39
+ */
40
+ type InferInputSchemaFromFields<
41
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
42
+ > = {
43
+ [K in keyof TFields]: InferFieldInput<TFields[K]>;
44
+ };
45
+
46
+ /**
47
+ * Check if a field is a container field by inspecting its TDbType.
48
+ * Container fields have a branded TDbType that extends ContainerDbType.
49
+ */
50
+ type IsContainerField<F> =
51
+ F extends FieldBuilder<any, any, infer TDbType, any>
52
+ ? NonNullable<TDbType> extends ContainerDbType
53
+ ? true
54
+ : false
55
+ : false;
56
+
57
+ /**
58
+ * Extract only selectable (non-container) field keys from a fields record.
59
+ * Container fields are excluded because they cannot be selected via $select in FileMaker OData.
60
+ */
61
+ type SelectableFieldKeys<
62
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
63
+ > = {
64
+ [K in keyof TFields]: IsContainerField<TFields[K]> extends true ? never : K;
65
+ }[keyof TFields];
66
+
67
+ /**
68
+ * Build a schema type excluding container fields (for query return types).
69
+ * This is used to ensure container fields don't appear in the return type
70
+ * when using defaultSelect: "schema" or "all".
71
+ */
72
+ type InferSelectableSchemaFromFields<
73
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
74
+ > = {
75
+ [K in SelectableFieldKeys<TFields>]: InferFieldOutput<TFields[K]>;
76
+ };
77
+
78
+ /**
79
+ * Internal Symbols for table properties (hidden from IDE autocomplete).
80
+ * These are used to store internal configuration that shouldn't be visible
81
+ * when users access table columns.
82
+ * @internal - Not exported from public API, only accessible via FMTable.Symbol
83
+ */
84
+ const FMTableName = Symbol.for("fmodata:FMTableName");
85
+ const FMTableEntityId = Symbol.for("fmodata:FMTableEntityId");
86
+ const FMTableSchema = Symbol.for("fmodata:FMTableSchema");
87
+ const FMTableFields = Symbol.for("fmodata:FMTableFields");
88
+ const FMTableNavigationPaths = Symbol.for("fmodata:FMTableNavigationPaths");
89
+ const FMTableDefaultSelect = Symbol.for("fmodata:FMTableDefaultSelect");
90
+ const FMTableBaseTableConfig = Symbol.for("fmodata:FMTableBaseTableConfig");
91
+ const FMTableUseEntityIds = Symbol.for("fmodata:FMTableUseEntityIds");
92
+
93
+ /**
94
+ * Base table class with Symbol-based internal properties.
95
+ * This follows the Drizzle ORM pattern where internal configuration
96
+ * is stored via Symbols, keeping it hidden from IDE autocomplete.
97
+ */
98
+ export class FMTable<
99
+ TFields extends Record<string, FieldBuilder<any, any, any, any>> = any,
100
+ TName extends string = string,
101
+ TNavigationPaths extends readonly string[] = readonly string[],
102
+ > {
103
+ /**
104
+ * Internal Symbols for accessing table metadata.
105
+ * @internal - Not intended for public use. Access table properties via columns instead.
106
+ */
107
+ static readonly Symbol = {
108
+ Name: FMTableName,
109
+ EntityId: FMTableEntityId,
110
+ UseEntityIds: FMTableUseEntityIds,
111
+ Schema: FMTableSchema,
112
+ Fields: FMTableFields,
113
+ NavigationPaths: FMTableNavigationPaths,
114
+ DefaultSelect: FMTableDefaultSelect,
115
+ BaseTableConfig: FMTableBaseTableConfig,
116
+ };
117
+
118
+ /** @internal */
119
+ [FMTableName]: TName;
120
+
121
+ /** @internal */
122
+ [FMTableEntityId]?: `FMTID:${string}`;
123
+
124
+ /** @internal */
125
+ [FMTableUseEntityIds]?: boolean;
126
+
127
+ /** @internal */
128
+ [FMTableSchema]: StandardSchemaV1<any, InferSchemaFromFields<TFields>>;
129
+
130
+ /** @internal */
131
+ [FMTableFields]: TFields;
132
+
133
+ /** @internal */
134
+ [FMTableNavigationPaths]: TNavigationPaths;
135
+
136
+ /** @internal */
137
+ [FMTableDefaultSelect]: "all" | "schema" | Record<string, Column<any, TName>>;
138
+
139
+ /** @internal */
140
+ [FMTableBaseTableConfig]: {
141
+ schema: Record<keyof TFields, StandardSchemaV1>;
142
+ inputSchema?: Record<keyof TFields, StandardSchemaV1>;
143
+ idField?: keyof TFields;
144
+ required: readonly (keyof TFields)[];
145
+ readOnly: readonly (keyof TFields)[];
146
+ containerFields: readonly (keyof TFields)[];
147
+ fmfIds?: Record<keyof TFields, `FMFID:${string}`>;
148
+ };
149
+
150
+ constructor(config: {
151
+ name: TName;
152
+ entityId?: `FMTID:${string}`;
153
+ useEntityIds?: boolean;
154
+ schema: StandardSchemaV1<any, InferSchemaFromFields<TFields>>;
155
+ fields: TFields;
156
+ navigationPaths: TNavigationPaths;
157
+ defaultSelect: "all" | "schema" | Record<string, Column<any, TName>>;
158
+ baseTableConfig: {
159
+ schema: Record<keyof TFields, StandardSchemaV1>;
160
+ inputSchema?: Record<keyof TFields, StandardSchemaV1>;
161
+ idField?: keyof TFields;
162
+ required: readonly (keyof TFields)[];
163
+ readOnly: readonly (keyof TFields)[];
164
+ containerFields: readonly (keyof TFields)[];
165
+ fmfIds?: Record<keyof TFields, `FMFID:${string}`>;
166
+ };
167
+ }) {
168
+ this[FMTableName] = config.name;
169
+ this[FMTableEntityId] = config.entityId;
170
+ this[FMTableUseEntityIds] = config.useEntityIds;
171
+ this[FMTableSchema] = config.schema;
172
+ this[FMTableFields] = config.fields;
173
+ this[FMTableNavigationPaths] = config.navigationPaths;
174
+ this[FMTableDefaultSelect] = config.defaultSelect;
175
+ this[FMTableBaseTableConfig] = config.baseTableConfig;
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Type helper to extract the column map from fields.
181
+ * Table name is baked into each column type for validation.
182
+ * Container fields are marked with IsContainer=true.
183
+ */
184
+ export type ColumnMap<
185
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
186
+ TName extends string,
187
+ > = {
188
+ [K in keyof TFields]: Column<
189
+ InferFieldOutput<TFields[K]>,
190
+ TName,
191
+ IsContainerField<TFields[K]>
192
+ >;
193
+ };
194
+
195
+ /**
196
+ * Extract only selectable (non-container) columns from a table.
197
+ * This is used to prevent selecting container fields in queries.
198
+ */
199
+ export type SelectableColumnMap<
200
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
201
+ TName extends string,
202
+ > = {
203
+ [K in SelectableFieldKeys<TFields>]: Column<
204
+ InferFieldOutput<TFields[K]>,
205
+ TName,
206
+ false
207
+ >;
208
+ };
209
+
210
+ /**
211
+ * Validates that a select object doesn't contain container field columns.
212
+ * Returns never if any container fields are found, otherwise returns the original type.
213
+ */
214
+ export type ValidateNoContainerFields<
215
+ TSelect extends Record<string, Column<any, any, any>>,
216
+ > = {
217
+ [K in keyof TSelect]: TSelect[K] extends Column<any, any, true>
218
+ ? never
219
+ : TSelect[K];
220
+ } extends TSelect
221
+ ? TSelect
222
+ : {
223
+ [K in keyof TSelect]: TSelect[K] extends Column<any, any, true>
224
+ ? "❌ Container fields cannot be selected. Use .getSingleField() instead."
225
+ : TSelect[K];
226
+ };
227
+
228
+ /**
229
+ * Extract the keys from a defaultSelect function's return type.
230
+ * Used to infer which fields are selected by default for type narrowing.
231
+ */
232
+ type ExtractDefaultSelectKeys<
233
+ TDefaultSelect,
234
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
235
+ TName extends string,
236
+ > = TDefaultSelect extends (columns: ColumnMap<TFields, TName>) => infer R
237
+ ? keyof R
238
+ : TDefaultSelect extends "schema"
239
+ ? keyof TFields
240
+ : keyof TFields; // "all" defaults to all keys
241
+
242
+ /**
243
+ * Complete table type with both metadata (via Symbols) and column accessors.
244
+ * This is the return type of fmTableOccurrence - users see columns directly,
245
+ * but internal config is hidden via Symbols.
246
+ */
247
+ export type FMTableWithColumns<
248
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
249
+ TName extends string,
250
+ TNavigationPaths extends readonly string[] = readonly string[],
251
+ > = FMTable<TFields, TName, TNavigationPaths> & ColumnMap<TFields, TName>;
252
+
253
+ /**
254
+ * Options for fmTableOccurrence function.
255
+ * Provides autocomplete-friendly typing while preserving inference for navigationPaths.
256
+ */
257
+ export interface FMTableOccurrenceOptions<
258
+ TFields extends Record<string, FieldBuilder<any, any, any, any>>,
259
+ TName extends string,
260
+ > {
261
+ /** The entity ID (FMTID) for this table occurrence */
262
+ entityId?: `FMTID:${string}`;
263
+
264
+ /**
265
+ * Default select behavior:
266
+ * - "all": Select all fields (including related tables)
267
+ * - "schema": Select only schema-defined fields (default)
268
+ * - function: Custom selection from columns
269
+ */
270
+ defaultSelect?:
271
+ | "all"
272
+ | "schema"
273
+ | ((
274
+ columns: ColumnMap<TFields, TName>,
275
+ ) => Record<string, Column<any, TName>>);
276
+
277
+ /** Navigation paths available from this table (for expand operations) */
278
+ navigationPaths?: readonly string[];
279
+
280
+ /** Whether to use entity IDs (FMTID/FMFID) instead of names in queries */
281
+ useEntityIds?: boolean;
282
+ }
283
+
284
+ /**
285
+ * Create a table occurrence with field builders.
286
+ * This is the main API for defining tables in the new ORM style.
287
+ *
288
+ * @example
289
+ * const users = fmTableOccurrence("users", {
290
+ * id: textField().primaryKey().entityId("FMFID:1"),
291
+ * name: textField().notNull().entityId("FMFID:6"),
292
+ * active: numberField()
293
+ * .outputValidator(z.coerce.boolean())
294
+ * .inputValidator(z.boolean().transform(v => v ? 1 : 0))
295
+ * .entityId("FMFID:7"),
296
+ * }, {
297
+ * entityId: "FMTID:100",
298
+ * defaultSelect: "schema",
299
+ * navigationPaths: ["contacts"],
300
+ * });
301
+ *
302
+ * // Access columns
303
+ * users.id // Column<string, "id">
304
+ * users.name // Column<string, "name">
305
+ *
306
+ * // Use in queries
307
+ * db.from(users).select("id", "name").where(eq(users.active, true))
308
+ */
309
+ export function fmTableOccurrence<
310
+ const TName extends string,
311
+ const TFields extends Record<string, FieldBuilder<any, any, any, any>>,
312
+ const TNavPaths extends readonly string[] = readonly [],
313
+ >(
314
+ name: TName,
315
+ fields: TFields,
316
+ options?: FMTableOccurrenceOptions<TFields, TName> & {
317
+ /** Navigation paths available from this table (for expand operations) */
318
+ navigationPaths?: TNavPaths;
319
+ },
320
+ ): FMTableWithColumns<TFields, TName, TNavPaths> {
321
+ // Extract configuration from field builders
322
+ const fieldConfigs = Object.entries(fields).map(([fieldName, builder]) => ({
323
+ fieldName,
324
+ config: (builder as any)._getConfig(),
325
+ }));
326
+
327
+ // Find primary key field
328
+ const primaryKeyField = fieldConfigs.find((f) => f.config.primaryKey);
329
+ const idField = primaryKeyField?.fieldName;
330
+
331
+ // Collect required fields (notNull fields)
332
+ const required = fieldConfigs
333
+ .filter((f) => f.config.notNull)
334
+ .map((f) => f.fieldName);
335
+
336
+ // Collect read-only fields
337
+ const readOnly = fieldConfigs
338
+ .filter((f) => f.config.readOnly)
339
+ .map((f) => f.fieldName);
340
+
341
+ // Collect container fields (cannot be selected via $select)
342
+ const containerFields = fieldConfigs
343
+ .filter((f) => f.config.fieldType === "container")
344
+ .map((f) => f.fieldName);
345
+
346
+ // Collect entity IDs
347
+ const fmfIds: Record<string, `FMFID:${string}`> = {};
348
+ for (const { fieldName, config } of fieldConfigs) {
349
+ if (config.entityId) {
350
+ fmfIds[fieldName] = config.entityId;
351
+ }
352
+ }
353
+
354
+ // Build Zod schema from field builders (output/read validators)
355
+ const zodSchema: Record<string, StandardSchemaV1> = {};
356
+ // Build input schema from field builders (input/write validators)
357
+ const inputSchema: Record<string, StandardSchemaV1> = {};
358
+
359
+ for (const { fieldName, config } of fieldConfigs) {
360
+ // Use outputValidator if provided, otherwise create a basic validator
361
+ if (config.outputValidator) {
362
+ zodSchema[fieldName] = config.outputValidator;
363
+ } else {
364
+ // Create a default validator based on field type and nullability
365
+ let validator: any;
366
+ switch (config.fieldType) {
367
+ case "text":
368
+ case "date":
369
+ case "time":
370
+ case "timestamp":
371
+ case "container":
372
+ case "calculated":
373
+ validator = z.string();
374
+ break;
375
+ case "number":
376
+ validator = z.number();
377
+ break;
378
+ default:
379
+ validator = z.unknown();
380
+ }
381
+
382
+ // Add nullability if not marked as notNull
383
+ if (!config.notNull) {
384
+ validator = validator.nullable();
385
+ }
386
+
387
+ zodSchema[fieldName] = validator;
388
+ }
389
+
390
+ // Store inputValidator if provided (for write operations)
391
+ if (config.inputValidator) {
392
+ inputSchema[fieldName] = config.inputValidator;
393
+ }
394
+ }
395
+
396
+ // Create a schema validator for the entire table
397
+ const tableSchema = z.object(zodSchema) as unknown as StandardSchemaV1<
398
+ any,
399
+ InferSchemaFromFields<TFields>
400
+ >;
401
+
402
+ // Build BaseTable-compatible config
403
+ const baseTableConfig = {
404
+ schema: zodSchema as Record<keyof TFields, StandardSchemaV1>,
405
+ inputSchema: (Object.keys(inputSchema).length > 0
406
+ ? inputSchema
407
+ : undefined) as Record<keyof TFields, StandardSchemaV1> | undefined,
408
+ idField: idField as keyof TFields | undefined,
409
+ required: required as readonly (keyof TFields)[],
410
+ readOnly: readOnly as readonly (keyof TFields)[],
411
+ containerFields: containerFields as readonly (keyof TFields)[],
412
+ fmfIds: (Object.keys(fmfIds).length > 0 ? fmfIds : undefined) as
413
+ | Record<keyof TFields, `FMFID:${string}`>
414
+ | undefined,
415
+ };
416
+
417
+ // Create column instances
418
+ const columns: Record<string, Column> = {};
419
+ for (const [fieldName, builder] of Object.entries(fields)) {
420
+ const config = (builder as any)._getConfig();
421
+ columns[fieldName] = new Column({
422
+ fieldName: String(fieldName),
423
+ entityId: config.entityId,
424
+ tableName: name,
425
+ tableEntityId: options?.entityId,
426
+ });
427
+ }
428
+
429
+ // Resolve defaultSelect: if it's a function, call it with columns; otherwise use as-is
430
+ const defaultSelectOption = options?.defaultSelect ?? "schema";
431
+ const resolvedDefaultSelect:
432
+ | "all"
433
+ | "schema"
434
+ | Record<string, Column<any, TName>> =
435
+ typeof defaultSelectOption === "function"
436
+ ? defaultSelectOption(columns as ColumnMap<TFields, TName>)
437
+ : defaultSelectOption;
438
+
439
+ // Create the FMTable instance with Symbol-based internal properties
440
+ const navigationPaths = (options?.navigationPaths ?? []) as TNavPaths;
441
+ const table = new FMTable<TFields, TName, TNavPaths>({
442
+ name,
443
+ entityId: options?.entityId,
444
+ useEntityIds: options?.useEntityIds,
445
+ schema: tableSchema,
446
+ fields,
447
+ navigationPaths,
448
+ defaultSelect: resolvedDefaultSelect,
449
+ baseTableConfig,
450
+ });
451
+
452
+ // Assign columns to the table instance (making them accessible directly)
453
+ Object.assign(table, columns);
454
+
455
+ return table as FMTableWithColumns<TFields, TName, TNavPaths>;
456
+ }
457
+
458
+ // /**
459
+ // * Type guard to check if a value is a TableOccurrence or FMTable.
460
+ // * Supports both Symbol-based (new) and underscore-prefixed (legacy) formats.
461
+ // */
462
+ // function isTableOccurrence(value: any): value is TableOccurrence {
463
+ // if (!value || typeof value !== "object") {
464
+ // return false;
465
+ // }
466
+
467
+ // // Check for Symbol-based format (new FMTable class)
468
+ // if (
469
+ // FMTableName in value &&
470
+ // FMTableSchema in value &&
471
+ // FMTableFields in value
472
+ // ) {
473
+ // return typeof value[FMTableName] === "string";
474
+ // }
475
+
476
+ // // Check for underscore-prefixed format (legacy interface)
477
+ // if ("_name" in value && "_schema" in value && "_fields" in value) {
478
+ // return typeof value._name === "string";
479
+ // }
480
+
481
+ // return false;
482
+ // }
483
+
484
+ /**
485
+ * Helper to extract the schema type from a TableOccurrence or FMTable.
486
+ */
487
+ export type InferTableSchema<T> =
488
+ T extends FMTable<infer TFields, any>
489
+ ? InferSchemaFromFields<TFields>
490
+ : never;
491
+
492
+ /**
493
+ * Extract the schema type from an FMTable instance.
494
+ * This is used to infer the schema from table objects passed to db.from(), expand(), etc.
495
+ */
496
+ export type InferSchemaOutputFromFMTable<T extends FMTable<any, any>> =
497
+ T extends FMTable<infer TFields, any>
498
+ ? InferSchemaFromFields<TFields>
499
+ : never;
500
+
501
+ /**
502
+ * Extract the input schema type from an FMTable instance.
503
+ * This is used for insert and update operations where we need write types.
504
+ */
505
+ export type InferInputSchemaFromFMTable<T extends FMTable<any, any>> =
506
+ T extends FMTable<infer TFields, any>
507
+ ? InferInputSchemaFromFields<TFields>
508
+ : never;
509
+
510
+ /**
511
+ * Helper type to check if a FieldBuilder's input type excludes null and undefined.
512
+ * This checks the TInput type parameter, which preserves nullability from notNull().
513
+ */
514
+ type FieldInputExcludesNullish<F> =
515
+ F extends FieldBuilder<any, infer TInput, any>
516
+ ? null extends TInput
517
+ ? false
518
+ : undefined extends TInput
519
+ ? false
520
+ : true
521
+ : false;
522
+
523
+ /**
524
+ * Check if a FieldBuilder is readOnly at the type level
525
+ */
526
+ type IsFieldReadOnly<F> =
527
+ F extends FieldBuilderType<any, any, any, infer ReadOnly>
528
+ ? ReadOnly extends true
529
+ ? true
530
+ : false
531
+ : false;
532
+
533
+ /**
534
+ * Compute insert data type from FMTable, making notNull fields required.
535
+ * Fields are required if their FieldBuilder's TInput type excludes null/undefined.
536
+ * All other fields are optional (can be omitted).
537
+ * readOnly fields are excluded (including primaryKey/idField since they're automatically readOnly).
538
+ */
539
+ export type InsertDataFromFMTable<T extends FMTable<any, any>> =
540
+ T extends FMTable<infer TFields, any>
541
+ ? {
542
+ [K in keyof TFields as IsFieldReadOnly<TFields[K]> extends true
543
+ ? never
544
+ : FieldInputExcludesNullish<TFields[K]> extends true
545
+ ? K
546
+ : never]: InferFieldInput<TFields[K]>;
547
+ } & {
548
+ [K in keyof TFields as IsFieldReadOnly<TFields[K]> extends true
549
+ ? never
550
+ : FieldInputExcludesNullish<TFields[K]> extends true
551
+ ? never
552
+ : K]?: InferFieldInput<TFields[K]>;
553
+ }
554
+ : never;
555
+
556
+ /**
557
+ * Compute update data type from FMTable.
558
+ * All fields are optional, but readOnly fields are excluded (including primaryKey/idField).
559
+ */
560
+ export type UpdateDataFromFMTable<T extends FMTable<any, any>> =
561
+ T extends FMTable<infer TFields, any>
562
+ ? {
563
+ [K in keyof TFields as IsFieldReadOnly<TFields[K]> extends true
564
+ ? never
565
+ : K]?: InferFieldInput<TFields[K]>;
566
+ }
567
+ : never;
568
+
569
+ /**
570
+ * Extract the table name type from an FMTable.
571
+ * This is a workaround since we can't directly index Symbols in types.
572
+ */
573
+ export type ExtractTableName<T extends FMTable<any, any>> =
574
+ T extends FMTable<any, infer Name> ? Name : never;
575
+
576
+ /**
577
+ * Validates that a target table's name matches one of the source table's navigationPaths.
578
+ * Used to ensure type-safe expand/navigate operations.
579
+ */
580
+ export type ValidExpandTarget<
581
+ SourceTable extends FMTable<any, any, any> | undefined,
582
+ TargetTable extends FMTable<any, any, any>,
583
+ > =
584
+ SourceTable extends FMTable<any, any, infer SourceNavPaths>
585
+ ? ExtractTableName<TargetTable> extends SourceNavPaths[number]
586
+ ? TargetTable
587
+ : never
588
+ : TargetTable;
589
+
590
+ // ============================================================================
591
+ // Helper Functions for Accessing FMTable Internal Properties
592
+ // ============================================================================
593
+
594
+ /**
595
+ * Get the table name from an FMTable instance.
596
+ * @param table - FMTable instance
597
+ * @returns The table name
598
+ */
599
+ export function getTableName<T extends FMTable<any, any>>(table: T): string {
600
+ return table[FMTableName];
601
+ }
602
+
603
+ /**
604
+ * Get the entity ID (FMTID) from an FMTable instance.
605
+ * @param table - FMTable instance
606
+ * @returns The entity ID or undefined if not using entity IDs
607
+ */
608
+ export function getTableEntityId<T extends FMTable<any, any>>(
609
+ table: T,
610
+ ): string | undefined {
611
+ return table[FMTableEntityId];
612
+ }
613
+
614
+ /**
615
+ * Get the schema validator from an FMTable instance.
616
+ * @param table - FMTable instance
617
+ * @returns The StandardSchemaV1 validator
618
+ */
619
+ export function getTableSchema<T extends FMTable<any, any>>(
620
+ table: T,
621
+ ): StandardSchemaV1 {
622
+ return table[FMTableSchema];
623
+ }
624
+
625
+ /**
626
+ * Get the fields from an FMTable instance.
627
+ * @param table - FMTable instance
628
+ * @returns The fields record
629
+ */
630
+ export function getTableFields<T extends FMTable<any, any>>(table: T) {
631
+ return table[FMTableFields];
632
+ }
633
+
634
+ /**
635
+ * Get the navigation paths from an FMTable instance.
636
+ * @param table - FMTable instance
637
+ * @returns Array of navigation path names
638
+ */
639
+ export function getNavigationPaths<T extends FMTable<any, any>>(
640
+ table: T,
641
+ ): readonly string[] {
642
+ return table[FMTableNavigationPaths];
643
+ }
644
+
645
+ /**
646
+ * Get the default select configuration from an FMTable instance.
647
+ * @param table - FMTable instance
648
+ * @returns Default select configuration
649
+ */
650
+ export function getDefaultSelect<T extends FMTable<any, any>>(table: T) {
651
+ return table[FMTableDefaultSelect];
652
+ }
653
+
654
+ /**
655
+ * Get the base table configuration from an FMTable instance.
656
+ * This provides access to schema, idField, required fields, readOnly fields, and field IDs.
657
+ * @param table - FMTable instance
658
+ * @returns Base table configuration object
659
+ */
660
+ export function getBaseTableConfig<T extends FMTable<any, any>>(table: T) {
661
+ return table[FMTableBaseTableConfig];
662
+ }
663
+
664
+ /**
665
+ * Check if an FMTable instance is using entity IDs (both FMTID and FMFIDs).
666
+ * @param table - FMTable instance
667
+ * @returns True if using entity IDs, false otherwise
668
+ */
669
+ export function isUsingEntityIds<T extends FMTable<any, any>>(
670
+ table: T,
671
+ ): boolean {
672
+ return (
673
+ table[FMTableEntityId] !== undefined &&
674
+ table[FMTableBaseTableConfig].fmfIds !== undefined
675
+ );
676
+ }
677
+
678
+ /**
679
+ * Get the field ID (FMFID) for a given field name, or the field name itself if not using IDs.
680
+ * @param table - FMTable instance
681
+ * @param fieldName - Field name to get the ID for
682
+ * @returns The FMFID string or the original field name
683
+ */
684
+ export function getFieldId<T extends FMTable<any, any>>(
685
+ table: T,
686
+ fieldName: string,
687
+ ): string {
688
+ const config = table[FMTableBaseTableConfig];
689
+ if (config.fmfIds && fieldName in config.fmfIds) {
690
+ const fieldId = config.fmfIds[fieldName];
691
+ if (fieldId) {
692
+ return fieldId;
693
+ }
694
+ }
695
+ return fieldName;
696
+ }
697
+
698
+ /**
699
+ * Get the field name for a given field ID (FMFID), or the ID itself if not found.
700
+ * @param table - FMTable instance
701
+ * @param fieldId - The FMFID to get the field name for
702
+ * @returns The field name or the original ID
703
+ */
704
+ export function getFieldName<T extends FMTable<any, any>>(
705
+ table: T,
706
+ fieldId: string,
707
+ ): string {
708
+ const config = table[FMTableBaseTableConfig];
709
+ if (config.fmfIds) {
710
+ for (const [fieldName, fmfId] of Object.entries(config.fmfIds)) {
711
+ if (fmfId === fieldId) {
712
+ return fieldName;
713
+ }
714
+ }
715
+ }
716
+ return fieldId;
717
+ }
718
+ /**
719
+ * Get the table ID (FMTID or name) from an FMTable instance.
720
+ * Returns the FMTID if available, otherwise returns the table name.
721
+ * @param table - FMTable instance
722
+ * @returns The FMTID string or the table name
723
+ */
724
+ export function getTableId<T extends FMTable<any, any>>(table: T): string {
725
+ return table[FMTableEntityId] ?? table[FMTableName];
726
+ }
727
+
728
+ /**
729
+ * Get all columns from a table as an object.
730
+ * Useful for selecting all fields except some using destructuring.
731
+ *
732
+ * @example
733
+ * const { password, ...cols } = getTableColumns(users)
734
+ * db.from(users).list().select(cols)
735
+ *
736
+ * @param table - FMTable instance
737
+ * @returns Object with all columns from the table
738
+ */
739
+ export function getTableColumns<T extends FMTableWithColumns<any, any>>(
740
+ table: T,
741
+ ): ColumnMap<T[typeof FMTableFields], ExtractTableName<T>> {
742
+ const fields = table[FMTableFields];
743
+ const tableName = table[FMTableName];
744
+ const tableEntityId = table[FMTableEntityId];
745
+ const baseConfig = table[FMTableBaseTableConfig];
746
+
747
+ const columns: Record<string, Column> = {};
748
+ for (const [fieldName, builder] of Object.entries(fields)) {
749
+ const config = (builder as any)._getConfig();
750
+ columns[fieldName] = new Column({
751
+ fieldName: String(fieldName),
752
+ entityId: baseConfig.fmfIds?.[fieldName],
753
+ tableName: tableName,
754
+ tableEntityId: tableEntityId,
755
+ });
756
+ }
757
+
758
+ return columns as ColumnMap<T[typeof FMTableFields], ExtractTableName<T>>;
759
+ }