@sequenceholdings/orm 2.1.0 → 2.1.1

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 (69) hide show
  1. package/README.md +38 -1
  2. package/dist/authoring.cjs +1358 -45
  3. package/dist/authoring.cjs.map +1 -1
  4. package/dist/authoring.d.cts +223 -2
  5. package/dist/authoring.d.ts +223 -2
  6. package/dist/authoring.js +1349 -47
  7. package/dist/authoring.js.map +1 -1
  8. package/dist/canonical.cjs +662 -38
  9. package/dist/canonical.cjs.map +1 -1
  10. package/dist/canonical.d.cts +696 -53
  11. package/dist/canonical.d.ts +696 -53
  12. package/dist/canonical.js +647 -39
  13. package/dist/canonical.js.map +1 -1
  14. package/dist/cli.cjs +1808 -247
  15. package/dist/cli.cjs.map +1 -1
  16. package/dist/cli.js +1809 -248
  17. package/dist/cli.js.map +1 -1
  18. package/dist/client.cjs +90 -25
  19. package/dist/client.cjs.map +1 -1
  20. package/dist/client.d.cts +39 -22
  21. package/dist/client.d.ts +39 -22
  22. package/dist/client.js +87 -26
  23. package/dist/client.js.map +1 -1
  24. package/dist/compiler.cjs +1325 -285
  25. package/dist/compiler.cjs.map +1 -1
  26. package/dist/compiler.d.cts +23 -2
  27. package/dist/compiler.d.ts +23 -2
  28. package/dist/compiler.js +1325 -285
  29. package/dist/compiler.js.map +1 -1
  30. package/dist/constants-YoFGjIBM.d.cts +99 -0
  31. package/dist/constants-YoFGjIBM.d.ts +99 -0
  32. package/dist/dynamic-input.d.cts +1 -0
  33. package/dist/dynamic-input.d.ts +1 -0
  34. package/dist/index.cjs +1605 -140
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.cts +230 -113
  37. package/dist/index.d.ts +230 -113
  38. package/dist/index.js +1583 -142
  39. package/dist/index.js.map +1 -1
  40. package/dist/ir.cjs +727 -38
  41. package/dist/ir.cjs.map +1 -1
  42. package/dist/ir.d.cts +539 -21
  43. package/dist/ir.d.ts +539 -21
  44. package/dist/ir.js +722 -39
  45. package/dist/ir.js.map +1 -1
  46. package/dist/{naming-DCn94LJ4.d.ts → naming-CPfx7EfF.d.ts} +14 -1
  47. package/dist/{naming-DPglwzkB.d.cts → naming-CU9ThP_E.d.cts} +14 -1
  48. package/dist/policy-1iZbGnZZ.d.cts +24 -0
  49. package/dist/policy-1iZbGnZZ.d.ts +24 -0
  50. package/dist/predicate-v2.cjs +726 -42
  51. package/dist/predicate-v2.cjs.map +1 -1
  52. package/dist/predicate-v2.d.cts +2 -1
  53. package/dist/predicate-v2.d.ts +2 -1
  54. package/dist/predicate-v2.js +726 -42
  55. package/dist/predicate-v2.js.map +1 -1
  56. package/dist/sdl.cjs +858 -54
  57. package/dist/sdl.cjs.map +1 -1
  58. package/dist/sdl.d.cts +7 -3
  59. package/dist/sdl.d.ts +7 -3
  60. package/dist/sdl.js +856 -55
  61. package/dist/sdl.js.map +1 -1
  62. package/package.json +9 -4
  63. package/templates/orm-namespace/CLAUDE.md +6 -4
  64. package/templates/orm-namespace/codegen.preset.d.cts +7 -0
  65. package/templates/orm-namespace/codegen.ts +2 -1
  66. package/templates/orm-namespace/graphql/example/queries.ts +4 -3
  67. package/templates/orm-namespace/package.json +2 -1
  68. package/templates/orm-namespace/schema/tables.ts +3 -2
  69. package/templates/orm-namespace/sequence.config.ts +7 -11
@@ -1,8 +1,10 @@
1
- import { ColumnType, CanonicalColumn, CanonicalDefinition, CanonicalTable } from './canonical.cjs';
1
+ import { ColumnType, CanonicalColumn, ComparatorKind, CanonicalFunctionRef, CanonicalFetchShape, CanonicalDefinition, CanonicalTable } from './canonical.cjs';
2
+ import { b as CellPart } from './constants-YoFGjIBM.cjs';
2
3
  import { ColumnsSelection } from 'drizzle-orm';
3
4
  import { TypedQueryBuilder } from 'drizzle-orm/query-builders/query-builder';
4
5
  import { QueryBuilder, PgView, PgTable, AnyPgColumn } from 'drizzle-orm/pg-core';
5
6
  import 'zod';
7
+ import './policy-1iZbGnZZ.cjs';
6
8
 
7
9
  /**
8
10
  * Fluent column builders for the v2 authoring surface — `t.uuid()`,
@@ -61,6 +63,8 @@ declare class ColumnBuilder {
61
63
  cast(usingSql: string): this;
62
64
  /** Emits `@deprecated` in the generated SDL; the column stays queryable. */
63
65
  deprecated(): this;
66
+ /** @internal System cell-metadata part of logical column `of` (generated resolved views only). */
67
+ cellPart(of: string, part: CellPart): this;
64
68
  /** Whether `.primaryKey()` was called — collected into the table-level PK. */
65
69
  get isPrimaryKey(): boolean;
66
70
  /** The enum name when this is an enum column, else undefined. */
@@ -179,6 +183,8 @@ interface RevertOverlayMutationInput<TOverlay extends OverlayTableRef = OverlayT
179
183
  * ],
180
184
  * })
