@pgxsinkit/contracts 0.1.53 → 0.2.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.
- package/dist/config.d.ts +79 -11
- package/dist/config.d.ts.map +1 -1
- package/dist/index.js +92 -14
- package/dist/index.js.map +5 -5
- package/dist/mutation.d.ts.map +1 -1
- package/dist/registry.d.ts +35 -11
- package/dist/registry.d.ts.map +1 -1
- package/dist/runtime.d.ts +1 -1
- package/package.json +1 -1
- package/skills/registry-authoring/SKILL.md +33 -10
- package/src/config.ts +143 -19
- package/src/mutation.ts +1 -2
- package/src/registry.ts +129 -34
- package/src/runtime.ts +1 -1
package/src/registry.ts
CHANGED
|
@@ -29,9 +29,13 @@ import {
|
|
|
29
29
|
CONFLICT_POLICIES,
|
|
30
30
|
isConflictPolicy,
|
|
31
31
|
isRetention,
|
|
32
|
+
isStorageBackend,
|
|
33
|
+
isStorageDurability,
|
|
32
34
|
isSubscriptionTiming,
|
|
33
35
|
isWriteMode,
|
|
34
36
|
RETENTIONS,
|
|
37
|
+
STORAGE_BACKENDS,
|
|
38
|
+
STORAGE_DURABILITIES,
|
|
35
39
|
SUBSCRIPTION_TIMINGS,
|
|
36
40
|
WRITE_MODES,
|
|
37
41
|
type ClientProjectionSpec,
|
|
@@ -47,6 +51,7 @@ import {
|
|
|
47
51
|
type ShapeSpec,
|
|
48
52
|
type ShapeSpecInput,
|
|
49
53
|
type SubscriptionTiming,
|
|
54
|
+
type SyncStorageDeclaration,
|
|
50
55
|
type TableGovernanceSpec as TableGovernanceSpecBase,
|
|
51
56
|
type TableMode,
|
|
52
57
|
type WriteMode,
|
|
@@ -142,8 +147,9 @@ export interface SyncTableEntry<TTable extends AnyPgTable = AnyPgTable, TLocalTa
|
|
|
142
147
|
* Consistency group (ADR-0009 decision 2). Tables sharing a `consistencyGroup` are synced on one
|
|
143
148
|
* `MultiShapeStream` and committed atomically at a shared LSN frontier, so a local reader never
|
|
144
149
|
* sees one grouped table advanced past another for the same server transaction. Omitted → the
|
|
145
|
-
* table is its own singleton group (independent frontier,
|
|
146
|
-
*
|
|
150
|
+
* table is its own singleton group (independent frontier, no cross-table atomicity — the resolution
|
|
151
|
+
* for a table that declares no group). The latency cost (a group advances only as fast as its
|
|
152
|
+
* slowest shape) is contained to the tables that opt in.
|
|
147
153
|
*/
|
|
148
154
|
consistencyGroup?: string;
|
|
149
155
|
/**
|
|
@@ -273,17 +279,16 @@ export type SyncTableInputProjection<
|
|
|
273
279
|
};
|
|
274
280
|
|
|
275
281
|
/**
|
|
276
|
-
* A row filter for {@link defineSyncTable}'s `shape
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* rename-safe column objects instead of hand-written column-name strings.
|
|
282
|
+
* A row filter for {@link defineSyncTable}'s `shape`, authored from the table's built, typed columns.
|
|
283
|
+
* Reference columns through `c(columns.x)` exactly as `extras` does with its `self` argument, so
|
|
284
|
+
* `customWhere` builds parameterized Electric `where`s from real, rename-safe column objects instead
|
|
285
|
+
* of hand-written column-name strings.
|
|
281
286
|
*/
|
|
282
|
-
export type RowFilterInput<TColumns extends Record<string, ColumnBuilderBase>> =
|
|
283
|
-
|
|
284
|
-
|
|
287
|
+
export type RowFilterInput<TColumns extends Record<string, ColumnBuilderBase>> = (
|
|
288
|
+
columns: PgBuildExtraConfigColumns<TColumns>,
|
|
289
|
+
) => RowFilterSpec;
|
|
285
290
|
|
|
286
|
-
/** {@link ShapeSpecInput} whose `rowFilter`
|
|
291
|
+
/** {@link ShapeSpecInput} whose `rowFilter` is authored from the built columns (typed by `TColumns`). */
|
|
287
292
|
export type ShapeSpecInputFor<TColumns extends Record<string, ColumnBuilderBase>> = Omit<
|
|
288
293
|
ShapeSpecInput,
|
|
289
294
|
"rowFilter"
|
|
@@ -379,10 +384,20 @@ export type SyncTableRegistry = Record<string, SyncTableEntry>;
|
|
|
379
384
|
export interface SyncRegistryDefinition<TRegistry extends SyncTableRegistry> {
|
|
380
385
|
schema?: string;
|
|
381
386
|
tables: TRegistry;
|
|
387
|
+
/**
|
|
388
|
+
* The storage contract for every store this registry mints (ADR-0049 decision 1, ADR-0047). Part of the
|
|
389
|
+
* DATA contract, not a per-open knob: `durability` binds every toolkit-minted open (relaxed default),
|
|
390
|
+
* and `backend` scopes the BROWSER store only (`opfs` default; Node/`file` and `memory` clones
|
|
391
|
+
* unaffected). Validated at {@link defineSyncRegistry} (fail-closed at module-eval); carried through on
|
|
392
|
+
* the returned registry and read back with {@link getSyncRegistryStorage}. See {@link SyncStorageDeclaration}.
|
|
393
|
+
*/
|
|
394
|
+
storage?: SyncStorageDeclaration;
|
|
382
395
|
}
|
|
383
396
|
|
|
384
397
|
export const syncRegistrySchemaSymbol = Symbol.for("@pgxsinkit/contracts/syncRegistrySchema");
|
|
385
398
|
|
|
399
|
+
export const syncRegistryStorageSymbol = Symbol.for("@pgxsinkit/contracts/syncRegistryStorage");
|
|
400
|
+
|
|
386
401
|
export type RegistryTables<TRegistry extends SyncTableRegistry> = {
|
|
387
402
|
[TKey in keyof TRegistry]: TRegistry[TKey]["table"];
|
|
388
403
|
};
|
|
@@ -577,14 +592,11 @@ export function defineSyncTable<
|
|
|
577
592
|
: undefined;
|
|
578
593
|
const table = schema ? schema.table(tableName, makeColumns(), extrasFn) : pgTable(tableName, makeColumns(), extrasFn);
|
|
579
594
|
|
|
580
|
-
//
|
|
581
|
-
//
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
typeof shape?.rowFilter === "function"
|
|
586
|
-
? shape.rowFilter(getColumns(table) as unknown as PgBuildExtraConfigColumns<ReturnType<typeof makeColumns>>)
|
|
587
|
-
: (shape?.rowFilter ?? undefined);
|
|
595
|
+
// Resolve `shape.rowFilter` against the built typed columns so all authoring uses rename-safe
|
|
596
|
+
// Drizzle column objects rather than hand-written column-name strings.
|
|
597
|
+
const resolvedRowFilter = shape?.rowFilter?.(
|
|
598
|
+
getColumns(table) as unknown as PgBuildExtraConfigColumns<ReturnType<typeof makeColumns>>,
|
|
599
|
+
);
|
|
588
600
|
const resolvedShape: ShapeSpec | undefined =
|
|
589
601
|
resolvedMode !== "writeonly" || shape != null
|
|
590
602
|
? {
|
|
@@ -725,7 +737,7 @@ export function defineReadProjection<
|
|
|
725
737
|
/** Column keys (of the owner) to sync locally + fetch from Electric. The PK is always kept. Omit → all. */
|
|
726
738
|
columns?: TColumns;
|
|
727
739
|
/** Row filter for this shape; the callback form receives the owner's full (physical) columns. */
|
|
728
|
-
rowFilter?:
|
|
740
|
+
rowFilter?: (columns: TableColumnsShape<TOwnerTable>) => RowFilterSpec;
|
|
729
741
|
/**
|
|
730
742
|
* Server-side egress projection (ADR-0004) for THIS shape — typically a `rowTransform` that redacts a
|
|
731
743
|
* sub-document of a kept column conditionally on row data. A projection does NOT inherit its owner's
|
|
@@ -882,10 +894,7 @@ export function defineReadProjection<
|
|
|
882
894
|
|
|
883
895
|
// Resolve the row filter against the OWNER's full columns (the customWhere runs in Electric on the
|
|
884
896
|
// physical table, so it may reference a column the subset omits).
|
|
885
|
-
const resolvedRowFilter
|
|
886
|
-
typeof opts.rowFilter === "function"
|
|
887
|
-
? opts.rowFilter(getColumns(owner.table) as unknown as TableColumnsShape<TOwnerTable>)
|
|
888
|
-
: opts.rowFilter;
|
|
897
|
+
const resolvedRowFilter = opts.rowFilter?.(getColumns(owner.table) as unknown as TableColumnsShape<TOwnerTable>);
|
|
889
898
|
|
|
890
899
|
// When a subset is requested, set the Electric `columns` allow-list to the KEPT physical column names
|
|
891
900
|
// so an omitted (e.g. heavy jsonb) column is never fetched over the wire — not merely stripped after.
|
|
@@ -957,8 +966,14 @@ export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TReg
|
|
|
957
966
|
}
|
|
958
967
|
validateRegistryTableUniqueness(input.tables);
|
|
959
968
|
validateRegistryLifecycleGroups(input.tables);
|
|
960
|
-
|
|
961
|
-
|
|
969
|
+
validateStorageDeclaration(input.storage);
|
|
970
|
+
|
|
971
|
+
// Schema first (it may return the same tables object), then stamp the storage declaration on the
|
|
972
|
+
// returned registry as a non-enumerable symbol — the same carrier pattern as the schema symbol — so
|
|
973
|
+
// client code reads the data-contract storage back off the registry value (getSyncRegistrySchema's
|
|
974
|
+
// twin, getSyncRegistryStorage). The bare-registry-map overload carries no storage and reads back
|
|
975
|
+
// `undefined`, which resolves to the ADR-0047 defaults at the mint seam.
|
|
976
|
+
return attachSyncRegistryStorage(attachSyncRegistrySchema(input.tables, input.schema), input.storage);
|
|
962
977
|
}
|
|
963
978
|
|
|
964
979
|
for (const entry of getRegistryEntries(input)) {
|
|
@@ -1173,15 +1188,10 @@ export function resolveServerVersionColumnName<TTable extends AnyPgTable>(
|
|
|
1173
1188
|
return undefined;
|
|
1174
1189
|
}
|
|
1175
1190
|
|
|
1191
|
+
// `field.column` is the drizzle property key (registry validation rejects a column-name declaration —
|
|
1192
|
+
// ADR-0012); resolve it to the underlying SQL column name.
|
|
1176
1193
|
const columns = getColumns(entry.table);
|
|
1177
|
-
|
|
1178
|
-
const byProperty = (columns as Record<string, { name: string } | undefined>)[field.column as string];
|
|
1179
|
-
if (byProperty) {
|
|
1180
|
-
return byProperty.name;
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
// Defensive: a managed field declared by column name rather than property key.
|
|
1184
|
-
return Object.values(columns).find((column) => column.name === field.column)?.name;
|
|
1194
|
+
return (columns as Record<string, { name: string } | undefined>)[field.column as string]?.name;
|
|
1185
1195
|
}
|
|
1186
1196
|
|
|
1187
1197
|
export function attachSyncRegistrySchema<TRegistry extends SyncTableRegistry>(registry: TRegistry, schema?: string) {
|
|
@@ -1231,6 +1241,77 @@ export function getSyncRegistrySchema<TRegistry extends SyncTableRegistry>(regis
|
|
|
1231
1241
|
return typeof schema === "string" && schema.length > 0 ? schema : "public";
|
|
1232
1242
|
}
|
|
1233
1243
|
|
|
1244
|
+
/**
|
|
1245
|
+
* Stamp the registry's storage declaration (ADR-0049 decision 1, ADR-0047) onto the registry value as a
|
|
1246
|
+
* non-enumerable symbol — the storage twin of {@link attachSyncRegistrySchema}. Carries the data-contract
|
|
1247
|
+
* storage through {@link defineSyncRegistry} so client code reads it back via {@link getSyncRegistryStorage}.
|
|
1248
|
+
* A `null`/`undefined` declaration attaches nothing (the bare-registry-map overload), leaving the reader to
|
|
1249
|
+
* resolve the ADR-0047 defaults at the mint seam.
|
|
1250
|
+
*/
|
|
1251
|
+
export function attachSyncRegistryStorage<TRegistry extends SyncTableRegistry>(
|
|
1252
|
+
registry: TRegistry,
|
|
1253
|
+
storage: SyncStorageDeclaration | undefined,
|
|
1254
|
+
) {
|
|
1255
|
+
if (storage == null) {
|
|
1256
|
+
return registry;
|
|
1257
|
+
}
|
|
1258
|
+
// Idempotent for an EQUAL declaration (a registry value reused across constructions may be re-stamped with
|
|
1259
|
+
// the same contract) but fail-closed on a CONFLICT — two different storage contracts over one registry is a
|
|
1260
|
+
// definition error, never silently resolved. `backend`/`durability` are the only fields, so a shallow
|
|
1261
|
+
// compare is exhaustive.
|
|
1262
|
+
const existing = getSyncRegistryStorage(registry);
|
|
1263
|
+
if (existing != null) {
|
|
1264
|
+
if (existing.backend !== storage.backend || existing.durability !== storage.durability) {
|
|
1265
|
+
throw new Error(
|
|
1266
|
+
`conflicting storage declaration for registry: already ${JSON.stringify(existing)}, cannot re-declare ` +
|
|
1267
|
+
`${JSON.stringify(storage)}`,
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
return registry;
|
|
1271
|
+
}
|
|
1272
|
+
Object.defineProperty(registry, syncRegistryStorageSymbol, {
|
|
1273
|
+
value: storage,
|
|
1274
|
+
enumerable: false,
|
|
1275
|
+
configurable: false,
|
|
1276
|
+
writable: false,
|
|
1277
|
+
});
|
|
1278
|
+
return registry;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
/**
|
|
1282
|
+
* Read the storage declaration (ADR-0049 decision 1, ADR-0047) a registry was built with, or `undefined`
|
|
1283
|
+
* when none was declared (a bare registry map, or a definition with no `storage`). The client resolves the
|
|
1284
|
+
* effective mint durability as `getSyncRegistryStorage(registry)?.durability ?? "relaxed"` at its single
|
|
1285
|
+
* mint seam — the storage twin of {@link getSyncRegistrySchema}.
|
|
1286
|
+
*/
|
|
1287
|
+
export function getSyncRegistryStorage<TRegistry extends SyncTableRegistry>(
|
|
1288
|
+
registry: TRegistry,
|
|
1289
|
+
): SyncStorageDeclaration | undefined {
|
|
1290
|
+
const storage = Reflect.get(registry, syncRegistryStorageSymbol) as unknown;
|
|
1291
|
+
return storage != null && typeof storage === "object" ? (storage as SyncStorageDeclaration) : undefined;
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* Fail-closed at module-eval (ADR-0049 decision 1): a declared `storage.backend` / `storage.durability` must
|
|
1296
|
+
* be a known value, matching how the other registry axes reject bad input at `defineSyncRegistry`. An absent
|
|
1297
|
+
* declaration or absent field is fine — it resolves to the ADR-0047 defaults (`opfs` / `relaxed`).
|
|
1298
|
+
*/
|
|
1299
|
+
function validateStorageDeclaration(storage: SyncStorageDeclaration | undefined) {
|
|
1300
|
+
if (storage == null) {
|
|
1301
|
+
return;
|
|
1302
|
+
}
|
|
1303
|
+
if (storage.backend !== undefined && !isStorageBackend(storage.backend)) {
|
|
1304
|
+
throw new Error(
|
|
1305
|
+
`invalid storage.backend "${String(storage.backend)}": must be one of ${STORAGE_BACKENDS.join(", ")}`,
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
if (storage.durability !== undefined && !isStorageDurability(storage.durability)) {
|
|
1309
|
+
throw new Error(
|
|
1310
|
+
`invalid storage.durability "${String(storage.durability)}": must be one of ${STORAGE_DURABILITIES.join(", ")}`,
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1234
1315
|
function isSyncRegistryDefinition<TRegistry extends SyncTableRegistry>(
|
|
1235
1316
|
input: TRegistry | SyncRegistryDefinition<TRegistry>,
|
|
1236
1317
|
): input is SyncRegistryDefinition<TRegistry> {
|
|
@@ -1326,11 +1407,25 @@ function validateSyncTableEntry(entry: SyncTableEntry<AnyPgTable>) {
|
|
|
1326
1407
|
);
|
|
1327
1408
|
}
|
|
1328
1409
|
|
|
1410
|
+
// A managed field's `column` is the drizzle PROPERTY KEY of the target column, never its SQL column
|
|
1411
|
+
// name — ADR-0012 keeps the two distinct. Both consumers resolve by property key and canonicalise to
|
|
1412
|
+
// it (the client stamp in mutation.ts, the Server-version resolve in resolveServerVersionColumnName);
|
|
1413
|
+
// neither tolerates a column-name spelling. So a declaration that names the SQL column instead of the
|
|
1414
|
+
// property must fail HERE, at the boundary where declarations enter, rather than silently
|
|
1415
|
+
// half-resolving downstream.
|
|
1416
|
+
const managedFieldPropertyKeys = new Set(Object.keys(getColumns(entry.table)));
|
|
1417
|
+
|
|
1329
1418
|
// The single claim-stamping strategy (ADR-0026): an `authClaim` managed field reads a value from the
|
|
1330
1419
|
// verified request claims at a JSON path, emitted into the apply-function DDL — so the path segments
|
|
1331
1420
|
// must be plain identifiers and any cast a plain type name (never a value-injection surface). A
|
|
1332
1421
|
// non-authClaim field carrying claimPath/cast is an authoring slip; reject it rather than ignore it.
|
|
1333
1422
|
for (const field of entry.governance?.managedFields ?? []) {
|
|
1423
|
+
if (!managedFieldPropertyKeys.has(field.column as string)) {
|
|
1424
|
+
throw new Error(
|
|
1425
|
+
`managed field ${tableName}.${String(field.column)} names an unknown column: field.column must be the ` +
|
|
1426
|
+
`drizzle PROPERTY KEY of the target column, not its SQL column name (ADR-0012)`,
|
|
1427
|
+
);
|
|
1428
|
+
}
|
|
1334
1429
|
if (field.strategy === "authClaim") {
|
|
1335
1430
|
if (!Array.isArray(field.claimPath) || field.claimPath.length === 0) {
|
|
1336
1431
|
throw new Error(
|
package/src/runtime.ts
CHANGED
|
@@ -47,7 +47,7 @@ export interface MutationDiagnostics {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
* A registry-wide mutation-journal summary
|
|
50
|
+
* A registry-wide mutation-journal summary for warm-store observability: the
|
|
51
51
|
* per-status counts across EVERY writable table's journal, folded from one aggregate query/subscription over
|
|
52
52
|
* the `pgxsinkit_all_mutations` view — so a consumer renders a global sync indicator with ONE subscription
|
|
53
53
|
* instead of one live query per writable journal. Cheap enough to mount permanently.
|