@pgxsinkit/contracts 0.1.32 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/apply-strategy.d.ts +35 -0
- package/dist/apply-strategy.d.ts.map +1 -0
- package/dist/config.d.ts +210 -32
- package/dist/config.d.ts.map +1 -1
- package/dist/convergence-model.d.ts +106 -0
- package/dist/convergence-model.d.ts.map +1 -0
- package/dist/fingerprint.d.ts +136 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5222 -3494
- package/dist/index.js.map +37 -30
- package/dist/mutation.d.ts +78 -0
- package/dist/mutation.d.ts.map +1 -1
- package/dist/projection.d.ts +88 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/registry-diff.d.ts +50 -0
- package/dist/registry-diff.d.ts.map +1 -0
- package/dist/registry.d.ts +253 -2
- package/dist/registry.d.ts.map +1 -1
- package/dist/runtime.d.ts +21 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/sql-defaults.d.ts +27 -0
- package/dist/sql-defaults.d.ts.map +1 -0
- package/dist/sql-identifier.d.ts +26 -0
- package/dist/sql-identifier.d.ts.map +1 -0
- package/dist/supabase-rls.d.ts +99 -3
- package/dist/supabase-rls.d.ts.map +1 -1
- package/package.json +8 -3
- package/skills/registry-authoring/SKILL.md +376 -0
- package/src/apply-strategy.ts +124 -0
- package/src/config.ts +254 -106
- package/src/convergence-model.ts +190 -0
- package/src/fingerprint.ts +265 -0
- package/src/index.ts +7 -0
- package/src/mutation.ts +94 -2
- package/src/projection.ts +150 -0
- package/src/registry-diff.ts +215 -0
- package/src/registry.ts +751 -23
- package/src/runtime.ts +21 -1
- package/src/sql-defaults.ts +29 -0
- package/src/sql-identifier.ts +130 -0
- package/src/supabase-rls.ts +515 -37
package/dist/mutation.d.ts
CHANGED
|
@@ -4,16 +4,33 @@ export declare const mutationKindSchema: z.ZodEnum<{
|
|
|
4
4
|
delete: "delete";
|
|
5
5
|
update: "update";
|
|
6
6
|
}>;
|
|
7
|
+
/**
|
|
8
|
+
* The full Mutation-journal status machine — every status a journal row can hold, including the two
|
|
9
|
+
* terminal states (`quarantined`, ADR-0006; `conflicted`, ADR-0015). Kept in lockstep with the
|
|
10
|
+
* client's `MutationStatus` (packages/client/src/mutation-state.ts). Distinct from
|
|
11
|
+
* {@link mutationAckStatusSchema}, the narrower *transport* subset a server ack may carry.
|
|
12
|
+
*/
|
|
7
13
|
export declare const mutationStatusSchema: z.ZodEnum<{
|
|
8
14
|
acked: "acked";
|
|
15
|
+
conflicted: "conflicted";
|
|
9
16
|
failed: "failed";
|
|
10
17
|
pending: "pending";
|
|
18
|
+
quarantined: "quarantined";
|
|
19
|
+
rejected: "rejected";
|
|
11
20
|
sending: "sending";
|
|
12
21
|
}>;
|
|
22
|
+
/**
|
|
23
|
+
* The *transport* statuses a server ack may carry. `acked` (applied), `failed` (transient, retry),
|
|
24
|
+
* `conflicted` (ADR-0015 stale write — overlay KEPT, resolve as a new write), and `rejected` (ADR-0022 —
|
|
25
|
+
* a business rejection from the authoritative endpoint: a server-side invariant the client cannot evaluate
|
|
26
|
+
* said no, so the optimistic overlay is auto-discarded for the whole write-unit and the typed reason is
|
|
27
|
+
* surfaced).
|
|
28
|
+
*/
|
|
13
29
|
export declare const mutationAckStatusSchema: z.ZodEnum<{
|
|
14
30
|
acked: "acked";
|
|
15
31
|
conflicted: "conflicted";
|
|
16
32
|
failed: "failed";
|
|
33
|
+
rejected: "rejected";
|
|
17
34
|
}>;
|
|
18
35
|
export declare const entityKeySchema: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
19
36
|
export declare const mutationEnvelopeSchema: z.ZodObject<{
|
|
@@ -28,6 +45,7 @@ export declare const mutationEnvelopeSchema: z.ZodObject<{
|
|
|
28
45
|
}>;
|
|
29
46
|
payload: z.ZodUnknown;
|
|
30
47
|
clientTimestampUs: z.ZodString;
|
|
48
|
+
baseServerVersion: z.ZodOptional<z.ZodString>;
|
|
31
49
|
}, z.core.$strict>;
|
|
32
50
|
export declare const mutationAckSchema: z.ZodObject<{
|
|
33
51
|
tableName: z.ZodString;
|
|
@@ -38,9 +56,11 @@ export declare const mutationAckSchema: z.ZodObject<{
|
|
|
38
56
|
acked: "acked";
|
|
39
57
|
conflicted: "conflicted";
|
|
40
58
|
failed: "failed";
|
|
59
|
+
rejected: "rejected";
|
|
41
60
|
}>;
|
|
42
61
|
serverUpdatedAtUs: z.ZodOptional<z.ZodString>;
|
|
43
62
|
conflictReason: z.ZodOptional<z.ZodString>;
|
|
63
|
+
rejectionReason: z.ZodOptional<z.ZodString>;
|
|
44
64
|
httpStatus: z.ZodOptional<z.ZodNumber>;
|
|
45
65
|
}, z.core.$strict>;
|
|
46
66
|
export declare const batchMutationRequestSchema: z.ZodObject<{
|
|
@@ -56,6 +76,32 @@ export declare const batchMutationRequestSchema: z.ZodObject<{
|
|
|
56
76
|
}>;
|
|
57
77
|
payload: z.ZodUnknown;
|
|
58
78
|
clientTimestampUs: z.ZodString;
|
|
79
|
+
baseServerVersion: z.ZodOptional<z.ZodString>;
|
|
80
|
+
}, z.core.$strict>>;
|
|
81
|
+
}, z.core.$strict>;
|
|
82
|
+
/**
|
|
83
|
+
* The body of an **authoritative write** (ADR-0022 §3, mechanism c): one pessimistic write-**unit** — a set
|
|
84
|
+
* of co-committed mutations the server applies in its **own isolated transaction**, atomically, returning a
|
|
85
|
+
* per-mutation ack. `writeUnit` is the client's unit id (the dynamic `transaction` tag, or the static
|
|
86
|
+
* consistency-group key), carried for attribution. Distinct from `batchMutationRequestSchema` only in
|
|
87
|
+
* *semantics* (atomic unit + a constraint exception → a clean `rejected` ack, never a whole-batch 500) and
|
|
88
|
+
* its endpoint path; the mutation envelope is identical.
|
|
89
|
+
*/
|
|
90
|
+
export declare const authoritativeWriteRequestSchema: z.ZodObject<{
|
|
91
|
+
writeUnit: z.ZodOptional<z.ZodString>;
|
|
92
|
+
mutations: z.ZodArray<z.ZodObject<{
|
|
93
|
+
tableName: z.ZodString;
|
|
94
|
+
entityKey: z.ZodRecord<z.ZodString, z.ZodString>;
|
|
95
|
+
mutationId: z.ZodUUID;
|
|
96
|
+
mutationSeq: z.ZodNumber;
|
|
97
|
+
kind: z.ZodEnum<{
|
|
98
|
+
create: "create";
|
|
99
|
+
delete: "delete";
|
|
100
|
+
update: "update";
|
|
101
|
+
}>;
|
|
102
|
+
payload: z.ZodUnknown;
|
|
103
|
+
clientTimestampUs: z.ZodString;
|
|
104
|
+
baseServerVersion: z.ZodOptional<z.ZodString>;
|
|
59
105
|
}, z.core.$strict>>;
|
|
60
106
|
}, z.core.$strict>;
|
|
61
107
|
export declare const batchMutationAckSchema: z.ZodObject<{
|
|
@@ -68,12 +114,41 @@ export declare const batchMutationAckSchema: z.ZodObject<{
|
|
|
68
114
|
acked: "acked";
|
|
69
115
|
conflicted: "conflicted";
|
|
70
116
|
failed: "failed";
|
|
117
|
+
rejected: "rejected";
|
|
71
118
|
}>;
|
|
72
119
|
serverUpdatedAtUs: z.ZodOptional<z.ZodString>;
|
|
73
120
|
conflictReason: z.ZodOptional<z.ZodString>;
|
|
121
|
+
rejectionReason: z.ZodOptional<z.ZodString>;
|
|
74
122
|
httpStatus: z.ZodOptional<z.ZodNumber>;
|
|
75
123
|
}, z.core.$strict>>;
|
|
76
124
|
}, z.core.$strict>;
|
|
125
|
+
/**
|
|
126
|
+
* Per-mutation attribution for a structural batch rejection. The batch write is atomic —
|
|
127
|
+
* one structurally-invalid mutation rejects the whole POST with a single non-2xx — so the
|
|
128
|
+
* server names the offending mutation(s) here. That lets the client quarantine exactly those
|
|
129
|
+
* and keep the innocent siblings retryable, instead of dragging the whole offline queue to
|
|
130
|
+
* quarantine at the shared attempt cap.
|
|
131
|
+
*/
|
|
132
|
+
export declare const mutationRejectionSchema: z.ZodObject<{
|
|
133
|
+
tableName: z.ZodString;
|
|
134
|
+
mutationId: z.ZodUUID;
|
|
135
|
+
mutationSeq: z.ZodNumber;
|
|
136
|
+
reason: z.ZodString;
|
|
137
|
+
}, z.core.$strict>;
|
|
138
|
+
/**
|
|
139
|
+
* The body of a non-2xx batch-mutation response. `rejections` is present only when the fault
|
|
140
|
+
* is attributable to specific mutations (payload validation); whole-batch faults (execution
|
|
141
|
+
* 5xx, auth, malformed envelope) carry just a `message`. Unknown extra fields are stripped.
|
|
142
|
+
*/
|
|
143
|
+
export declare const batchMutationErrorSchema: z.ZodObject<{
|
|
144
|
+
message: z.ZodOptional<z.ZodString>;
|
|
145
|
+
rejections: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
146
|
+
tableName: z.ZodString;
|
|
147
|
+
mutationId: z.ZodUUID;
|
|
148
|
+
mutationSeq: z.ZodNumber;
|
|
149
|
+
reason: z.ZodString;
|
|
150
|
+
}, z.core.$strict>>>;
|
|
151
|
+
}, z.core.$strip>;
|
|
77
152
|
export type MutationKind = z.infer<typeof mutationKindSchema>;
|
|
78
153
|
export type MutationStatus = z.infer<typeof mutationStatusSchema>;
|
|
79
154
|
export type MutationAckStatus = z.infer<typeof mutationAckStatusSchema>;
|
|
@@ -81,5 +156,8 @@ export type EntityKey = z.infer<typeof entityKeySchema>;
|
|
|
81
156
|
export type MutationEnvelope = z.infer<typeof mutationEnvelopeSchema>;
|
|
82
157
|
export type MutationAck = z.infer<typeof mutationAckSchema>;
|
|
83
158
|
export type BatchMutationRequest = z.infer<typeof batchMutationRequestSchema>;
|
|
159
|
+
export type AuthoritativeWriteRequest = z.infer<typeof authoritativeWriteRequestSchema>;
|
|
84
160
|
export type BatchMutationAck = z.infer<typeof batchMutationAckSchema>;
|
|
161
|
+
export type MutationRejection = z.infer<typeof mutationRejectionSchema>;
|
|
162
|
+
export type BatchMutationError = z.infer<typeof batchMutationErrorSchema>;
|
|
85
163
|
//# sourceMappingURL=mutation.d.ts.map
|
package/dist/mutation.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutation.d.ts","sourceRoot":"","sources":["../src/mutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,eAAO,MAAM,kBAAkB;;;;EAAyC,CAAC;AACzE,eAAO,MAAM,oBAAoB
|
|
1
|
+
{"version":3,"file":"mutation.d.ts","sourceRoot":"","sources":["../src/mutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,eAAO,MAAM,kBAAkB;;;;EAAyC,CAAC;AACzE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;EAQ/B,CAAC;AACH;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB;;;;;EAAwD,CAAC;AAE7F,eAAO,MAAM,eAAe,uCAA+D,CAAC;AAE5F,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;kBAyBxB,CAAC;AAEZ,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;kBAqBnB,CAAC;AAEZ,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;;;kBAI5B,CAAC;AAEZ;;;;;;;GAOG;AACH,eAAO,MAAM,+BAA+B;;;;;;;;;;;;;;;;kBAKjC,CAAC;AAEZ,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;kBAIxB,CAAC;AAEZ;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB;;;;;kBASzB,CAAC;AAEZ;;;;GAIG;AACH,eAAO,MAAM,wBAAwB;;;;;;;;iBAGnC,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAC9D,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAClE,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AACxE,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AACxD,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AACtE,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAC5D,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAC;AAC9E,MAAM,MAAM,yBAAyB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,+BAA+B,CAAC,CAAC;AACxF,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AACtE,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AACxE,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,wBAAwB,CAAC,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { AnyPgTable } from "drizzle-orm/pg-core";
|
|
2
|
+
import type { Retention } from "./config";
|
|
3
|
+
import type { SyncTableEntry, SyncTableRegistry } from "./registry";
|
|
4
|
+
/**
|
|
5
|
+
* Per-client mode projection (ADR-0025). The authoritative (server) registry defines a table once with
|
|
6
|
+
* its full write contract; a client that must only *read* that table consumes the same entry through
|
|
7
|
+
* `asReadonly`. The read/identity contract — table, columns, primary key, synced-table name, column
|
|
8
|
+
* omission, the shape/row filter, AND the column-builder factory (`makeColumns`) — is preserved
|
|
9
|
+
* verbatim; the write-capability metadata is dropped:
|
|
10
|
+
*
|
|
11
|
+
* - `mode` flips to `readonly`;
|
|
12
|
+
* - the overlay-merged read-model `view` and the overlay/journal client projection (the local write
|
|
13
|
+
* machinery `@pgxsinkit/client` provisions for a writable table) are removed — a readonly client reads
|
|
14
|
+
* the synced base table directly;
|
|
15
|
+
* - `conflictPolicy`, `governance` (managed fields), and `writeMode` are removed — a readonly table has
|
|
16
|
+
* no write path, and `defineSyncRegistry` would otherwise still treat them as a writable declaration.
|
|
17
|
+
*
|
|
18
|
+
* The result is the same entry `defineSyncTable` would have produced for this table with
|
|
19
|
+
* `mode: "readonly"`, so `defineSyncRegistry` accepts it without the writable-table requirements
|
|
20
|
+
* (server-version field + `conflictPolicy`).
|
|
21
|
+
*
|
|
22
|
+
* Lifecycle axes (`consistencyGroup`, `subscription`, `retention`) are preserved — a projection may keep
|
|
23
|
+
* the authoritative grouping/timing/durability; change them on the projected entry if a client needs to.
|
|
24
|
+
*
|
|
25
|
+
* `makeColumns` (the column-builder factory `defineSyncTable` stashes) is carried too: since ADR-0029 P1
|
|
26
|
+
* the client derives EVERY synced-table object from it (`getSyncedLocalTable` → `projectedColumnBuilders`),
|
|
27
|
+
* so a readonly projection that dropped it could not build its own local synced read cache — the member-boot
|
|
28
|
+
* failure this keep-list's NOTE below predicted. It is read-derivation machinery, not a write handle.
|
|
29
|
+
*
|
|
30
|
+
* NOTE: if a new *read-relevant* field is added to {@link SyncTableEntry}, carry it here too (this builds
|
|
31
|
+
* the readonly entry by listing what to keep, so a new field is otherwise silently dropped).
|
|
32
|
+
*/
|
|
33
|
+
export declare function asReadonly<TTable extends AnyPgTable, TLocalTable extends AnyPgTable>(entry: SyncTableEntry<TTable, TLocalTable>): SyncTableEntry<TTable, TLocalTable>;
|
|
34
|
+
/**
|
|
35
|
+
* Project an entry onto a different **retention** (ADR-0021) — the per-table local-persistence axis:
|
|
36
|
+
* `persistent` (the durable PGlite/OPFS backend) | `ephemeral` (the table's whole local cluster — read
|
|
37
|
+
* cache, overlay, journal, sequence, views, reconcile function — emitted as `TEMP`/`pg_temp`, leaving no
|
|
38
|
+
* durable trace). Returns a copy of `entry` with `retention` overridden and **everything else preserved
|
|
39
|
+
* verbatim** (table, columns, mode, write contract, shape/row filter, the other lifecycle axes).
|
|
40
|
+
*
|
|
41
|
+
* Retention is a **lifecycle** axis, not a read-contract one, so a per-client registry may legitimately
|
|
42
|
+
* differ on it: {@link fingerprintReadContract} excludes retention, so a `withRetention(...)` of an
|
|
43
|
+
* authoritative entry still satisfies {@link assertReadContractPreserved}. This is how one authoritative
|
|
44
|
+
* registry yields a table that is durable for one client and ephemeral for another, e.g.
|
|
45
|
+
* `withRetention(asReadonly(authoritative.exam), "ephemeral")`.
|
|
46
|
+
*
|
|
47
|
+
* Unlike `mode` (whose overlay/journal/view fields {@link asReadonly} must re-resolve), retention has **no
|
|
48
|
+
* entry-derived fields** — the durable-vs-`TEMP` decision is taken by the client's schema generator at
|
|
49
|
+
* runtime from this scalar — so overriding it needs no re-resolution and a plain copy is correct. The
|
|
50
|
+
* return type is the input entry's exact type (write handles, create/update typing, governance marker all
|
|
51
|
+
* carry through); the cast restates what a generic object spread cannot prove.
|
|
52
|
+
*
|
|
53
|
+
* Two constraints carry over (enforced elsewhere, not by this helper):
|
|
54
|
+
* - **Consistency-group uniformity** (ADR-0021 §4): every table sharing a `consistencyGroup` must agree on
|
|
55
|
+
* retention — override the whole group, not one member, or `defineSyncRegistry` rejects the mixed group.
|
|
56
|
+
* A singleton-group table can be flipped alone.
|
|
57
|
+
* - **No durable offline write queue for `ephemeral`** (ADR-0021 composition rule): an ephemeral writable
|
|
58
|
+
* table's journal is `TEMP`, so a write staged offline does not survive session end — pair a must-not-lose
|
|
59
|
+
* write with a `pessimistic` write-mode (ADR-0022) or a prompt flush.
|
|
60
|
+
*/
|
|
61
|
+
export declare function withRetention<TEntry extends SyncTableEntry>(entry: TEntry, retention: Retention): TEntry;
|
|
62
|
+
/**
|
|
63
|
+
* The named `ephemeral` lifecycle projection — `withRetention(entry, "ephemeral")` — the lifecycle twin of
|
|
64
|
+
* {@link asReadonly}, for the common direction (a client wants no durable trace of a table the authoritative
|
|
65
|
+
* registry keeps `persistent`). Composes with `asReadonly`: `asEphemeral(asReadonly(authoritative.exam))`
|
|
66
|
+
* is a read-only, no-durable-trace projection. The reverse direction (an ephemeral authoritative table a
|
|
67
|
+
* client wants durable) is the bidirectional {@link withRetention} with `"persistent"`. See
|
|
68
|
+
* {@link withRetention} for the constraints that carry over (group uniformity; no durable offline queue).
|
|
69
|
+
*/
|
|
70
|
+
export declare function asEphemeral<TEntry extends SyncTableEntry>(entry: TEntry): TEntry;
|
|
71
|
+
/**
|
|
72
|
+
* Assert that a per-client `projection` registry preserves the **read contract** (ADR-0025) of the
|
|
73
|
+
* `authoritative` registry it projects from. For every table the projection declares, its
|
|
74
|
+
* {@link fingerprintReadContract} must equal the authoritative entry's: a projection may differ only in
|
|
75
|
+
* write capability and lifecycle orchestration, never in the data it syncs (columns, primary key,
|
|
76
|
+
* row-filter shape). A table present in the authoritative registry but absent from the projection is a
|
|
77
|
+
* permitted subset; a table in the projection with no authoritative source is an error (no contract to
|
|
78
|
+
* project from).
|
|
79
|
+
*
|
|
80
|
+
* Throws, naming the divergent tables, on any mismatch. Call it where the client registries are assembled
|
|
81
|
+
* (module-eval or a test) so a drifted projection fails closed instead of silently serving different rows
|
|
82
|
+
* to different clients. The `customWhere` body is invisible to the fingerprint — bump
|
|
83
|
+
* {@link RowFilterSpec.revision} so a logic-only divergence is caught.
|
|
84
|
+
*/
|
|
85
|
+
export declare function assertReadContractPreserved(authoritative: SyncTableRegistry, projection: SyncTableRegistry, options?: {
|
|
86
|
+
label?: string;
|
|
87
|
+
}): void;
|
|
88
|
+
//# sourceMappingURL=projection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../src/projection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,OAAO,KAAK,EAAgC,cAAc,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAElG;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,UAAU,CAAC,MAAM,SAAS,UAAU,EAAE,WAAW,SAAS,UAAU,EAClF,KAAK,EAAE,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC,GACzC,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC,CA2BrC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAAC,MAAM,SAAS,cAAc,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,GAAG,MAAM,CAExG;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,MAAM,SAAS,cAAc,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEhF;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2BAA2B,CACzC,aAAa,EAAE,iBAAiB,EAChC,UAAU,EAAE,iBAAiB,EAC7B,OAAO,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAC3B,IAAI,CAsBN"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type CanonicalTable } from "./fingerprint";
|
|
2
|
+
import type { SyncTableRegistry } from "./registry";
|
|
3
|
+
/**
|
|
4
|
+
* The registry-diff gate (ADR-0006): classify a registry change as
|
|
5
|
+
* `compatible | risky | breaking` so loss-detection happens at *authoring* time, not
|
|
6
|
+
* as a runtime surprise. A breaking diff is a conscious release decision — rework to
|
|
7
|
+
* expand/contract, or accept-and-notify. This catches the one case the runtime cannot:
|
|
8
|
+
* silent column *repurposing* (a same-named column whose type/meaning changed).
|
|
9
|
+
*
|
|
10
|
+
* pgxsinkit ships this mechanism; enforcement (whether a non-zero check blocks CI) is
|
|
11
|
+
* the consumer's, via a committed lock that makes a breaking change a reviewable diff.
|
|
12
|
+
*/
|
|
13
|
+
export type RegistryChangeSeverity = "compatible" | "risky" | "breaking";
|
|
14
|
+
export interface RegistryChange {
|
|
15
|
+
severity: RegistryChangeSeverity;
|
|
16
|
+
table: string;
|
|
17
|
+
detail: string;
|
|
18
|
+
}
|
|
19
|
+
export interface RegistryDiff {
|
|
20
|
+
severity: RegistryChangeSeverity;
|
|
21
|
+
changes: RegistryChange[];
|
|
22
|
+
}
|
|
23
|
+
/** A committed baseline of a registry's shape — the enforcement surface for the gate. */
|
|
24
|
+
export interface RegistryLock {
|
|
25
|
+
version: string;
|
|
26
|
+
tables: CanonicalTable[];
|
|
27
|
+
}
|
|
28
|
+
/** Build the committed lock (fingerprint + canonical shape) for a registry. */
|
|
29
|
+
export declare function buildRegistryLock(registry: SyncTableRegistry): RegistryLock;
|
|
30
|
+
/** Classify the change from one canonical registry shape to another. */
|
|
31
|
+
export declare function diffCanonicalRegistries(previous: readonly CanonicalTable[], next: readonly CanonicalTable[]): RegistryDiff;
|
|
32
|
+
/** Classify the change between two registries. */
|
|
33
|
+
export declare function compareRegistries(previous: SyncTableRegistry, next: SyncTableRegistry): RegistryDiff;
|
|
34
|
+
/** Classify a registry against a committed lock baseline. */
|
|
35
|
+
export declare function diffRegistryAgainstLock(registry: SyncTableRegistry, lock: RegistryLock): RegistryDiff;
|
|
36
|
+
/**
|
|
37
|
+
* The consumer-facing check: `ok` is false on a breaking diff. The consumer wires the
|
|
38
|
+
* exit code (e.g. `process.exit(result.ok ? 0 : 1)`) into their own CI — pgxsinkit does
|
|
39
|
+
* not reach into anyone's pipeline.
|
|
40
|
+
*/
|
|
41
|
+
export declare function runRegistryCheck(input: {
|
|
42
|
+
registry: SyncTableRegistry;
|
|
43
|
+
lock: RegistryLock;
|
|
44
|
+
}): {
|
|
45
|
+
ok: boolean;
|
|
46
|
+
diff: RegistryDiff;
|
|
47
|
+
};
|
|
48
|
+
/** A human-readable, stable summary of a diff (one line per change). */
|
|
49
|
+
export declare function summarizeRegistryDiff(diff: RegistryDiff): string;
|
|
50
|
+
//# sourceMappingURL=registry-diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry-diff.d.ts","sourceRoot":"","sources":["../src/registry-diff.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6C,KAAK,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD;;;;;;;;;GASG;AAEH,MAAM,MAAM,sBAAsB,GAAG,YAAY,GAAG,OAAO,GAAG,UAAU,CAAC;AAEzE,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,sBAAsB,CAAC;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,sBAAsB,CAAC;IACjC,OAAO,EAAE,cAAc,EAAE,CAAC;CAC3B;AAED,yFAAyF;AACzF,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,cAAc,EAAE,CAAC;CAC1B;AAWD,+EAA+E;AAC/E,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,YAAY,CAE3E;AAiHD,wEAAwE;AACxE,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,SAAS,cAAc,EAAE,EACnC,IAAI,EAAE,SAAS,cAAc,EAAE,GAC9B,YAAY,CAqBd;AAED,kDAAkD;AAClD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,IAAI,EAAE,iBAAiB,GAAG,YAAY,CAEpG;AAED,6DAA6D;AAC7D,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,IAAI,EAAE,YAAY,GAAG,YAAY,CAErG;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,iBAAiB,CAAC;IAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GAAG;IAC5F,EAAE,EAAE,OAAO,CAAC;IACZ,IAAI,EAAE,YAAY,CAAC;CACpB,CAGA;AAED,wEAAwE;AACxE,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,YAAY,GAAG,MAAM,CAKhE"}
|
package/dist/registry.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ import { type AnyPgTable, type PgBigInt64Builder, type PgBuildColumns, type PgBu
|
|
|
3
3
|
import type { pgSchema } from "drizzle-orm/pg-core";
|
|
4
4
|
import type { ExtractTablesWithRelations } from "drizzle-orm/relations";
|
|
5
5
|
import { getColumns } from "drizzle-orm/utils";
|
|
6
|
-
import
|
|
6
|
+
import { type ApplyStrategy, type SyncColumnType } from "./apply-strategy";
|
|
7
|
+
import { type ClientProjectionSpec, type ConflictPolicy, type DeferrableConstraintSpec, type ManagedFieldApplyOn, type ManagedFieldSpec, type ManagedFieldStrategy, type PrimaryKeySpec, type Retention, type RowFilterSpec, type ServerProjectionSpec, type ShapeSpec, type ShapeSpecInput, type SubscriptionTiming, type TableGovernanceSpec as TableGovernanceSpecBase, type TableMode, type WriteMode } from "./config";
|
|
7
8
|
type PgSchemaType = ReturnType<typeof pgSchema>;
|
|
8
9
|
type AnyPgView = PgView;
|
|
9
10
|
type TableColumnsShape<TTable extends AnyPgTable> = TTable extends {
|
|
@@ -52,7 +53,63 @@ export interface SyncTableEntry<TTable extends AnyPgTable = AnyPgTable, TLocalTa
|
|
|
52
53
|
primaryKey: PrimaryKeySpec;
|
|
53
54
|
shape?: ShapeSpec;
|
|
54
55
|
clientProjection?: ClientProjectionSpecForTable<TTable>;
|
|
56
|
+
serverProjection?: ServerProjectionSpec;
|
|
55
57
|
governance?: TableGovernanceSpecForTable<TTable>;
|
|
58
|
+
/**
|
|
59
|
+
* Conflict policy (ADR-0015): what happens to a stale write on this table. **Required for writable
|
|
60
|
+
* tables** (registry validation rejects an undeclared one — the third hard-require); ignored for
|
|
61
|
+
* `readonly` tables (they have no write path). See {@link ConflictPolicy}.
|
|
62
|
+
*/
|
|
63
|
+
conflictPolicy?: ConflictPolicy;
|
|
64
|
+
/**
|
|
65
|
+
* Consistency group (ADR-0009 decision 2). Tables sharing a `consistencyGroup` are synced on one
|
|
66
|
+
* `MultiShapeStream` and committed atomically at a shared LSN frontier, so a local reader never
|
|
67
|
+
* sees one grouped table advanced past another for the same server transaction. Omitted → the
|
|
68
|
+
* table is its own singleton group (independent frontier, today's behaviour). The latency cost (a
|
|
69
|
+
* group advances only as fast as its slowest shape) is contained to the tables that opt in.
|
|
70
|
+
*/
|
|
71
|
+
consistencyGroup?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Subscription timing (ADR-0021): `eager` (default) | `lazy`. A `lazy` table is excluded from the
|
|
74
|
+
* boot subscription set and subscribed on first query-reference. A property of the **consistency
|
|
75
|
+
* group** — every table sharing a `consistencyGroup` must agree (validated). See
|
|
76
|
+
* {@link SubscriptionTiming}.
|
|
77
|
+
*/
|
|
78
|
+
subscription?: SubscriptionTiming;
|
|
79
|
+
/**
|
|
80
|
+
* Retention (ADR-0021): `persistent` (default) | `ephemeral`. An `ephemeral` table's whole local
|
|
81
|
+
* cluster is emitted as `TEMP` — no durable trace, no durable offline write queue. A property of the
|
|
82
|
+
* consistency group — every table sharing a `consistencyGroup` must agree (validated). See
|
|
83
|
+
* {@link Retention}.
|
|
84
|
+
*/
|
|
85
|
+
retention?: Retention;
|
|
86
|
+
/**
|
|
87
|
+
* Write-mode (ADR-0022): `optimistic` (default) | `pessimistic`. A `pessimistic` consistency group is a
|
|
88
|
+
* standing server-authoritative write-unit — its writes flush-route to the authoritative endpoint and
|
|
89
|
+
* the UI shows success only after the server confirms. Write-mode is a property of the **write-unit**;
|
|
90
|
+
* the static write-unit is the consistency group, so every table sharing a `consistencyGroup` must agree
|
|
91
|
+
* (validated). See {@link WriteMode}.
|
|
92
|
+
*/
|
|
93
|
+
writeMode?: WriteMode;
|
|
94
|
+
/**
|
|
95
|
+
* True when this entry is a read PROJECTION over a table OWNED by another entry (built by
|
|
96
|
+
* {@link defineReadProjection}). Such an entry owns no physical table — its `table` is the owner's,
|
|
97
|
+
* and only its `localTable` + `shape` are its own — so migration/apply/RLS generation skips it and a
|
|
98
|
+
* consumer's schema barrel must never export a fresh table for it. Absent → the entry owns its table.
|
|
99
|
+
*/
|
|
100
|
+
readProjection?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* The column-builder factory that produced this entry's table (set by {@link defineSyncTable}).
|
|
103
|
+
* Retained so {@link defineReadProjection} can reuse the owner's column definitions to build a typed
|
|
104
|
+
* column subset without restating them, AND — since ADR-0029 P1 — so the client can derive every
|
|
105
|
+
* synced-table object (the local synced read cache, overlay, journal) via
|
|
106
|
+
* `getSyncedLocalTable` → `projectedColumnBuilders`. It is therefore read-derivation machinery, not a
|
|
107
|
+
* write handle: it is carried through every projection ({@link asReadonly}, `withRetention`,
|
|
108
|
+
* `defineReadProjection`), NOT fingerprinted (functions are invisible to the read-contract hash), and
|
|
109
|
+
* required on every registered entry — `defineSyncRegistry`/`validateSyncTableEntry` reject a
|
|
110
|
+
* hand-assembled entry that lacks it, since the client hard-requires it at boot.
|
|
111
|
+
*/
|
|
112
|
+
makeColumns?: () => Record<string, ColumnBuilderBase>;
|
|
56
113
|
}
|
|
57
114
|
/** Column property key names from a `makeColumns` factory function. */
|
|
58
115
|
type ColumnKeys<TColumns extends Record<string, ColumnBuilderBase>> = keyof TColumns & string;
|
|
@@ -63,6 +120,27 @@ type ProjectedLocalTable<TName extends string, TColumns extends Record<string, C
|
|
|
63
120
|
columns: PgBuildColumns<TName, ProjectedColumnsShape<TColumns, TOmittedColumns>>;
|
|
64
121
|
dialect: "pg";
|
|
65
122
|
}>;
|
|
123
|
+
/**
|
|
124
|
+
* The local table TYPE for a {@link defineReadProjection} entry: the OWNER's already-built columns
|
|
125
|
+
* (`TableColumnsShape<TOwnerTable>`) restricted to the kept keys (`TKeptKey`), under the projection's own
|
|
126
|
+
* local name (`TAs`).
|
|
127
|
+
*
|
|
128
|
+
* Why this exists rather than reusing the inner `defineSyncTable`'s `localTable` type: a projection
|
|
129
|
+
* rebuilds its columns from the owner's stashed `makeColumns`, but {@link SyncTableEntry.makeColumns} is
|
|
130
|
+
* type-erased to `() => Record<string, ColumnBuilderBase>`, so a `localTable` inferred through it collapses
|
|
131
|
+
* to an open index signature — forcing bracket-access + `!` in consumers. Picking directly off
|
|
132
|
+
* `TOwnerTable`'s real column types restores rename-safe, per-column typing.
|
|
133
|
+
*
|
|
134
|
+
* Sound by construction — never an over-claim: the runtime keeps `columns ∪ primaryKey`, a SUPERSET of
|
|
135
|
+
* `TKeptKey`, so every typed column exists at runtime. A primary-key column not listed in `columns` is
|
|
136
|
+
* kept at runtime but omitted from the type (a safe under-claim; include it in `columns` to type it).
|
|
137
|
+
*/
|
|
138
|
+
type ProjectionLocalTable<TOwnerTable extends AnyPgTable, TAs extends string, TKeptKey extends string> = PgTableWithColumns<{
|
|
139
|
+
name: TAs;
|
|
140
|
+
schema: string | undefined;
|
|
141
|
+
columns: Pick<TableColumnsShape<TOwnerTable>, TKeptKey & keyof TableColumnsShape<TOwnerTable>>;
|
|
142
|
+
dialect: "pg";
|
|
143
|
+
}>;
|
|
66
144
|
type ReadModelView<TName extends string, TColumns extends Record<string, ColumnBuilderBase>, TOmittedColumns extends readonly ColumnKeys<TColumns>[]> = PgViewWithSelection<`${TName}_read_model`, true, PgBuildColumns<`${TName}_read_model`, ProjectedColumnsShape<TColumns, TOmittedColumns> & {
|
|
67
145
|
overlay_kind: SetNotNull<PgVarcharBuilder<[string, ...string[]]>>;
|
|
68
146
|
local_updated_at_us: SetNotNull<PgBigInt64Builder>;
|
|
@@ -80,6 +158,18 @@ export type SyncTableInputGovernance<TColumns extends Record<string, ColumnBuild
|
|
|
80
158
|
export type SyncTableInputProjection<TColumns extends Record<string, ColumnBuilderBase>, TOmittedColumns extends readonly ColumnKeys<TColumns>[] = []> = Omit<ClientProjectionSpec, "omitColumns"> & {
|
|
81
159
|
omitColumns?: TOmittedColumns;
|
|
82
160
|
};
|
|
161
|
+
/**
|
|
162
|
+
* A row filter for {@link defineSyncTable}'s `shape`: either a static {@link RowFilterSpec}, or a
|
|
163
|
+
* function of the table's built (typed) columns. The function form is the all-in-one way to author a
|
|
164
|
+
* typed-Drizzle row filter inline — reference columns through `c(columns.x)` exactly as `extras`
|
|
165
|
+
* does with its `self` argument, so `customWhere` builds parameterized Electric `where`s from real,
|
|
166
|
+
* rename-safe column objects instead of hand-written column-name strings.
|
|
167
|
+
*/
|
|
168
|
+
export type RowFilterInput<TColumns extends Record<string, ColumnBuilderBase>> = RowFilterSpec | ((columns: PgBuildExtraConfigColumns<TColumns>) => RowFilterSpec);
|
|
169
|
+
/** {@link ShapeSpecInput} whose `rowFilter` may also be a function of the built columns (typed by `TColumns`). */
|
|
170
|
+
export type ShapeSpecInputFor<TColumns extends Record<string, ColumnBuilderBase>> = Omit<ShapeSpecInput, "rowFilter"> & {
|
|
171
|
+
rowFilter?: RowFilterInput<TColumns>;
|
|
172
|
+
};
|
|
83
173
|
/**
|
|
84
174
|
* Input for `defineSyncTable`. Supply `tableName + makeColumns` — the Drizzle table
|
|
85
175
|
* (and, for `readwrite` mode, the read-model view) are created internally.
|
|
@@ -106,9 +196,36 @@ export type SyncTableInput<TName extends string, TColumns extends Record<string,
|
|
|
106
196
|
* Use an array with multiple entries for composite keys.
|
|
107
197
|
*/
|
|
108
198
|
primaryKey?: string[];
|
|
109
|
-
shape?:
|
|
199
|
+
shape?: ShapeSpecInputFor<TColumns>;
|
|
110
200
|
clientProjection?: SyncTableInputProjection<TColumns, TOmittedColumns>;
|
|
201
|
+
/** Server-side response-path projection (e.g. `rowTransform`). Server authority, not client shape. */
|
|
202
|
+
serverProjection?: ServerProjectionSpec;
|
|
111
203
|
governance?: SyncTableInputGovernance<TColumns>;
|
|
204
|
+
/**
|
|
205
|
+
* Conflict policy (ADR-0015): what happens to a stale write on this table. **Required for writable
|
|
206
|
+
* tables** — `defineSyncTable`/`defineSyncRegistry` reject a writable table without one. See
|
|
207
|
+
* {@link ConflictPolicy}.
|
|
208
|
+
*/
|
|
209
|
+
conflictPolicy?: ConflictPolicy;
|
|
210
|
+
/**
|
|
211
|
+
* Bind this table into a consistency group (ADR-0009 decision 2): grouped tables sync on one
|
|
212
|
+
* `MultiShapeStream` and commit atomically. Omit for the default singleton group. See
|
|
213
|
+
* {@link SyncTableEntry.consistencyGroup}.
|
|
214
|
+
*/
|
|
215
|
+
consistencyGroup?: string;
|
|
216
|
+
/**
|
|
217
|
+
* Subscription timing (ADR-0021): `eager` (default) | `lazy`. See
|
|
218
|
+
* {@link SyncTableEntry.subscription}.
|
|
219
|
+
*/
|
|
220
|
+
subscription?: SubscriptionTiming;
|
|
221
|
+
/**
|
|
222
|
+
* Retention (ADR-0021): `persistent` (default) | `ephemeral`. See {@link SyncTableEntry.retention}.
|
|
223
|
+
*/
|
|
224
|
+
retention?: Retention;
|
|
225
|
+
/**
|
|
226
|
+
* Write-mode (ADR-0022): `optimistic` (default) | `pessimistic`. See {@link SyncTableEntry.writeMode}.
|
|
227
|
+
*/
|
|
228
|
+
writeMode?: WriteMode;
|
|
112
229
|
};
|
|
113
230
|
export type SyncTableRegistry = Record<string, SyncTableEntry>;
|
|
114
231
|
export interface SyncRegistryDefinition<TRegistry extends SyncTableRegistry> {
|
|
@@ -147,6 +264,11 @@ export type SyncTableRecord<TRegistry extends SyncTableRegistry, TKey extends ke
|
|
|
147
264
|
export declare function defineSyncTable<const TName extends string, const TColumns extends Record<string, ColumnBuilderBase>, const TOmittedColumns extends readonly ColumnKeys<TColumns>[] = [], const TGovernance extends SyncTableInputGovernance<TColumns> | undefined = undefined>(input: Omit<SyncTableInput<TName, TColumns, TOmittedColumns>, "governance"> & {
|
|
148
265
|
governance?: TGovernance;
|
|
149
266
|
}): {
|
|
267
|
+
conflictPolicy?: ConflictPolicy;
|
|
268
|
+
consistencyGroup?: string;
|
|
269
|
+
subscription?: SubscriptionTiming;
|
|
270
|
+
retention?: Retention;
|
|
271
|
+
writeMode?: WriteMode;
|
|
150
272
|
mode: TableMode;
|
|
151
273
|
primaryKey: PrimaryKeySpec;
|
|
152
274
|
shape?: ShapeSpec;
|
|
@@ -348,6 +470,7 @@ export declare function defineSyncTable<const TName extends string, const TColum
|
|
|
348
470
|
}>; };
|
|
349
471
|
dialect: 'pg';
|
|
350
472
|
}>>;
|
|
473
|
+
serverProjection?: ServerProjectionSpec;
|
|
351
474
|
table: PgTableWithColumns<{
|
|
352
475
|
name: TName;
|
|
353
476
|
schema: undefined;
|
|
@@ -449,7 +572,105 @@ export declare function defineSyncTable<const TName extends string, const TColum
|
|
|
449
572
|
}>;
|
|
450
573
|
localTable: ProjectedLocalTable<TName, TColumns, TOmittedColumns>;
|
|
451
574
|
view?: ReadModelView<TName, TColumns, TOmittedColumns>;
|
|
575
|
+
makeColumns: () => Record<string, ColumnBuilderBase>;
|
|
452
576
|
} & SyncTableInputGovernanceMarker<TGovernance>;
|
|
577
|
+
/**
|
|
578
|
+
* Define a **read projection**: a second client shape over a table an `owner` entry already owns. The
|
|
579
|
+
* projection reads the SAME physical rows under a DISTINCT local identity (`as`) and its own narrower
|
|
580
|
+
* shape — a typed column subset and/or an admin/role-scoped `rowFilter` — without owning, migrating, or
|
|
581
|
+
* RLS-guarding any new table. The first use is a light admin view of a heavy authoring table (titles,
|
|
582
|
+
* not the jsonb), while the learner keeps reading the full table through the owner's shape.
|
|
583
|
+
*
|
|
584
|
+
* It is the *obvious, DRY* way to express "another shape over this table", versus the bare
|
|
585
|
+
* `shape.electricTable` string it replaces (a footgun — config that silently un-asserts table
|
|
586
|
+
* ownership):
|
|
587
|
+
*
|
|
588
|
+
* - **Owns nothing.** The returned entry's `table` IS `owner.table` (the same object), so there is no
|
|
589
|
+
* new `pgTable` to migrate or to leak into a drizzle-kit schema barrel. Only `localTable` (named `as`)
|
|
590
|
+
* and `shape` are its own. `readProjection` is set so generators skip it.
|
|
591
|
+
* - **DRY columns.** `columns` is a typed subset of the owner's column keys; the local table is built by
|
|
592
|
+
* filtering the owner's own column definitions (never restated), and the same subset becomes the
|
|
593
|
+
* Electric `columns` allow-list so an omitted (e.g. heavy jsonb) column never crosses the wire. The
|
|
594
|
+
* primary key is always kept. Omit `columns` to sync every column.
|
|
595
|
+
* - **Source is derived, never named.** The physical Electric target is taken from the owner — there is
|
|
596
|
+
* no consumer-facing source field to get wrong (see {@link ShapeSpec.electricTable}).
|
|
597
|
+
* - **Readonly.** A projection has no write path; the engine resolves an incoming shape request by its
|
|
598
|
+
* unique `shapeKey` (= `as`) and consults the derived physical target only on egress.
|
|
599
|
+
*
|
|
600
|
+
* The `rowFilter` callback receives the OWNER's full columns — `customWhere` runs in Electric against
|
|
601
|
+
* the physical table, so it may reference a column the local subset omits. RLS for the projection's
|
|
602
|
+
* reads lives on the OWNER's table (a projection adds no DDL to a table it does not own); its
|
|
603
|
+
* `customWhere` must be a subset of what that RLS allows.
|
|
604
|
+
*
|
|
605
|
+
* The `owner` may be a `defineSyncTable` entry OR an `asReadonly` of one — an `asReadonly` projection
|
|
606
|
+
* preserves the full read contract (physical table, columns, primary key) and only drops the write path,
|
|
607
|
+
* so projecting off it is equivalent to projecting off its writable source. A CHAINED read projection
|
|
608
|
+
* (an owner that is itself a `defineReadProjection`) is rejected, because it composes wrongly — see below.
|
|
609
|
+
*
|
|
610
|
+
* ### Server-side egress redaction (`serverProjection` + `serverOnlyColumns`)
|
|
611
|
+
*
|
|
612
|
+
* A projection may carry its own `serverProjection` (a {@link ServerProjectionSpec}, typically a
|
|
613
|
+
* `rowTransform`) — resolved by the projection's `shapeKey` and run on the proxy egress path for this
|
|
614
|
+
* shape only. The order on egress is **transform first, then omission**: the transform runs against the
|
|
615
|
+
* fetched row, then column omission strips this projection's omitted columns (the client keep-set is
|
|
616
|
+
* `columns ∪ primaryKey`) before the row reaches the client wire. This lets a "secure window" over a
|
|
617
|
+
* keyed table stream the body while stripping the keys per row.
|
|
618
|
+
*
|
|
619
|
+
* `serverOnlyColumns` are owner column keys the transform must READ but that are NOT in the client shape
|
|
620
|
+
* (e.g. a `keysWithheld` control flag). Such a key stays omitted from the client keep-set, yet is ADDED
|
|
621
|
+
* to the Electric fetch allow-list — so it is fetched from Electric, visible to the transform, and then
|
|
622
|
+
* stripped on egress by the same omission pass, never reaching the client. It requires a
|
|
623
|
+
* `serverProjection.rowTransform` (a fetch no transform reads is dead weight) AND `columns` (with
|
|
624
|
+
* `columns` omitted every column is already kept, so "server-only" is a contradiction), and must be
|
|
625
|
+
* disjoint from `columns` and the primary key.
|
|
626
|
+
*
|
|
627
|
+
* **No inheritance — ENFORCED.** A projection does NOT inherit its owner's `serverProjection`. That is
|
|
628
|
+
* deliberate: an inherited transform whose input column is absent from the projection's fetch list would
|
|
629
|
+
* read `undefined` and silently fail OPEN (serving the un-redacted body) — half-protection worse than
|
|
630
|
+
* none. Because a bare projection over a redacting owner would therefore egress RAW owner rows, the
|
|
631
|
+
* registry no longer merely warns: when `owner.serverProjection?.rowTransform` exists, this function
|
|
632
|
+
* THROWS at definition time unless the projection declares a posture. You must either declare your own
|
|
633
|
+
* `serverProjection` on the projection (typically the same transform fn, plus `serverOnlyColumns` for its
|
|
634
|
+
* control-flag inputs), or — only after confirming the projection's kept columns leak nothing — opt out
|
|
635
|
+
* explicitly with the literal `serverProjection: "unredacted"`, which attaches no transform (egress raw)
|
|
636
|
+
* but records that as a visible, reviewed decision at the definition site. The opt-out is meaningful only
|
|
637
|
+
* where it applies: `"unredacted"` over an owner with NO egress `rowTransform` is itself rejected, so a
|
|
638
|
+
* stale opt-out cannot silently pre-authorize a leak the day the owner grows one.
|
|
639
|
+
*/
|
|
640
|
+
export declare function defineReadProjection<const TOwnerTable extends AnyPgTable, const TOwnerLocal extends AnyPgTable, const TAs extends string, const TColumns extends readonly TableColumnKey<TOwnerTable>[] = readonly TableColumnKey<TOwnerTable>[]>(owner: SyncTableEntry<TOwnerTable, TOwnerLocal>, opts: {
|
|
641
|
+
/** The projection's distinct local identity — its PGlite table name AND its `shapeKey`. */
|
|
642
|
+
as: TAs;
|
|
643
|
+
/** Column keys (of the owner) to sync locally + fetch from Electric. The PK is always kept. Omit → all. */
|
|
644
|
+
columns?: TColumns;
|
|
645
|
+
/** Row filter for this shape; the callback form receives the owner's full (physical) columns. */
|
|
646
|
+
rowFilter?: RowFilterSpec | ((columns: TableColumnsShape<TOwnerTable>) => RowFilterSpec);
|
|
647
|
+
/**
|
|
648
|
+
* Server-side egress projection (ADR-0004) for THIS shape — typically a `rowTransform` that redacts a
|
|
649
|
+
* sub-document of a kept column conditionally on row data. A projection does NOT inherit its owner's
|
|
650
|
+
* `serverProjection` (see the docblock's no-inheritance caution): an inherited transform whose input
|
|
651
|
+
* column is unfetched would fail OPEN, so inheritance is refused, not silent. When the OWNER declares
|
|
652
|
+
* an egress `rowTransform`, this is therefore **required** — the registry throws at definition time
|
|
653
|
+
* unless you either declare your own spec here (typically the same transform fn, plus
|
|
654
|
+
* `serverOnlyColumns` for its control-flag inputs) OR opt out with the literal `"unredacted"`. Use
|
|
655
|
+
* `"unredacted"` only after confirming this projection's kept columns leak nothing; it attaches NO
|
|
656
|
+
* egress transform (the shape streams raw owner rows), but records that as a visible, reviewed
|
|
657
|
+
* decision at the definition site. `"unredacted"` over a transform-less owner is itself rejected — a
|
|
658
|
+
* stale opt-out would silently pre-authorize a leak the day the owner grows a transform.
|
|
659
|
+
*/
|
|
660
|
+
serverProjection?: ServerProjectionSpec | "unredacted";
|
|
661
|
+
/**
|
|
662
|
+
* Owner column keys the `serverProjection.rowTransform` must READ but which are NOT part of the client
|
|
663
|
+
* shape (e.g. a `keysWithheld` control flag). They are added to the Electric fetch allow-list so the
|
|
664
|
+
* transform can see them, then stripped on egress before the client wire. Requires
|
|
665
|
+
* `serverProjection.rowTransform` and `columns`; must be disjoint from `columns` and the primary key.
|
|
666
|
+
*/
|
|
667
|
+
serverOnlyColumns?: readonly TableColumnKey<TOwnerTable>[];
|
|
668
|
+
consistencyGroup?: string;
|
|
669
|
+
subscription?: SubscriptionTiming;
|
|
670
|
+
retention?: Retention;
|
|
671
|
+
}): SyncTableEntry<TOwnerTable, ProjectionLocalTable<TOwnerTable, TAs, TColumns[number]>> & {
|
|
672
|
+
readProjection: true;
|
|
673
|
+
};
|
|
453
674
|
export declare function defineSyncRegistry<const TRegistry extends {
|
|
454
675
|
[TKey in keyof TRegistry]: SyncTableEntry;
|
|
455
676
|
}>(registry: TRegistry): TRegistry;
|
|
@@ -467,8 +688,38 @@ export declare function getOmittedProjectedColumns<TTable extends AnyPgTable>(en
|
|
|
467
688
|
column: import("drizzle-orm/pg-core").PgColumn<any, import("drizzle-orm/pg-core").PgColumnBaseConfig<any>, {}>;
|
|
468
689
|
}[];
|
|
469
690
|
export declare function getProjectedColumnNames<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>): string[];
|
|
691
|
+
/**
|
|
692
|
+
* Resolves the {@link SyncColumnType}s of a synced table's client-projected columns from its Drizzle
|
|
693
|
+
* definition (ADR-0009 decision 3) — the same `getSQLType()`/`dimensions` introspection the local
|
|
694
|
+
* schema generator uses, so the apply ladder and the generated DDL can never disagree about a
|
|
695
|
+
* column's type. Drives both {@link classifyTableApplyStrategy} and the engine's `json` apply cast,
|
|
696
|
+
* removing the runtime `information_schema` round-trip.
|
|
697
|
+
*/
|
|
698
|
+
export declare function deriveSyncColumnTypes<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>): SyncColumnType[];
|
|
699
|
+
/**
|
|
700
|
+
* `serial`/`bigserial`/`smallserial` are DDL-position conveniences (an integer column plus a sequence
|
|
701
|
+
* default), NOT real cast-position types: `json_to_recordset(… AS x(id serial))` and `value::serial`
|
|
702
|
+
* are both invalid SQL. {@link SyncColumnType.sqlType} is contracted to be usable verbatim as a cast
|
|
703
|
+
* type, so we normalise the serial family to its underlying integer type here — the single derivation
|
|
704
|
+
* point — which also lets the column classify as COPY-safe rather than falling to the `insert` floor.
|
|
705
|
+
*
|
|
706
|
+
* Exported so any code deriving a {@link SyncColumnType} from a bare Drizzle column (e.g. test-support
|
|
707
|
+
* `makeApplyTarget`, which cannot go through {@link deriveSyncColumnTypes} without a registry entry)
|
|
708
|
+
* stays byte-faithful to this single normalisation rather than re-`getSQLType()`ing without it.
|
|
709
|
+
*/
|
|
710
|
+
export declare function normalizeCastPositionType(sqlType: string): string;
|
|
711
|
+
/** The statically-chosen bulk-insert strategy for a synced table (ADR-0009 decision 3). */
|
|
712
|
+
export declare function classifyTableApplyStrategy<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>): ApplyStrategy;
|
|
470
713
|
export declare function getOmittedProjectedColumnNames<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>): string[];
|
|
471
714
|
export declare function getLocalSyncedTablePrimaryKeyColumns<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>): string[];
|
|
715
|
+
/**
|
|
716
|
+
* The Server version column (ADR-0010): the `nowMicroseconds`-on-update managed field a writable
|
|
717
|
+
* table stamps on every write (conventionally `updated_at_us`), made strictly monotonic by the
|
|
718
|
+
* applier. Returns its **column name**, resolving the managed field's drizzle property key. Returns
|
|
719
|
+
* `undefined` when none is declared — registry validation rejects that for writable tables, so the
|
|
720
|
+
* convergence barrier never has to degrade.
|
|
721
|
+
*/
|
|
722
|
+
export declare function resolveServerVersionColumnName<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>): string | undefined;
|
|
472
723
|
export declare function attachSyncRegistrySchema<TRegistry extends SyncTableRegistry>(registry: TRegistry, schema?: string): TRegistry;
|
|
473
724
|
export declare function getSyncRegistrySchema<TRegistry extends SyncTableRegistry>(registry: TRegistry): string;
|
|
474
725
|
export {};
|