181
185
  * ```
186
+ *
187
+ * @deprecated Resolved tables write sparse cells directly (`defineResolvedTable`). Kept so existing definitions still compile.
182
188
  */
183
189
  declare function overrideRowMutation<TOverlay extends OverlayTableRef, TSynced extends SeqTable>(input: OverrideRowMutationInput<TOverlay, TSynced>): CowMutationInput;
184
190
  /**
@@ -187,9 +193,182 @@ declare function overrideRowMutation<TOverlay extends OverlayTableRef, TSynced e
187
193
  *
188
194
  * The DELETE is gated on a matching synced row — overlay-only creates (or rows
189
195
  * whose synced source disappeared) are not deleted by revert.
196
+ *
197
+ * @deprecated Resolved tables write sparse cells directly (`defineResolvedTable`). Kept so existing definitions still compile.
190
198
  */
191
199
  declare function revertOverlayMutation<TOverlay extends OverlayTableRef, TSynced extends SeqTable>(input: RevertOverlayMutationInput<TOverlay, TSynced>): CowMutationInput;
192
200
 
201
+ /**
202
+ * `defineResolvedTable` — one logical table backed by manual, etl and
203
+ * prefetch sources, with a SQL comparator per column.
204
+ *
205
+ * It expands into existing primitives so DDL, apply, SDL and policy
206
+ * compilation stay shared:
207
+ *
208
+ * - one internal table per manual/prefetch source (`<name>__<source>`,
209
+ * `cellMetadata: true`, sparse nullable columns),
210
+ * - the author's own synced tables for etl sources,
211
+ * - a managed view named `<name>` whose generated query unions every
212
+ * source's primary keys (so `pk = $1` pushes down into each source),
213
+ * left-joins each source back, and picks every column's winner with a
214
+ * CASE over typed per-cell timestamps (built-in comparators) or a small
215
+ * ORDER BY … LIMIT 1 lateral (custom).
216
+ *
217
+ * All comparators run in SQL — never app code — so reads stay one statement
218
+ * and filters/policies see resolved values.
219
+ */
220
+
221
+ /** A duration: milliseconds, or a string like `'1500ms'`, `'30s'`, `'5m'`. */
222
+ type DurationInput = number | `${number}ms` | `${number}s` | `${number}m`;
223
+ type FunctionRef = CanonicalFunctionRef;
224
+ /** A Managed Function (id or slug) that implements a prefetch source's fetch and writeback. */
225
+ declare function managedFn(id: string): FunctionRef;
226
+ interface ManualSourceInput {
227
+ readonly kind: 'manual';
228
+ }
229
+ interface EtlSourceInput {
230
+ readonly kind: 'etl';
231
+ /** A `defineSyncedTable` relation fed by a pipeline. */
232
+ readonly table: RelationRef;
233
+ /** Property on the synced table whose value timestamps every cell (e.g. an ingested_at column). */
234
+ readonly updatedAt?: string;
235
+ /** Resolved columns this source supplies; defaults to every column the synced table shares by name. */
236
+ readonly columns?: readonly string[];
237
+ }
238
+ interface PrefetchSourceInput {
239
+ readonly kind: 'prefetch';
240
+ readonly function: FunctionRef;
241
+ /** Resolved column holding the source system's key (may be the primary key). */
242
+ readonly key: string;
243
+ /** Resolved columns the function returns. */
244
+ readonly columns: readonly string[];
245
+ /** What the source API supports: one key per call, or batches. */
246
+ readonly fetch: 'single' | {
247
+ readonly batch: {
248
+ readonly maxKeys: number;
249
+ };
250
+ };
251
+ readonly prefetchDefaults?: {
252
+ readonly maxStaleness?: DurationInput;
253
+ readonly timeout?: DurationInput;
254
+ readonly onError?: 'fallback' | 'fail';
255
+ };
256
+ readonly writeback?: {
257
+ readonly columns: readonly string[];
258
+ readonly defaultMode?: 'sync' | 'async';
259
+ readonly timeout?: DurationInput;
260
+ readonly clearManualOnConfirm?: boolean;
261
+ };
262
+ }
263
+ type ResolvedSourceInput = ManualSourceInput | EtlSourceInput | PrefetchSourceInput;
264
+ declare function manualSource(): ManualSourceInput;
265
+ declare function etlSource(input: Omit<EtlSourceInput, 'kind'>): EtlSourceInput;
266
+ declare function prefetchSource(input: Omit<PrefetchSourceInput, 'kind'>): PrefetchSourceInput;
267
+ interface Comparator {
268
+ readonly kind: ComparatorKind;
269
+ readonly candidates: readonly string[];
270
+ readonly orderBy?: string;
271
+ }
272
+ /** Newest `updated_at` wins; source order breaks ties. */
273
+ declare const latest: (candidates: readonly string[]) => Comparator;
274
+ /** Largest value wins. */
275
+ declare const highest: (candidates: readonly string[]) => Comparator;
276
+ /** Smallest value wins. */
277
+ declare const lowest: (candidates: readonly string[]) => Comparator;
278
+ /** First candidate with an opinion (a cleared cell counts) wins. */
279
+ declare const priority: (candidates: readonly string[]) => Comparator;
280
+ /** First candidate with a non-null value wins. */
281
+ declare const nonNullFirst: (candidates: readonly string[]) => Comparator;
282
+ /**
283
+ * Author-defined SQL ORDER BY key over each candidate's `value`, `updated_at`,
284
+ * `source`, `actor` and `ord` (its position in `candidates`). The first row
285
+ * wins; `ord` is always appended as the final tie-break.
286
+ *
287
+ * ```ts
288
+ * custom(['edits', 'abrigo'], "CASE WHEN source = 'abrigo' AND value IS NOT NULL THEN 0 ELSE 1 END")
289
+ * ```
290
+ */
291
+ declare const custom: (candidates: readonly string[], orderBy: string) => Comparator;
292
+ interface ResolvedTablePoliciesInput {
293
+ read: PolicyInput;
294
+ write?: PolicyInput;
295
+ insert?: PolicyInput;
296
+ update?: PolicyInput;
297
+ }
298
+ interface ResolvedTableConfig {
299
+ description?: string;
300
+ sources: Record<string, ResolvedSourceInput>;
301
+ /** Per-column comparator; unlisted columns use `default`. */
302
+ resolve?: Record<string, Comparator>;
303
+ /** Fallback comparator; each column keeps only the candidates that supply it. Defaults to priority(manual, prefetch…, etl…). */
304
+ default?: Comparator;
305
+ rows?: {
306
+ deletedBy?: readonly string[];
307
+ };
308
+ links?: Record<string, SeqLink>;
309
+ /** Reads use `read`; writes are evaluated against the resolved row. */
310
+ policies: ResolvedTablePoliciesInput;
311
+ /**
312
+ * Store the resolved rows in an indexed internal table instead of
313
+ * computing them on every read, so filters and sorts on resolved values can
314
+ * use `indexes`. The data plane refreshes a row in the same transaction as
315
+ * any ORM write to its sources (edits, prefetch, writeback); ETL changes are
316
+ * picked up by a periodic reconcile. Every ETL source must have read policy
317
+ * `'true'` — the copy is shared by all callers.
318
+ */
319
+ materialize?: true | {
320
+ indexes?: readonly IndexInput[];
321
+ };
322
+ }
323
+ /** Authoring-side resolved metadata carried on the generated view (serialized by namespace.ts). */
324
+ interface ResolvedViewConfig {
325
+ readonly internalTables: readonly SeqTable[];
326
+ /** A materialized table's compute view. */
327
+ readonly internalViews?: readonly SeqView[];
328
+ readonly materialized?: {
329
+ readonly table: string;
330
+ readonly compute: string;
331
+ };
332
+ readonly sources: ReadonlyArray<ResolvedSourcePlan>;
333
+ readonly rules: ReadonlyArray<{
334
+ column: string;
335
+ comparator: ComparatorKind;
336
+ candidates: string[];
337
+ orderBy?: string;
338
+ }>;
339
+ readonly deletedBy: readonly string[];
340
+ readonly policies: Omit<ResolvedTablePoliciesInput, 'read'>;
341
+ }
342
+ interface ResolvedSourcePlan {
343
+ readonly name: string;
344
+ readonly kind: ResolvedSourceInput['kind'];
345
+ readonly table: SeqTable;
346
+ readonly columns: readonly string[];
347
+ readonly updatedAt?: string;
348
+ readonly key?: string;
349
+ readonly fetch?: CanonicalFetchShape;
350
+ readonly function?: FunctionRef;
351
+ readonly prefetchDefaults?: {
352
+ maxStalenessMs: number;
353
+ timeoutMs: number;
354
+ onError: 'fallback' | 'fail';
355
+ };
356
+ readonly writeback?: {
357
+ columns: string[];
358
+ defaultMode: 'sync' | 'async';
359
+ timeoutMs: number;
360
+ clearManualOnConfirm: boolean;
361
+ };
362
+ }
363
+ declare const SYSTEM_VIEW_COLUMNS: {
364
+ readonly writebackStatus: () => ColumnBuilder;
365
+ readonly writebackError: () => ColumnBuilder;
366
+ };
367
+ type SystemViewColumns = {
368
+ [K in keyof typeof SYSTEM_VIEW_COLUMNS]: ColumnBuilder;
369
+ };
370
+ declare function defineResolvedTable<const TColumns extends Record<string, ColumnBuilder>>(name: string, columns: TColumns, config: ResolvedTableConfig): SeqView<TColumns & SystemViewColumns>;
371
+
193
372
  /**
194
373
  * Managed regular-view authoring. A view keeps its GraphQL contract in the
195
374
  * Sequence column/policy DSL, while its SELECT is authored with Drizzle and
@@ -212,6 +391,10 @@ interface ViewConfig {
212
391
  links?: Record<string, SeqLink>;
213
392
  policies: ViewPoliciesInput;
214
393
  query: (context: ViewQueryContext) => ViewSelectQuery;
394
+ /** Set by defineResolvedTable — multi-source metadata serialized onto the canonical view. */
395
+ resolved?: ResolvedViewConfig;
396
+ /** System-owned view (a materialized resolved table's compute view): no GraphQL surface. Set by defineResolvedTable. */
397
+ internal?: boolean;
215
398
  }
216
399
  interface SeqView<TColumns extends Record<string, ColumnBuilder> = Record<string, ColumnBuilder>> {
217
400
  readonly kind: typeof VIEW_BRAND;
@@ -258,6 +441,8 @@ interface OverlayWinsUnionInput<TOverlay extends SeqTable = SeqTable, TSynced ex
258
441
  * policies: { read: 'true' },
259
442
  * })
260
443
  * ```
444
+ *
445
+ * @deprecated Use `defineResolvedTable` — per-cell comparators over manual/etl/prefetch sources. Kept so existing definitions still compile.
261
446
  */
262
447
  declare function overlayWinsUnion<TOverlay extends SeqTable, TSynced extends SeqTable>(input: OverlayWinsUnionInput<TOverlay, TSynced>): ViewConfig['query'];
263
448
 
@@ -268,6 +453,7 @@ type RelationRef = SeqTable | SeqView | (() => SeqTable | SeqView);
268
453
  /** Backward-compatible alias retained for existing consumers. */
269
454
  type TableRef = RelationRef;
270
455
  type LinkKind = 'toOne' | 'toMany' | 'toPlatformUser';
456
+ type ReferentialAction = 'cascade' | 'restrict' | 'no action' | 'set null' | 'set default';
271
457
  interface SeqLink {
272
458
  readonly kind: typeof LINK_BRAND;
273
459
  readonly linkKind: LinkKind;
@@ -277,6 +463,8 @@ interface SeqLink {
277
463
  readonly on: string;
278
464
  readonly references: string | undefined;
279
465
  readonly enforce: boolean;
466
+ readonly onDelete: ReferentialAction | undefined;
467
+ readonly onUpdate: ReferentialAction | undefined;
280
468
  readonly description: string | undefined;
281
469
  }
282
470
  interface ToOneOptions {
@@ -284,6 +472,8 @@ interface ToOneOptions {
284
472
  references?: string;
285
473
  /** Emit a real FK constraint (same-namespace toOne only). */
286
474
  enforce?: boolean;
475
+ onDelete?: ReferentialAction;
476
+ onUpdate?: ReferentialAction;
287
477
  description?: string;
288
478
  }
289
479
  interface ToManyOptions {
@@ -330,6 +520,11 @@ interface IndexInput {
330
520
  unique?: boolean;
331
521
  name?: string;
332
522
  }
523
+ /** Named PostgreSQL CHECK using the supported, validated predicate subset. */
524
+ interface CheckInput {
525
+ name: string;
526
+ expression: string;
527
+ }
333
528
  /** Database integrity constraint; independent of GraphQL relationship traversal. */
334
529
  interface ForeignKeyInput {
335
530
  name: string;
@@ -338,6 +533,8 @@ interface ForeignKeyInput {
338
533
  references: readonly string[];
339
534
  /** Omit for immediate, non-deferrable PostgreSQL enforcement. */
340
535
  deferrable?: 'initially-immediate' | 'initially-deferred';
536
+ onDelete?: ReferentialAction;
537
+ onUpdate?: ReferentialAction;
341
538
  }
342
539
  interface TableConfig {
343
540
  description?: string;
@@ -346,6 +543,7 @@ interface TableConfig {
346
543
  /** Composite primary key: property names (or column names). Mutually exclusive with column-level `.primaryKey()`. */
347
544
  primaryKey?: readonly string[];
348
545
  indexes?: readonly IndexInput[];
546
+ checks?: readonly CheckInput[];
349
547
  foreignKeys?: readonly ForeignKeyInput[];
350
548
  links?: Record<string, SeqLink>;
351
549
  policies?: TablePoliciesInput;
@@ -356,6 +554,29 @@ interface TableConfig {
356
554
  * mutation roots whose SQL the engine generates.
357
555
  */
358
556
  cowMutations?: readonly CowMutationInput[];
557
+ /**
558
+ * Track per-cell edit metadata: every data column `c` gets typed system
559
+ * columns `c__at` (timestamptz) and `c__by` (text), stamped by each
560
+ * insert/update for exactly the columns it writes. Read through the typed
561
+ * GraphQL field `cells { c { at by } }`; never client-writable.
562
+ */
563
+ cellMetadata?: boolean;
564
+ /**
565
+ * Internal (resolved-table manual source): also keep a `c__rev` uuid per
566
+ * cell identifying each edit. Set by defineResolvedTable.
567
+ */
568
+ cellRevisions?: boolean;
569
+ /**
570
+ * Internal: this table is the materialized copy of resolved table
571
+ * `materializes` (its columns mirror the view, cell parts included). Set by
572
+ * defineResolvedTable.
573
+ */
574
+ materializes?: string;
575
+ /**
576
+ * System-owned backing table of a resolved table (set by
577
+ * defineResolvedTable). Gets DDL but no GraphQL surface.
578
+ */
579
+ internal?: boolean;
359
580
  }
360
581
  interface SeqTable<TColumns extends Record<string, ColumnBuilder> = Record<string, ColumnBuilder>> {
361
582
  readonly kind: typeof TABLE_BRAND;
@@ -553,4 +774,4 @@ declare function beginAuthoringContext(): AuthoringContext;
553
774
  declare function endAuthoringContext(context: AuthoringContext): void;
554
775
  declare function activeAuthoringContext(): AuthoringContext | undefined;
555
776
 
556
- export { type AuthoringContext, type AuthoringRegistration, COLUMN_BRAND, COLUMN_DEFAULT_FNS, ColumnBuilder, type ColumnDefaultFn, type ColumnDefaultLiteral, type ColumnTypeSpec, type CowMutationInput, type DrizzleRelation, ENUM_BRAND, type EnumRef, type ForeignKeyInput, type IndexInput, LINK_BRAND, type LinkKind, type LoadNamespaceOptions, type LoadedNamespaceV2, NAMESPACE_CONFIG_BRAND, NAMESPACE_CONFIG_FILE, type NamespaceCapabilitiesInput, type NamespaceConfigInput, ORM_TO_PIPELINE_TYPE, type OrmContractColumnMap, type OrmContractPipelineColumn, type OrmSyncedTableContract, type OrmToPipelineType, type OverlayTableRef, type OverlayWinsUnionInput, type OverrideRowMutationInput, type PolicyInput, type PolicyVariantInput, type PolicyWithVariantsInput, type RelationRef, type RevertOverlayMutationInput, SCHEMA_DIR, SYNCED_CONTRACT_HASH_DOMAIN, type SeqLink, type SeqNamespaceConfig, type SeqTable, type SeqView, type SerializeNamespaceInput, type SyncedTableConfig, type SyncedTablePoliciesInput, TABLE_BRAND, type TableConfig, type TablePoliciesInput, type TableRef, type ToManyOptions, type ToOneOptions, type ToPlatformUserOptions, type TypedSyncedTableConfig, VIEW_BRAND, type ViewConfig, type ViewPoliciesInput, type ViewQueryContext, type ViewSelectQuery, activeAuthoringContext, beginAuthoringContext, computeCanonicalHash, computeSyncedContractHash, defineEnum, defineNamespace, defineSyncedTable, defineTable, defineView, deriveColumnName, endAuthoringContext, extractOrmSyncedTableContract, isColumnBuilder, isEnumRef, isSeqLink, isSeqNamespaceConfig, isSeqTable, isSeqView, loadNamespaceFromDir, overlayWinsUnion, overrideRowMutation, revertOverlayMutation, serializeNamespace, syncedContractSurface, t, toMany, toOne, toPlatformUser };
777
+ export { type AuthoringContext, type AuthoringRegistration, COLUMN_BRAND, COLUMN_DEFAULT_FNS, type CheckInput, ColumnBuilder, type ColumnDefaultFn, type ColumnDefaultLiteral, type ColumnTypeSpec, type Comparator, type CowMutationInput, type DrizzleRelation, type DurationInput, ENUM_BRAND, type EnumRef, type EtlSourceInput, type ForeignKeyInput, type FunctionRef, type IndexInput, LINK_BRAND, type LinkKind, type LoadNamespaceOptions, type LoadedNamespaceV2, type ManualSourceInput, NAMESPACE_CONFIG_BRAND, NAMESPACE_CONFIG_FILE, type NamespaceCapabilitiesInput, type NamespaceConfigInput, ORM_TO_PIPELINE_TYPE, type OrmContractColumnMap, type OrmContractPipelineColumn, type OrmSyncedTableContract, type OrmToPipelineType, type OverlayTableRef, type OverlayWinsUnionInput, type OverrideRowMutationInput, type PolicyInput, type PolicyVariantInput, type PolicyWithVariantsInput, type PrefetchSourceInput, type ReferentialAction, type RelationRef, type ResolvedSourceInput, type ResolvedTableConfig, type ResolvedTablePoliciesInput, type RevertOverlayMutationInput, SCHEMA_DIR, SYNCED_CONTRACT_HASH_DOMAIN, type SeqLink, type SeqNamespaceConfig, type SeqTable, type SeqView, type SerializeNamespaceInput, type SyncedTableConfig, type SyncedTablePoliciesInput, TABLE_BRAND, type TableConfig, type TablePoliciesInput, type TableRef, type ToManyOptions, type ToOneOptions, type ToPlatformUserOptions, type TypedSyncedTableConfig, VIEW_BRAND, type ViewConfig, type ViewPoliciesInput, type ViewQueryContext, type ViewSelectQuery, activeAuthoringContext, beginAuthoringContext, computeCanonicalHash, computeSyncedContractHash, custom, defineEnum, defineNamespace, defineResolvedTable, defineSyncedTable, defineTable, defineView, deriveColumnName, endAuthoringContext, etlSource, extractOrmSyncedTableContract, highest, isColumnBuilder, isEnumRef, isSeqLink, isSeqNamespaceConfig, isSeqTable, isSeqView, latest, loadNamespaceFromDir, lowest, managedFn, manualSource, nonNullFirst, overlayWinsUnion, overrideRowMutation, prefetchSource, priority, revertOverlayMutation, serializeNamespace, syncedContractSurface, t, toMany, toOne, toPlatformUser };
@@ -1,8 +1,10 @@
1
- import { ColumnType, CanonicalColumn, CanonicalDefinition, CanonicalTable } from './canonical.js';
1
+ import { ColumnType, CanonicalColumn, ComparatorKind, CanonicalFunctionRef, CanonicalFetchShape, CanonicalDefinition, CanonicalTable } from './canonical.js';
2
+ import { b as CellPart } from './constants-YoFGjIBM.js';
2
3
  import { ColumnsSelection } from 'drizzle-orm';
3
4
  import { TypedQueryBuilder } from 'drizzle-orm/query-builders/query-builder';
4
5
  import { QueryBuilder, PgView, PgTable, AnyPgColumn } from 'drizzle-orm/pg-core';
5
6
  import 'zod';
7
+ import './policy-1iZbGnZZ.js';
6
8
 
7
9
  /**
8
10
  * Fluent column builders for the v2 authoring surface — `t.uuid()`,
@@ -61,6 +63,8 @@ declare class ColumnBuilder {
61
63
  cast(usingSql: string): this;
62
64
  /** Emits `@deprecated` in the generated SDL; the column stays queryable. */
63
65
  deprecated(): this;
66
+ /** @internal System cell-metadata part of logical column `of` (generated resolved views only). */
67
+ cellPart(of: string, part: CellPart): this;
64
68
  /** Whether `.primaryKey()` was called — collected into the table-level PK. */
65
69
  get isPrimaryKey(): boolean;
66
70
  /** The enum name when this is an enum column, else undefined. */
@@ -179,6 +183,8 @@ interface RevertOverlayMutationInput<TOverlay extends OverlayTableRef = OverlayT
179
183
  * ],
180
184
  * })
181
185
  * ```
186
+ *
187
+ * @deprecated Resolved tables write sparse cells directly (`defineResolvedTable`). Kept so existing definitions still compile.
182
188
  */
183
189
  declare function overrideRowMutation<TOverlay extends OverlayTableRef, TSynced extends SeqTable>(input: OverrideRowMutationInput<TOverlay, TSynced>): CowMutationInput;
184
190
  /**
@@ -187,9 +193,182 @@ declare function overrideRowMutation<TOverlay extends OverlayTableRef, TSynced e
187
193
  *
188
194
  * The DELETE is gated on a matching synced row — overlay-only creates (or rows
189
195
  * whose synced source disappeared) are not deleted by revert.
196
+ *
197
+ * @deprecated Resolved tables write sparse cells directly (`defineResolvedTable`). Kept so existing definitions still compile.
190
198
  */
191
199
  declare function revertOverlayMutation<TOverlay extends OverlayTableRef, TSynced extends SeqTable>(input: RevertOverlayMutationInput<TOverlay, TSynced>): CowMutationInput;
192
200
 
201
+ /**
202
+ * `defineResolvedTable` — one logical table backed by manual, etl and
203
+ * prefetch sources, with a SQL comparator per column.
204
+ *
205
+ * It expands into existing primitives so DDL, apply, SDL and policy
206
+ * compilation stay shared:
207
+ *
208
+ * - one internal table per manual/prefetch source (`<name>__<source>`,
209
+ * `cellMetadata: true`, sparse nullable columns),
210
+ * - the author's own synced tables for etl sources,
211
+ * - a managed view named `<name>` whose generated query unions every
212
+ * source's primary keys (so `pk = $1` pushes down into each source),
213
+ * left-joins each source back, and picks every column's winner with a
214
+ * CASE over typed per-cell timestamps (built-in comparators) or a small
215
+ * ORDER BY … LIMIT 1 lateral (custom).
216
+ *
217
+ * All comparators run in SQL — never app code — so reads stay one statement
218
+ * and filters/policies see resolved values.
219
+ */
220
+
221
+ /** A duration: milliseconds, or a string like `'1500ms'`, `'30s'`, `'5m'`. */
222
+ type DurationInput = number | `${number}ms` | `${number}s` | `${number}m`;
223
+ type FunctionRef = CanonicalFunctionRef;
224
+ /** A Managed Function (id or slug) that implements a prefetch source's fetch and writeback. */
225
+ declare function managedFn(id: string): FunctionRef;
226
+ interface ManualSourceInput {
227
+ readonly kind: 'manual';
228
+ }
229
+ interface EtlSourceInput {
230
+ readonly kind: 'etl';
231
+ /** A `defineSyncedTable` relation fed by a pipeline. */
232
+ readonly table: RelationRef;
233
+ /** Property on the synced table whose value timestamps every cell (e.g. an ingested_at column). */
234
+ readonly updatedAt?: string;
235
+ /** Resolved columns this source supplies; defaults to every column the synced table shares by name. */
236
+ readonly columns?: readonly string[];
237
+ }
238
+ interface PrefetchSourceInput {
239
+ readonly kind: 'prefetch';
240
+ readonly function: FunctionRef;
241
+ /** Resolved column holding the source system's key (may be the primary key). */
242
+ readonly key: string;
243
+ /** Resolved columns the function returns. */
244
+ readonly columns: readonly string[];
245
+ /** What the source API supports: one key per call, or batches. */
246
+ readonly fetch: 'single' | {
247
+ readonly batch: {
248
+ readonly maxKeys: number;
249
+ };
250
+ };
251
+ readonly prefetchDefaults?: {
252
+ readonly maxStaleness?: DurationInput;
253
+ readonly timeout?: DurationInput;
254
+ readonly onError?: 'fallback' | 'fail';
255
+ };
256
+ readonly writeback?: {
257
+ readonly columns: readonly string[];
258
+ readonly defaultMode?: 'sync' | 'async';
259
+ readonly timeout?: DurationInput;
260
+ readonly clearManualOnConfirm?: boolean;
261
+ };
262
+ }
263
+ type ResolvedSourceInput = ManualSourceInput | EtlSourceInput | PrefetchSourceInput;
264
+ declare function manualSource(): ManualSourceInput;
265
+ declare function etlSource(input: Omit<EtlSourceInput, 'kind'>): EtlSourceInput;
266
+ declare function prefetchSource(input: Omit<PrefetchSourceInput, 'kind'>): PrefetchSourceInput;
267
+ interface Comparator {
268
+ readonly kind: ComparatorKind;
269
+ readonly candidates: readonly string[];
270
+ readonly orderBy?: string;
271
+ }
272
+ /** Newest `updated_at` wins; source order breaks ties. */
273
+ declare const latest: (candidates: readonly string[]) => Comparator;
274
+ /** Largest value wins. */
275
+ declare const highest: (candidates: readonly string[]) => Comparator;
276
+ /** Smallest value wins. */
277
+ declare const lowest: (candidates: readonly string[]) => Comparator;
278
+ /** First candidate with an opinion (a cleared cell counts) wins. */
279
+ declare const priority: (candidates: readonly string[]) => Comparator;
280
+ /** First candidate with a non-null value wins. */
281
+ declare const nonNullFirst: (candidates: readonly string[]) => Comparator;
282
+ /**
283
+ * Author-defined SQL ORDER BY key over each candidate's `value`, `updated_at`,
284
+ * `source`, `actor` and `ord` (its position in `candidates`). The first row
285
+ * wins; `ord` is always appended as the final tie-break.
286
+ *
287
+ * ```ts
288
+ * custom(['edits', 'abrigo'], "CASE WHEN source = 'abrigo' AND value IS NOT NULL THEN 0 ELSE 1 END")
289
+ * ```
290
+ */
291
+ declare const custom: (candidates: readonly string[], orderBy: string) => Comparator;
292
+ interface ResolvedTablePoliciesInput {
293
+ read: PolicyInput;
294
+ write?: PolicyInput;
295
+ insert?: PolicyInput;
296
+ update?: PolicyInput;
297
+ }
298
+ interface ResolvedTableConfig {
299
+ description?: string;
300
+ sources: Record<string, ResolvedSourceInput>;
301
+ /** Per-column comparator; unlisted columns use `default`. */
302
+ resolve?: Record<string, Comparator>;
303
+ /** Fallback comparator; each column keeps only the candidates that supply it. Defaults to priority(manual, prefetch…, etl…). */
304
+ default?: Comparator;
305
+ rows?: {
306
+ deletedBy?: readonly string[];
307
+ };
308
+ links?: Record<string, SeqLink>;
309
+ /** Reads use `read`; writes are evaluated against the resolved row. */
310
+ policies: ResolvedTablePoliciesInput;
311
+ /**
312
+ * Store the resolved rows in an indexed internal table instead of
313
+ * computing them on every read, so filters and sorts on resolved values can
314
+ * use `indexes`. The data plane refreshes a row in the same transaction as
315
+ * any ORM write to its sources (edits, prefetch, writeback); ETL changes are
316
+ * picked up by a periodic reconcile. Every ETL source must have read policy
317
+ * `'true'` — the copy is shared by all callers.
318
+ */
319
+ materialize?: true | {
320
+ indexes?: readonly IndexInput[];
321
+ };
322
+ }
323
+ /** Authoring-side resolved metadata carried on the generated view (serialized by namespace.ts). */
324
+ interface ResolvedViewConfig {
325
+ readonly internalTables: readonly SeqTable[];
326
+ /** A materialized table's compute view. */
327
+ readonly internalViews?: readonly SeqView[];
328
+ readonly materialized?: {
329
+ readonly table: string;
330
+ readonly compute: string;
331
+ };
332
+ readonly sources: ReadonlyArray<ResolvedSourcePlan>;
333
+ readonly rules: ReadonlyArray<{
334
+ column: string;
335
+ comparator: ComparatorKind;
336
+ candidates: string[];
337
+ orderBy?: string;
338
+ }>;
339
+ readonly deletedBy: readonly string[];
340
+ readonly policies: Omit<ResolvedTablePoliciesInput, 'read'>;
341
+ }
342
+ interface ResolvedSourcePlan {
343
+ readonly name: string;
344
+ readonly kind: ResolvedSourceInput['kind'];
345
+ readonly table: SeqTable;
346
+ readonly columns: readonly string[];
347
+ readonly updatedAt?: string;
348
+ readonly key?: string;
349
+ readonly fetch?: CanonicalFetchShape;
350
+ readonly function?: FunctionRef;
351
+ readonly prefetchDefaults?: {
352
+ maxStalenessMs: number;
353
+ timeoutMs: number;
354
+ onError: 'fallback' | 'fail';
355
+ };
356
+ readonly writeback?: {
357
+ columns: string[];
358
+ defaultMode: 'sync' | 'async';
359
+ timeoutMs: number;
360
+ clearManualOnConfirm: boolean;
361
+ };
362
+ }
363
+ declare const SYSTEM_VIEW_COLUMNS: {
364
+ readonly writebackStatus: () => ColumnBuilder;
365
+ readonly writebackError: () => ColumnBuilder;
366
+ };
367
+ type SystemViewColumns = {
368
+ [K in keyof typeof SYSTEM_VIEW_COLUMNS]: ColumnBuilder;
369
+ };
370
+ declare function defineResolvedTable<const TColumns extends Record<string, ColumnBuilder>>(name: string, columns: TColumns, config: ResolvedTableConfig): SeqView<TColumns & SystemViewColumns>;
371
+
193
372
  /**
194
373
  * Managed regular-view authoring. A view keeps its GraphQL contract in the
195
374
  * Sequence column/policy DSL, while its SELECT is authored with Drizzle and
@@ -212,6 +391,10 @@ interface ViewConfig {
212
391
  links?: Record<string, SeqLink>;
213
392
  policies: ViewPoliciesInput;
214
393
  query: (context: ViewQueryContext) => ViewSelectQuery;
394
+ /** Set by defineResolvedTable — multi-source metadata serialized onto the canonical view. */
395
+ resolved?: ResolvedViewConfig;
396
+ /** System-owned view (a materialized resolved table's compute view): no GraphQL surface. Set by defineResolvedTable. */
397
+ internal?: boolean;
215
398
  }
216
399
  interface SeqView<TColumns extends Record<string, ColumnBuilder> = Record<string, ColumnBuilder>> {
217
400
  readonly kind: typeof VIEW_BRAND;
@@ -258,6 +441,8 @@ interface OverlayWinsUnionInput<TOverlay extends SeqTable = SeqTable, TSynced ex
258
441
  * policies: { read: 'true' },
259
442
  * })
260
443
  * ```
444
+ *
445
+ * @deprecated Use `defineResolvedTable` — per-cell comparators over manual/etl/prefetch sources. Kept so existing definitions still compile.
261
446
  */
262
447
  declare function overlayWinsUnion<TOverlay extends SeqTable, TSynced extends SeqTable>(input: OverlayWinsUnionInput<TOverlay, TSynced>): ViewConfig['query'];
263
448
 
@@ -268,6 +453,7 @@ type RelationRef = SeqTable | SeqView | (() => SeqTable | SeqView);
268
453
  /** Backward-compatible alias retained for existing consumers. */
269
454
  type TableRef = RelationRef;
270
455
  type LinkKind = 'toOne' | 'toMany' | 'toPlatformUser';
456
+ type ReferentialAction = 'cascade' | 'restrict' | 'no action' | 'set null' | 'set default';
271
457
  interface SeqLink {
272
458
  readonly kind: typeof LINK_BRAND;
273
459
  readonly linkKind: LinkKind;
@@ -277,6 +463,8 @@ interface SeqLink {
277
463
  readonly on: string;
278
464
  readonly references: string | undefined;
279
465
  readonly enforce: boolean;
466
+ readonly onDelete: ReferentialAction | undefined;
467
+ readonly onUpdate: ReferentialAction | undefined;
280
468
  readonly description: string | undefined;
281
469
  }
282
470
  interface ToOneOptions {
@@ -284,6 +472,8 @@ interface ToOneOptions {
284
472
  references?: string;
285
473
  /** Emit a real FK constraint (same-namespace toOne only). */
286
474
  enforce?: boolean;
475
+ onDelete?: ReferentialAction;
476
+ onUpdate?: ReferentialAction;
287
477
  description?: string;
288
478
  }
289
479
  interface ToManyOptions {
@@ -330,6 +520,11 @@ interface IndexInput {
330
520
  unique?: boolean;
331
521
  name?: string;
332
522
  }
523
+ /** Named PostgreSQL CHECK using the supported, validated predicate subset. */
524
+ interface CheckInput {
525
+ name: string;
526
+ expression: string;
527
+ }
333
528
  /** Database integrity constraint; independent of GraphQL relationship traversal. */
334
529
  interface ForeignKeyInput {
335
530
  name: string;
@@ -338,6 +533,8 @@ interface ForeignKeyInput {
338
533
  references: readonly string[];
339
534
  /** Omit for immediate, non-deferrable PostgreSQL enforcement. */
340
535
  deferrable?: 'initially-immediate' | 'initially-deferred';
536
+ onDelete?: ReferentialAction;
537
+ onUpdate?: ReferentialAction;
341
538
  }
342
539
  interface TableConfig {
343
540
  description?: string;
@@ -346,6 +543,7 @@ interface TableConfig {
346
543
  /** Composite primary key: property names (or column names). Mutually exclusive with column-level `.primaryKey()`. */
347
544
  primaryKey?: readonly string[];
348
545
  indexes?: readonly IndexInput[];
546
+ checks?: readonly CheckInput[];
349
547
  foreignKeys?: readonly ForeignKeyInput[];
350
548
  links?: Record<string, SeqLink>;
351
549
  policies?: TablePoliciesInput;
@@ -356,6 +554,29 @@ interface TableConfig {
356
554
  * mutation roots whose SQL the engine generates.
357
555
  */
358
556
  cowMutations?: readonly CowMutationInput[];
557
+ /**
558
+ * Track per-cell edit metadata: every data column `c` gets typed system
559
+ * columns `c__at` (timestamptz) and `c__by` (text), stamped by each
560
+ * insert/update for exactly the columns it writes. Read through the typed
561
+ * GraphQL field `cells { c { at by } }`; never client-writable.
562
+ */
563
+ cellMetadata?: boolean;
564
+ /**
565
+ * Internal (resolved-table manual source): also keep a `c__rev` uuid per
566
+ * cell identifying each edit. Set by defineResolvedTable.
567
+ */
568
+ cellRevisions?: boolean;
569
+ /**
570
+ * Internal: this table is the materialized copy of resolved table
571
+ * `materializes` (its columns mirror the view, cell parts included). Set by
572
+ * defineResolvedTable.
573
+ */
574
+ materializes?: string;
575
+ /**
576
+ * System-owned backing table of a resolved table (set by
577
+ * defineResolvedTable). Gets DDL but no GraphQL surface.
578
+ */
579
+ internal?: boolean;
359
580
  }
360
581
  interface SeqTable<TColumns extends Record<string, ColumnBuilder> = Record<string, ColumnBuilder>> {
361
582
  readonly kind: typeof TABLE_BRAND;
@@ -553,4 +774,4 @@ declare function beginAuthoringContext(): AuthoringContext;
553
774
  declare function endAuthoringContext(context: AuthoringContext): void;
554
775
  declare function activeAuthoringContext(): AuthoringContext | undefined;
555
776
 
556
- export { type AuthoringContext, type AuthoringRegistration, COLUMN_BRAND, COLUMN_DEFAULT_FNS, ColumnBuilder, type ColumnDefaultFn, type ColumnDefaultLiteral, type ColumnTypeSpec, type CowMutationInput, type DrizzleRelation, ENUM_BRAND, type EnumRef, type ForeignKeyInput, type IndexInput, LINK_BRAND, type LinkKind, type LoadNamespaceOptions, type LoadedNamespaceV2, NAMESPACE_CONFIG_BRAND, NAMESPACE_CONFIG_FILE, type NamespaceCapabilitiesInput, type NamespaceConfigInput, ORM_TO_PIPELINE_TYPE, type OrmContractColumnMap, type OrmContractPipelineColumn, type OrmSyncedTableContract, type OrmToPipelineType, type OverlayTableRef, type OverlayWinsUnionInput, type OverrideRowMutationInput, type PolicyInput, type PolicyVariantInput, type PolicyWithVariantsInput, type RelationRef, type RevertOverlayMutationInput, SCHEMA_DIR, SYNCED_CONTRACT_HASH_DOMAIN, type SeqLink, type SeqNamespaceConfig, type SeqTable, type SeqView, type SerializeNamespaceInput, type SyncedTableConfig, type SyncedTablePoliciesInput, TABLE_BRAND, type TableConfig, type TablePoliciesInput, type TableRef, type ToManyOptions, type ToOneOptions, type ToPlatformUserOptions, type TypedSyncedTableConfig, VIEW_BRAND, type ViewConfig, type ViewPoliciesInput, type ViewQueryContext, type ViewSelectQuery, activeAuthoringContext, beginAuthoringContext, computeCanonicalHash, computeSyncedContractHash, defineEnum, defineNamespace, defineSyncedTable, defineTable, defineView, deriveColumnName, endAuthoringContext, extractOrmSyncedTableContract, isColumnBuilder, isEnumRef, isSeqLink, isSeqNamespaceConfig, isSeqTable, isSeqView, loadNamespaceFromDir, overlayWinsUnion, overrideRowMutation, revertOverlayMutation, serializeNamespace, syncedContractSurface, t, toMany, toOne, toPlatformUser };
777
+ export { type AuthoringContext, type AuthoringRegistration, COLUMN_BRAND, COLUMN_DEFAULT_FNS, type CheckInput, ColumnBuilder, type ColumnDefaultFn, type ColumnDefaultLiteral, type ColumnTypeSpec, type Comparator, type CowMutationInput, type DrizzleRelation, type DurationInput, ENUM_BRAND, type EnumRef, type EtlSourceInput, type ForeignKeyInput, type FunctionRef, type IndexInput, LINK_BRAND, type LinkKind, type LoadNamespaceOptions, type LoadedNamespaceV2, type ManualSourceInput, NAMESPACE_CONFIG_BRAND, NAMESPACE_CONFIG_FILE, type NamespaceCapabilitiesInput, type NamespaceConfigInput, ORM_TO_PIPELINE_TYPE, type OrmContractColumnMap, type OrmContractPipelineColumn, type OrmSyncedTableContract, type OrmToPipelineType, type OverlayTableRef, type OverlayWinsUnionInput, type OverrideRowMutationInput, type PolicyInput, type PolicyVariantInput, type PolicyWithVariantsInput, type PrefetchSourceInput, type ReferentialAction, type RelationRef, type ResolvedSourceInput, type ResolvedTableConfig, type ResolvedTablePoliciesInput, type RevertOverlayMutationInput, SCHEMA_DIR, SYNCED_CONTRACT_HASH_DOMAIN, type SeqLink, type SeqNamespaceConfig, type SeqTable, type SeqView, type SerializeNamespaceInput, type SyncedTableConfig, type SyncedTablePoliciesInput, TABLE_BRAND, type TableConfig, type TablePoliciesInput, type TableRef, type ToManyOptions, type ToOneOptions, type ToPlatformUserOptions, type TypedSyncedTableConfig, VIEW_BRAND, type ViewConfig, type ViewPoliciesInput, type ViewQueryContext, type ViewSelectQuery, activeAuthoringContext, beginAuthoringContext, computeCanonicalHash, computeSyncedContractHash, custom, defineEnum, defineNamespace, defineResolvedTable, defineSyncedTable, defineTable, defineView, deriveColumnName, endAuthoringContext, etlSource, extractOrmSyncedTableContract, highest, isColumnBuilder, isEnumRef, isSeqLink, isSeqNamespaceConfig, isSeqTable, isSeqView, latest, loadNamespaceFromDir, lowest, managedFn, manualSource, nonNullFirst, overlayWinsUnion, overrideRowMutation, prefetchSource, priority, revertOverlayMutation, serializeNamespace, syncedContractSurface, t, toMany, toOne, toPlatformUser };