@pgxsinkit/contracts 0.1.32 → 0.1.36
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/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @pgxsinkit/contracts
|
|
2
|
+
|
|
3
|
+
Shared contracts for [pgxsinkit](https://pgxsinkit.github.io): the sync registry
|
|
4
|
+
(`defineSyncTable` / `defineSyncRegistry`), the transport DTOs both the read and write
|
|
5
|
+
paths use, the registry fingerprint and diff gate, and the SQL identifier resolver.
|
|
6
|
+
|
|
7
|
+
Install it always — every other `@pgxsinkit/*` package builds on it.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun add @pgxsinkit/contracts
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
See the [documentation](https://pgxsinkit.github.io) for the full model and API.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static, type-driven read-path apply ladder (ADR-0009 decision 3).
|
|
3
|
+
*
|
|
4
|
+
* We own the registry's column types ahead of time and only ever target the latest PGlite, so the
|
|
5
|
+
* bulk-insert strategy for a synced table is chosen **once, statically** from its column types —
|
|
6
|
+
* never by probing `information_schema` at runtime. This module is the pure, dependency-free core
|
|
7
|
+
* (no registry, no Drizzle import) so it stays in the fast unit lane; {@link deriveSyncColumnTypes}
|
|
8
|
+
* in `registry.ts` bridges a `SyncTableEntry`'s Drizzle columns into {@link SyncColumnType}s.
|
|
9
|
+
*/
|
|
10
|
+
/** The bulk-insert path the engine uses for a fresh subscription's initial backfill. */
|
|
11
|
+
export type ApplyStrategy = "copy" | "json" | "insert";
|
|
12
|
+
/** Resolved column type descriptor for the apply ladder — derived from the registry, not the DB. */
|
|
13
|
+
export interface SyncColumnType {
|
|
14
|
+
/** SQL column name (not the Drizzle property key). */
|
|
15
|
+
name: string;
|
|
16
|
+
/**
|
|
17
|
+
* The column's base SQL type from Drizzle's `getSQLType()` (e.g. `"uuid"`, `"text"`, `"jsonb"`,
|
|
18
|
+
* `"bigint"`, `"timestamp with time zone"`). Array-ness is carried separately in {@link isArray};
|
|
19
|
+
* this string is the element/base type and is usable verbatim as a `json_to_recordset` cast type
|
|
20
|
+
* (with `[]` appended when `isArray`).
|
|
21
|
+
*/
|
|
22
|
+
sqlType: string;
|
|
23
|
+
/** True when the column is an array of any dimension. */
|
|
24
|
+
isArray: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Chooses the bulk-insert strategy for a table from its column types (ADR-0009 decision 3):
|
|
28
|
+
* - every column COPY-safe → `copy`;
|
|
29
|
+
* - else every column COPY-safe ∪ array/json/jsonb → `json`;
|
|
30
|
+
* - else → `insert` (the always-correct floor for anything not positively whitelisted).
|
|
31
|
+
*
|
|
32
|
+
* Pure and total: an empty column list falls to `insert`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function classifyApplyStrategy(columns: readonly SyncColumnType[]): ApplyStrategy;
|
|
35
|
+
//# sourceMappingURL=apply-strategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply-strategy.d.ts","sourceRoot":"","sources":["../src/apply-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,wFAAwF;AACxF,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEvD,oGAAoG;AACpG,MAAM,WAAW,cAAc;IAC7B,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,OAAO,EAAE,OAAO,CAAC;CAClB;AAiED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,GAAG,aAAa,CAwBvF"}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,5 +1,79 @@
|
|
|
1
|
+
import { type AnyColumn, type SQL } from "drizzle-orm";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
export type TableMode = "readonly" | "writeonly" | "readwrite";
|
|
4
|
+
/**
|
|
5
|
+
* The per-writable-table Conflict policy (ADR-0015): what happens to a **stale** write — one whose
|
|
6
|
+
* Base server version is behind the row's current Server version at apply (an external write
|
|
7
|
+
* interleaved). It is a **required** declaration on every writable table; there is no silent default
|
|
8
|
+
* (registry validation rejects an undeclared writable table — the third hard-require). v1:
|
|
9
|
+
*
|
|
10
|
+
* - `last-write-wins` — apply the stale write anyway. This is today's implicit behaviour, but now a
|
|
11
|
+
* conscious, named choice rather than silent clobbering.
|
|
12
|
+
* - `reject-if-stale` — do not apply; surface the conflict so the user's edit is kept (the optimistic
|
|
13
|
+
* Overlay stays, marked conflicted) and resolved as a new write.
|
|
14
|
+
*
|
|
15
|
+
* `field-merge` (apply only the changed fields over the current row) and `custom-resolver` (a client
|
|
16
|
+
* re-resolution protocol) are reserved; the union is additive so they slot in without a breaking
|
|
17
|
+
* change.
|
|
18
|
+
*/
|
|
19
|
+
export type ConflictPolicy = "last-write-wins" | "reject-if-stale";
|
|
20
|
+
/** The Conflict policy values accepted in v1 (ADR-0015). Source of truth for registry validation. */
|
|
21
|
+
export declare const CONFLICT_POLICIES: readonly ["last-write-wins", "reject-if-stale"];
|
|
22
|
+
/** Type guard: is `value` one of the v1 {@link ConflictPolicy} values? */
|
|
23
|
+
export declare function isConflictPolicy(value: unknown): value is ConflictPolicy;
|
|
24
|
+
/**
|
|
25
|
+
* Subscription timing for a synced table (ADR-0021): **when** its Electric shape subscribes.
|
|
26
|
+
*
|
|
27
|
+
* - `eager` (default) — subscribed in the boot set, as today.
|
|
28
|
+
* - `lazy` — excluded from boot; subscribed on first query-reference. With `persistent` retention,
|
|
29
|
+
* first use is a one-time ignition that promotes the table to a normal eager table for subsequent
|
|
30
|
+
* sessions; with `ephemeral` retention it is session-scoped.
|
|
31
|
+
*
|
|
32
|
+
* A property of the **consistency group**: every table sharing a `consistencyGroup` must agree, since
|
|
33
|
+
* a group commits atomically on one `MultiShapeStream` and cannot be partly lazy (ADR-0021 §4).
|
|
34
|
+
*/
|
|
35
|
+
export type SubscriptionTiming = "eager" | "lazy";
|
|
36
|
+
/** The {@link SubscriptionTiming} values. Source of truth for registry validation. */
|
|
37
|
+
export declare const SUBSCRIPTION_TIMINGS: readonly ["eager", "lazy"];
|
|
38
|
+
/** Type guard: is `value` a {@link SubscriptionTiming}? */
|
|
39
|
+
export declare function isSubscriptionTiming(value: unknown): value is SubscriptionTiming;
|
|
40
|
+
/**
|
|
41
|
+
* Retention for a synced table (ADR-0021): **whether** its local copy is durable.
|
|
42
|
+
*
|
|
43
|
+
* - `persistent` (default) — the durable PGlite backend with a resumable subscription-state.
|
|
44
|
+
* - `ephemeral` — the table's whole per-table local cluster (read cache, overlay, journal, sequence,
|
|
45
|
+
* views, reconcile trigger/function) is emitted as `TEMP`, so reads **and** writes leave no durable
|
|
46
|
+
* trace. Consequence: no durable offline write queue — pair a must-not-lose write with a pessimistic
|
|
47
|
+
* flush (ADR-0022).
|
|
48
|
+
*
|
|
49
|
+
* Like {@link SubscriptionTiming}, a property of the consistency group: every table in a group agrees.
|
|
50
|
+
*/
|
|
51
|
+
export type Retention = "persistent" | "ephemeral";
|
|
52
|
+
/** The {@link Retention} values. Source of truth for registry validation. */
|
|
53
|
+
export declare const RETENTIONS: readonly ["persistent", "ephemeral"];
|
|
54
|
+
/** Type guard: is `value` a {@link Retention}? */
|
|
55
|
+
export declare function isRetention(value: unknown): value is Retention;
|
|
56
|
+
/**
|
|
57
|
+
* Write-mode (ADR-0022): **how** a write reaches the server — the write-side twin of {@link Retention}.
|
|
58
|
+
*
|
|
59
|
+
* - `optimistic` (default) — the write enters the local journal with an optimistic overlay, the UI updates
|
|
60
|
+
* immediately, and the convergence loop flushes the journal as one all-or-nothing batch; the canonical
|
|
61
|
+
* row returns via the sync echo. The path that has always existed.
|
|
62
|
+
* - `pessimistic` — the write is **server-authoritative**: it flush-routes to an authoritative endpoint that
|
|
63
|
+
* applies it in its own isolated, serialised transaction and returns a per-mutation result (accepted, or
|
|
64
|
+
* rejected-with-typed-reason) **before** the UI shows success. For invariants the client cannot evaluate
|
|
65
|
+
* locally — a capacity/quota/uniqueness gate enforced by a server-side rule.
|
|
66
|
+
*
|
|
67
|
+
* Write-mode is a property of an atomic **write-unit**, not a single table (ADR-0022 §1): a unit is uniformly
|
|
68
|
+
* one mode. The *static* write-unit is the **consistency group** — so, like {@link SubscriptionTiming} and
|
|
69
|
+
* {@link Retention}, every table sharing a `consistencyGroup` must agree (validated). A *dynamic* override is
|
|
70
|
+
* the imperative `transaction({ mode })` block, which scopes a mode to an ad-hoc set of mutations.
|
|
71
|
+
*/
|
|
72
|
+
export type WriteMode = "optimistic" | "pessimistic";
|
|
73
|
+
/** The {@link WriteMode} values. Source of truth for registry validation. */
|
|
74
|
+
export declare const WRITE_MODES: readonly ["optimistic", "pessimistic"];
|
|
75
|
+
/** Type guard: is `value` a {@link WriteMode}? */
|
|
76
|
+
export declare function isWriteMode(value: unknown): value is WriteMode;
|
|
3
77
|
/**
|
|
4
78
|
* Minimal verified-JWT claim shape the sync layer understands. Providers may
|
|
5
79
|
* attach arbitrary extra claims; those stay reachable through index access and
|
|
@@ -19,12 +93,26 @@ export interface PrimaryKeySpec {
|
|
|
19
93
|
export interface ShapeSpec {
|
|
20
94
|
tableName: string;
|
|
21
95
|
shapeKey: string;
|
|
96
|
+
/**
|
|
97
|
+
* INTERNAL / resolved — the physical Postgres table this shape reads, when it differs from the
|
|
98
|
+
* shape's own `tableName`. A read PROJECTION (`defineReadProjection`) sets it to the OWNING table's
|
|
99
|
+
* name so several shapes can read one physical table under distinct `shapeKey`s; the engine resolves
|
|
100
|
+
* an incoming request by `shapeKey` and consults this only on egress, to build the upstream Electric
|
|
101
|
+
* `table` param. `attachSyncRegistrySchema` also fills/qualifies it for schema-bound registries.
|
|
102
|
+
*
|
|
103
|
+
* Not a consumer input — there is no valid reason to hand-set it (it can only be redundant with, or
|
|
104
|
+
* wrong about, the table you are reading), so it is omitted from {@link ShapeSpecInput}. The
|
|
105
|
+
* combinator derives it from the owner; `defineSyncTable` never sets it from input.
|
|
106
|
+
*/
|
|
22
107
|
electricTable?: string;
|
|
23
108
|
rowFilter?: RowFilterSpec;
|
|
24
109
|
}
|
|
25
110
|
/** Input variant of {@link ShapeSpec} where `tableName` and `shapeKey` are optional.
|
|
26
|
-
* When omitted, both default to the top-level `tableName` of the `defineSyncTable` call.
|
|
27
|
-
|
|
111
|
+
* When omitted, both default to the top-level `tableName` of the `defineSyncTable` call.
|
|
112
|
+
* `electricTable` is deliberately absent — it is a resolved/internal field, never a consumer input
|
|
113
|
+
* (see {@link ShapeSpec.electricTable}); a read projection over an existing table is authored with
|
|
114
|
+
* `defineReadProjection`, which derives it from the owner. */
|
|
115
|
+
export type ShapeSpecInput = Omit<ShapeSpec, "tableName" | "shapeKey" | "electricTable"> & {
|
|
28
116
|
tableName?: string;
|
|
29
117
|
shapeKey?: string;
|
|
30
118
|
};
|
|
@@ -50,13 +138,21 @@ export interface ClientProjectionSpec {
|
|
|
50
138
|
overlayTable?: string;
|
|
51
139
|
journalTable?: string;
|
|
52
140
|
omitColumns?: readonly string[];
|
|
141
|
+
localPrimaryKey?: PrimaryKeySpec;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Server-side projection applied in the proxy response path. This is server
|
|
145
|
+
* authority, not client shape — it never alters the local PGlite schema or the
|
|
146
|
+
* Electric shape URL — so it lives apart from {@link ClientProjectionSpec} (ADR-0004).
|
|
147
|
+
*/
|
|
148
|
+
export interface ServerProjectionSpec {
|
|
53
149
|
/**
|
|
54
150
|
* Optional per-row rewrite applied in the proxy response path. Runs before column
|
|
55
|
-
* omission, so it may read a column (e.g. a control flag) that
|
|
56
|
-
* removes from the client-visible row. See
|
|
151
|
+
* omission, so it may read a column (e.g. a control flag) that
|
|
152
|
+
* `clientProjection.omitColumns` then removes from the client-visible row. See
|
|
153
|
+
* {@link RowTransform}.
|
|
57
154
|
*/
|
|
58
155
|
rowTransform?: RowTransform;
|
|
59
|
-
localPrimaryKey?: PrimaryKeySpec;
|
|
60
156
|
}
|
|
61
157
|
export interface DeferrableConstraintSpec {
|
|
62
158
|
constraintName: string;
|
|
@@ -64,11 +160,36 @@ export interface DeferrableConstraintSpec {
|
|
|
64
160
|
initiallyDeferred?: boolean;
|
|
65
161
|
}
|
|
66
162
|
export type ManagedFieldApplyOn = "create" | "update";
|
|
67
|
-
|
|
163
|
+
/**
|
|
164
|
+
* How the applier stamps a server-managed column (it overrides any client-sent value — the client write
|
|
165
|
+
* payload omits managed fields, and the apply function re-derives them under the verified request claims):
|
|
166
|
+
*
|
|
167
|
+
* - `nowMicroseconds` — `clock_timestamp()` microseconds, stamped via the canonical `pgxsinkit_clock_us()`
|
|
168
|
+
* DB function (installed by the utilities migration). The audit/version columns (`created_at_us`,
|
|
169
|
+
* `updated_at_us`); the `updated_at_us`-on-update field is the strictly-monotonic Server version (ADR-0010).
|
|
170
|
+
* - `authClaim` — a value read from the **verified JWT claims** at a JSON {@link ManagedFieldSpec.claimPath}
|
|
171
|
+
* (e.g. `["sub"]` for the auth subject, or `["app_metadata","person_id"]` for an app-minted identity). This
|
|
172
|
+
* is the single claim-stamping strategy: the old `authUid` is exactly `{ claimPath: ["sub"], cast: "uuid" }`,
|
|
173
|
+
* so there is one mechanism, not a `sub`-only special case beside a general one.
|
|
174
|
+
*/
|
|
175
|
+
export type ManagedFieldStrategy = "nowMicroseconds" | "authClaim";
|
|
68
176
|
export interface ManagedFieldSpec {
|
|
69
177
|
column: string;
|
|
70
178
|
applyOn: ManagedFieldApplyOn[];
|
|
71
179
|
strategy: ManagedFieldStrategy;
|
|
180
|
+
/**
|
|
181
|
+
* For `strategy: "authClaim"` only (required there, forbidden otherwise): the JSON path into the verified
|
|
182
|
+
* request claims to stamp from — `["sub"]`, `["app_metadata", "person_id"]`, etc. Each segment must be a
|
|
183
|
+
* plain identifier (`[A-Za-z_][A-Za-z0-9_]*`); it is emitted into the apply-function DDL as a `jsonb #>>`
|
|
184
|
+
* text-array path, so it is never a value-injection surface.
|
|
185
|
+
*/
|
|
186
|
+
claimPath?: string[];
|
|
187
|
+
/**
|
|
188
|
+
* Optional SQL cast for an `authClaim` value (`jsonb #>>` yields text). Defaults to the **target column's
|
|
189
|
+
* own SQL type** (so a `uuid` column casts to `uuid` with no declaration needed). Override only to force a
|
|
190
|
+
* different cast; must be a plain SQL type name.
|
|
191
|
+
*/
|
|
192
|
+
cast?: string;
|
|
72
193
|
}
|
|
73
194
|
export interface TableGovernanceSpec {
|
|
74
195
|
deferrableConstraints?: DeferrableConstraintSpec[];
|
|
@@ -80,6 +201,27 @@ export interface TableSpecInput {
|
|
|
80
201
|
shape?: ShapeSpec;
|
|
81
202
|
clientProjection?: ClientProjectionSpec;
|
|
82
203
|
governance?: TableGovernanceSpec;
|
|
204
|
+
/**
|
|
205
|
+
* Consistency group (ADR-0009 decision 2): tables sharing a group sync on one `MultiShapeStream`
|
|
206
|
+
* and commit atomically at a shared LSN frontier. Absent → the table is its own singleton group.
|
|
207
|
+
*/
|
|
208
|
+
consistencyGroup?: string;
|
|
209
|
+
/**
|
|
210
|
+
* Subscription timing (ADR-0021). Absent → `eager`. A `lazy` table is excluded from the boot
|
|
211
|
+
* subscription set and subscribed on first query-reference. See {@link SubscriptionTiming}.
|
|
212
|
+
*/
|
|
213
|
+
subscription?: SubscriptionTiming;
|
|
214
|
+
/**
|
|
215
|
+
* Retention (ADR-0021). Absent → `persistent`. An `ephemeral` table's whole local cluster is emitted
|
|
216
|
+
* as `TEMP` — no durable trace. See {@link Retention}.
|
|
217
|
+
*/
|
|
218
|
+
retention?: Retention;
|
|
219
|
+
/**
|
|
220
|
+
* Write-mode (ADR-0022). Absent → `optimistic`. A `pessimistic` consistency group is a standing
|
|
221
|
+
* server-authoritative write-unit whose writes flush-route to the authoritative endpoint. See
|
|
222
|
+
* {@link WriteMode}.
|
|
223
|
+
*/
|
|
224
|
+
writeMode?: WriteMode;
|
|
83
225
|
}
|
|
84
226
|
export interface SyncConfigInput<TTables extends Record<string, TableSpecInput> = Record<string, TableSpecInput>> {
|
|
85
227
|
electricUrl: string;
|
|
@@ -95,38 +237,74 @@ export declare function getLocalSyncPrimaryKeyColumns(source: {
|
|
|
95
237
|
clientProjection?: Pick<ClientProjectionSpec, "localPrimaryKey">;
|
|
96
238
|
}): string[];
|
|
97
239
|
export interface RowFilterSpec {
|
|
98
|
-
/** WHERE "column" = '<owner claim>' — ownership-based row filtering. */
|
|
99
|
-
ownership?: {
|
|
100
|
-
column: string;
|
|
101
|
-
/**
|
|
102
|
-
* Dot-path into the JWT claims used as the owner value (e.g.
|
|
103
|
-
* "app_metadata.person_id"). Defaults to "sub". A missing, empty, or
|
|
104
|
-
* non-primitive claim denies all rows, matching the missing-sub behavior.
|
|
105
|
-
*/
|
|
106
|
-
claim?: string;
|
|
107
|
-
};
|
|
108
|
-
/** OR clause for shared content: adds rows owned by sharedUserId. */
|
|
109
|
-
shared?: {
|
|
110
|
-
/** Column used for owner comparison. Defaults to ownership.column. */
|
|
111
|
-
ownerColumn?: string;
|
|
112
|
-
/** If set, also requires "sharedColumn" = true. */
|
|
113
|
-
sharedColumn?: string;
|
|
114
|
-
/** Static UUID or a function that returns a UUID given runtime params. */
|
|
115
|
-
sharedUserId: string | ((params: Record<string, unknown>) => string);
|
|
116
|
-
};
|
|
117
240
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
241
|
+
* The row filter: returns the Electric shape `where` for this request, or `null` to bypass
|
|
242
|
+
* filtering (e.g. admin access). **Prefer returning a Drizzle `SQL` fragment** built from the
|
|
243
|
+
* table's columns: reference each column through {@link c} (a bare, rename-safe identifier) and
|
|
244
|
+
* embed request-derived values directly — they become **bound `$n` params**, never hand-escaped
|
|
245
|
+
* literals. Enum columns must be cast to text (`${c(col)}::text = 'x'`) for Electric's grammar,
|
|
246
|
+
* and subqueries must be self-contained (not correlated), since Electric needs plain column refs.
|
|
247
|
+
*
|
|
248
|
+
* Returning a raw **string** is the escape hatch for a predicate Drizzle can't express. SECURITY:
|
|
249
|
+
* a string is interpolated verbatim into the `where` — it is NOT escaped, so any request-derived
|
|
250
|
+
* value you embed must be escaped/validated (`escapeSqlLiteral`) inside this function, or it is a
|
|
251
|
+
* SQL-injection vector. Reach for the string form only when the Drizzle fragment cannot express it.
|
|
120
252
|
*/
|
|
121
|
-
customWhere?: (claims: JwtClaims, params?: Record<string, unknown>) => string | null;
|
|
253
|
+
customWhere?: (claims: JwtClaims, params?: Record<string, unknown>) => string | SQL | null;
|
|
122
254
|
/** Column projection for the shape URL (e.g. ["id", "source_text"]). */
|
|
123
255
|
columns?: string[];
|
|
256
|
+
/**
|
|
257
|
+
* An opaque version tag for the part of this filter the fingerprint cannot see — the `customWhere`
|
|
258
|
+
* body (you cannot hash a closure; only its *presence* is fingerprinted). Bump this (any new
|
|
259
|
+
* string/number) whenever you change that logic so the fingerprint shifts and the local read cache
|
|
260
|
+
* rebuilds + the shape subscription resets. Leaving it unchanged after a `customWhere`
|
|
261
|
+
* authorization change would silently serve the stale shape.
|
|
262
|
+
*/
|
|
263
|
+
revision?: string | number;
|
|
124
264
|
}
|
|
125
265
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
266
|
+
* The non-parameterized inline `where` for a filter: the string a `customWhere` returns (the raw
|
|
267
|
+
* escape hatch), or `null` when there is no filter or `customWhere` returns a Drizzle `SQL` fragment
|
|
268
|
+
* (which is parameterized by {@link buildRowFilterShape}, not inlined here). Most callers want
|
|
269
|
+
* `buildRowFilterShape` — this is the inline-string view it composes from.
|
|
130
270
|
*/
|
|
131
271
|
export declare function buildRowFilterWhere(filter: RowFilterSpec, claims: JwtClaims | null, params?: Record<string, unknown>): string | null;
|
|
272
|
+
/**
|
|
273
|
+
* A **bare** (table-unqualified) quoted identifier for a Drizzle column — `"workspace_id"`, never
|
|
274
|
+
* `"work_items"."workspace_id"`. Electric's shape `where` grammar requires *plain* column references
|
|
275
|
+
* (it rejects a qualified one with "Expected a plain column reference"), and Drizzle qualifies columns
|
|
276
|
+
* by default — so reference columns through `c()` when authoring a `customWhere` Drizzle fragment. The
|
|
277
|
+
* column object keeps the reference rename-safe and existence-checked at compile time; only the bare
|
|
278
|
+
* name reaches the wire. Subqueries must stay self-contained (not correlated), since bare names then
|
|
279
|
+
* resolve unambiguously to each FROM — a correlated subquery would need qualification Electric rejects.
|
|
280
|
+
*/
|
|
281
|
+
export declare function c(column: AnyColumn): SQL;
|
|
282
|
+
/**
|
|
283
|
+
* The deny-all row filter: a `customWhere` returns this to make **no** rows visible (e.g. an
|
|
284
|
+
* unauthenticated request), the counterpart to returning `null` (which bypasses filtering — all rows
|
|
285
|
+
* visible). It is a Drizzle `SQL` fragment (`false`), so it stays on the typed/parameterized path
|
|
286
|
+
* with the rest of the filter rather than being a hand-written `"1 = 0"` string. `WHERE false`
|
|
287
|
+
* matches nothing; Electric accepts it (verified) exactly as it accepts `1 = 0`.
|
|
288
|
+
*/
|
|
289
|
+
export declare const DENY_ALL: SQL;
|
|
290
|
+
/**
|
|
291
|
+
* The ownership shape `where` — the read-path mirror of an owner-column RLS policy: rows whose owner
|
|
292
|
+
* column equals the caller's subject, {@link DENY_ALL} for an unauthenticated caller. Takes the real
|
|
293
|
+
* Drizzle owner column (bare via {@link c}, rename-safe); the subject rides as a typed interpolation —
|
|
294
|
+
* a bound param through `buildRowFilterShape`, or a drizzle-escaped literal when a proxy renders it
|
|
295
|
+
* inline for a shape URL.
|
|
296
|
+
*/
|
|
297
|
+
export declare function buildOwnershipShapeWhere(ownerColumn: AnyColumn, subject: string | null | undefined): SQL;
|
|
298
|
+
/** The parameterized shape filter the proxy sends to Electric: a `where` and its positional params. */
|
|
299
|
+
export interface RowFilterShape {
|
|
300
|
+
where: string;
|
|
301
|
+
params: string[];
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The shape filter the proxy sends to Electric: the `where` plus its positional `params` (`$1`, `$2`,
|
|
305
|
+
* …). A `customWhere` returning a Drizzle `SQL` fragment is serialized here, so request-derived values
|
|
306
|
+
* become **bound params** — never hand-escaped literals; a string `customWhere` is the raw escape
|
|
307
|
+
* hatch (no params). Returns `null` when there is no filter (all rows visible).
|
|
308
|
+
*/
|
|
309
|
+
export declare function buildRowFilterShape(filter: RowFilterSpec, claims: JwtClaims | null, params?: Record<string, unknown>): RowFilterShape | null;
|
|
132
310
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,SAAS,EAAE,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,CAAC;AAE/D;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,cAAc,GAAG,iBAAiB,GAAG,iBAAiB,CAAC;AAEnE,qGAAqG;AACrG,eAAO,MAAM,iBAAiB,iDAAsF,CAAC;AAErH,0EAA0E;AAC1E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,cAAc,CAExE;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,kBAAkB,GAAG,OAAO,GAAG,MAAM,CAAC;AAElD,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,4BAAqE,CAAC;AAEvG,2DAA2D;AAC3D,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB,CAEhF;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,SAAS,GAAG,YAAY,GAAG,WAAW,CAAC;AAEnD,6EAA6E;AAC7E,eAAO,MAAM,UAAU,sCAAsE,CAAC;AAE9F,kDAAkD;AAClD,wBAAgB,WAAW,CAAC,KAAK,EA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,GAAG,CAExC;AAED;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,EAAE,GAAgB,CAAC;AAExC;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,GAAG,CAExG;AAED,uGAAuG;AACvG,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,SAAS,GAAG,IAAI,EACxB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,cAAc,GAAG,IAAI,CAavB"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type AnyPgTable } from "drizzle-orm/pg-core";
|
|
2
|
+
import { type SyncTableEntry } from "./registry";
|
|
3
|
+
/**
|
|
4
|
+
* The Convergence model (ADR-0011): the single owner of *how* local optimistic state converges to
|
|
5
|
+
* server state. It owns the shared barrier predicate, the per-table sync-state **view** generator,
|
|
6
|
+
* and the event-model spec below. Read sync (the Sync applier) and write sync (the mutation runtime)
|
|
7
|
+
* remain the two edges with their own transaction paths, but neither independently decides whether an
|
|
8
|
+
* entity is resolved — this module is the one resolution authority.
|
|
9
|
+
*
|
|
10
|
+
* Distinct from the convergence *driver* (`convergence.ts`, ADR-0005), which schedules *when* to
|
|
11
|
+
* converge. This module defines *how* an entity resolves.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* The Convergence barrier predicate (ADR-0010): an acked create/update is resolved only once the
|
|
15
|
+
* synced echo's Server version has reached the write's acked version. Emitted identically by the
|
|
16
|
+
* reconcile trigger (schema.ts), `reconcileTable` (mutation.ts), **and** the per-table sync-state
|
|
17
|
+
* view (decision 4) — one rule, three consumers, no drift (ADR-0004).
|
|
18
|
+
*
|
|
19
|
+
* `syncedAlias` is the synced-side reference (a table alias, or `NEW` inside the trigger);
|
|
20
|
+
* `journalAlias` qualifies the journal's `server_updated_at_us` (omit it where the journal is the
|
|
21
|
+
* unaliased target of the `DELETE`).
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildOverlayResolutionBarrier<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>, options: {
|
|
24
|
+
syncedAlias: string;
|
|
25
|
+
journalAlias?: string;
|
|
26
|
+
}): string;
|
|
27
|
+
/**
|
|
28
|
+
* The qualified object names the sync-state view is generated over. The synced read cache is
|
|
29
|
+
* droppable (ADR-0006); overlay + journal are the authority. The view joins all three but stores
|
|
30
|
+
* nothing — it is a derived projection (ADR-0011 decision 2).
|
|
31
|
+
*/
|
|
32
|
+
export interface SyncStateViewProjection {
|
|
33
|
+
/** Qualified synced read-cache table. */
|
|
34
|
+
syncedTable: string;
|
|
35
|
+
/** Qualified overlay (optimistic intent) table. */
|
|
36
|
+
overlayTable: string;
|
|
37
|
+
/** Qualified mutation-journal table. */
|
|
38
|
+
journalTable: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Generate the per-writable-table `<table>_sync_state` **view** (ADR-0011 decision 2): a derived
|
|
42
|
+
* projection — never a stored copy — over synced + overlay + journal that answers "what is this
|
|
43
|
+
* entity's convergence state?" with one authoritative, queryable row per entity that has local
|
|
44
|
+
* activity (an overlay or journal row). Keyed on the **real PK columns**, never a generic
|
|
45
|
+
* `entity_key_json` (decision 2).
|
|
46
|
+
*
|
|
47
|
+
* Columns:
|
|
48
|
+
* - `observed_server_version` — the synced row's Server version (NULL when no echo has landed).
|
|
49
|
+
* - `acked_server_version` — the highest Server version the server assigned to our acked writes.
|
|
50
|
+
* - `pending_count` — journal rows still owed to the server (pending/sending/failed).
|
|
51
|
+
* - `has_acked_unobserved_write` — an acked create/update whose echo has not yet caught up. Derived
|
|
52
|
+
* from the **same** {@link buildOverlayResolutionBarrier} predicate the resolver uses (decision 4),
|
|
53
|
+
* so what the UI shows can never drift from what the resolver does.
|
|
54
|
+
* - `local_delete_pending` — an optimistic delete is staged in the overlay.
|
|
55
|
+
* - `conflict_state` — the reason a `conflicted` (stale, reject-if-stale) write was declined, or NULL
|
|
56
|
+
* when the entity has no conflicted mutation (ADR-0015). Surfaced from the journal's
|
|
57
|
+
* `conflict_reason`, scoped to `status = 'conflicted'` so a stale failure reason never leaks in.
|
|
58
|
+
* - `quarantined_count` — terminal local writes the server permanently rejected (ADR-0006): a poison
|
|
59
|
+
* mutation named in a batch rejection, or one that exhausted the attempt cap. Quarantine blocks
|
|
60
|
+
* sync and *keeps* the optimistic overlay, so without this the view would show an entity with
|
|
61
|
+
* `pending_count = 0` and `conflict_state = NULL` yet still carrying un-converged local intent.
|
|
62
|
+
* - `quarantine_state` — the `last_error` of a quarantined write (the rejection reason), or NULL when
|
|
63
|
+
* the entity has none. The reason counterpart to `quarantined_count`, mirroring `conflict_state`.
|
|
64
|
+
*
|
|
65
|
+
* Note `pending_count` is the *retryable* owed set (pending/sending/failed); a quarantined write is
|
|
66
|
+
* terminal, counted separately so a blocked write is never mistaken for one still in flight.
|
|
67
|
+
*
|
|
68
|
+
* The Read model stays lean (it already carries `overlay_kind`); an app that wants per-row
|
|
69
|
+
* convergence status joins this view on the PK.
|
|
70
|
+
*/
|
|
71
|
+
export declare function buildSyncStateView<TTable extends AnyPgTable>(entry: SyncTableEntry<TTable>, projection: SyncStateViewProjection): string;
|
|
72
|
+
/**
|
|
73
|
+
* The event model the Convergence model implements (ADR-0011). The per-entity convergence state is
|
|
74
|
+
* **derived** from synced + overlay + journal — never a mutated row — so this table is the spec the
|
|
75
|
+
* derivation answers to, not a state machine that is stepped. Exported so a test can assert the
|
|
76
|
+
* implemented derivation covers every event.
|
|
77
|
+
*/
|
|
78
|
+
export declare const CONVERGENCE_EVENTS: readonly [{
|
|
79
|
+
readonly event: "local create/update/delete enqueued";
|
|
80
|
+
readonly effect: "overlay upserted; a journal mutation appended (records base server version for ADR-0015)";
|
|
81
|
+
}, {
|
|
82
|
+
readonly event: "mutation sent";
|
|
83
|
+
readonly effect: "journal row → sending";
|
|
84
|
+
}, {
|
|
85
|
+
readonly event: "mutation acked";
|
|
86
|
+
readonly effect: "journal row → acked, server_updated_at_us stamped; entity shows acked_unobserved until the echo catches up";
|
|
87
|
+
}, {
|
|
88
|
+
readonly event: "Electric insert/update observed";
|
|
89
|
+
readonly effect: "synced row applied; the barrier predicate runs; resolved entities clear overlay + acked journal";
|
|
90
|
+
}, {
|
|
91
|
+
readonly event: "Electric delete observed";
|
|
92
|
+
readonly effect: "resolved by synced-row absence (deletes carry no Server version — ADR-0010)";
|
|
93
|
+
}, {
|
|
94
|
+
readonly event: "resolution";
|
|
95
|
+
readonly effect: "clear overlay/journal only through the shared barrier predicate (decision 4)";
|
|
96
|
+
}, {
|
|
97
|
+
readonly event: "conflict detected";
|
|
98
|
+
readonly effect: "conflict_state recorded on the journal row (ADR-0015); surfaced in the view";
|
|
99
|
+
}, {
|
|
100
|
+
readonly event: "mutation quarantined";
|
|
101
|
+
readonly effect: "journal row → quarantined (terminal, ADR-0006), overlay kept; surfaced as quarantined_count/quarantine_state";
|
|
102
|
+
}, {
|
|
103
|
+
readonly event: "shape must-refetch";
|
|
104
|
+
readonly effect: "subscription reset + re-stream; affected entity state re-derives from the fresh synced rows";
|
|
105
|
+
}];
|
|
106
|
+
//# sourceMappingURL=convergence-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convergence-model.d.ts","sourceRoot":"","sources":["../src/convergence-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,qBAAqB,CAAC;AAEtE,OAAO,EAAkC,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAGjF;;;;;;;;;GASG;AAEH;;;;;;;;;GASG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,SAAS,UAAU,EACrE,KAAK,EAAE,cAAc,CAAC,MAAM,CAAC,EAC7B,OAAO,EAAE;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD,MAAM,CAWR;AAED;;;;GAIG;AACH,MAAM,WAAW,uBAAuB;IACtC,yCAAyC;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,mDAAmD;IACnD,YAAY,EAAE,MAAM,CAAC;IACrB,wCAAwC;IACxC,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,UAAU,EAC1D,KAAK,EAAE,cAAc,CAAC,MAAM,CAAC,EAC7B,UAAU,EAAE,uBAAuB,GAClC,MAAM,CA6DR;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB;aAE3B,KAAK,EAAE,qCAAqC;aAC5C,MAAM,EAAE,0FAA0F;;aAElG,KAAK,EAAE,eAAe;aAAE,MAAM,EAAE,uBAAuB;;aAEvD,KAAK,EAAE,gBAAgB;aACvB,MAAM,EACJ,4GAA4G;;aAG9G,KAAK,EAAE,iCAAiC;aACxC,MAAM,EAAE,iGAAiG;;aAGzG,KAAK,EAAE,0BAA0B;aACjC,MAAM,EAAE,6EAA6E;;aAErF,KAAK,EAAE,YAAY;aAAE,MAAM,EAAE,8EAA8E;;aAC3G,KAAK,EAAE,mBAAmB;aAAE,MAAM,EAAE,6EAA6E;;aAEjH,KAAK,EAAE,sBAAsB;aAC7B,MAAM,EACJ,8GAA8G;;aAGhH,KAAK,EAAE,oBAAoB;aAC3B,MAAM,EAAE,6FAA6F;EAE/F,CAAC"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { SyncTableEntry, SyncTableRegistry } from "./registry";
|
|
2
|
+
/**
|
|
3
|
+
* The registry fingerprint (ADR-0004): a stable, order-independent description of
|
|
4
|
+
* the shape-relevant registry metadata, plus a hash of it.
|
|
5
|
+
*
|
|
6
|
+
* This is the single source of "has the shape changed" — consumed as the local-DB
|
|
7
|
+
* version key and as the basis of the registry-diff gate (ADR-0006). Function *bodies*
|
|
8
|
+
* (`rowTransform`, `customWhere`) cannot be fingerprinted and are excluded — but their
|
|
9
|
+
* *presence* and the surrounding **static** filter structure (the projected columns)
|
|
10
|
+
* participate. For the invisible *logic* itself, a consumer-bumped `rowFilter.revision` is
|
|
11
|
+
* folded in: changing it is how a `customWhere` authorization change is forced to shift the
|
|
12
|
+
* fingerprint (and so rebuild the cache + reset the subscription).
|
|
13
|
+
*/
|
|
14
|
+
export interface CanonicalColumn {
|
|
15
|
+
name: string;
|
|
16
|
+
type: string;
|
|
17
|
+
notNull: boolean;
|
|
18
|
+
hasDefault: boolean;
|
|
19
|
+
primary: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface CanonicalTable {
|
|
22
|
+
key: string;
|
|
23
|
+
mode: string;
|
|
24
|
+
primaryKey: string[];
|
|
25
|
+
localPrimaryKey: string[] | null;
|
|
26
|
+
columns: CanonicalColumn[];
|
|
27
|
+
projection: {
|
|
28
|
+
syncedTable: string | null;
|
|
29
|
+
overlayTable: string | null;
|
|
30
|
+
journalTable: string | null;
|
|
31
|
+
omitColumns: string[];
|
|
32
|
+
} | null;
|
|
33
|
+
shape: {
|
|
34
|
+
tableName: string;
|
|
35
|
+
shapeKey: string;
|
|
36
|
+
electricTable: string | null;
|
|
37
|
+
rowFilter: CanonicalRowFilter | null;
|
|
38
|
+
} | null;
|
|
39
|
+
managedFields: Array<{
|
|
40
|
+
field: string;
|
|
41
|
+
strategy: string;
|
|
42
|
+
applyOn: string[];
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Consistency group (ADR-0009 decision 2). Part of the fingerprint because it decides which
|
|
46
|
+
* subscription-state row a table persists under: moving a table between groups must shift the
|
|
47
|
+
* fingerprint (forcing a cache rebuild + subscription reset) and surface in the diff gate. `null`
|
|
48
|
+
* = the default singleton group.
|
|
49
|
+
*/
|
|
50
|
+
consistencyGroup: string | null;
|
|
51
|
+
/**
|
|
52
|
+
* Retention (ADR-0021). Part of the fingerprint because it changes the cluster DDL — an `ephemeral`
|
|
53
|
+
* table's whole cluster is emitted as `TEMP`/`pg_temp` — so flipping persistent↔ephemeral must force a
|
|
54
|
+
* cache rebuild + subscription reset. (Subscription timing is NOT included: it is pure runtime
|
|
55
|
+
* orchestration over identical tables and needs no rebuild.)
|
|
56
|
+
*/
|
|
57
|
+
retention: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The static, fingerprint-able structure of a row filter. A changed projection shifts the
|
|
61
|
+
* fingerprint, so the local store rebuilds and the diff gate flags it. `customWhere`'s body is
|
|
62
|
+
* invisible — only its presence (`hasCustomWhere`) is recorded — so a `customWhere` *logic* change
|
|
63
|
+
* is surfaced only by bumping `revision`.
|
|
64
|
+
*/
|
|
65
|
+
export interface CanonicalRowFilter {
|
|
66
|
+
hasCustomWhere: boolean;
|
|
67
|
+
columns: string[] | null;
|
|
68
|
+
/**
|
|
69
|
+
* The consumer-supplied version tag for the non-fingerprintable filter logic (the `customWhere`
|
|
70
|
+
* body). Changing it shifts the fingerprint, which is the only way a `customWhere` *logic* change
|
|
71
|
+
* forces a cache + subscription reset.
|
|
72
|
+
*/
|
|
73
|
+
revision: string | null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The canonical, order-independent shape of a registry. Tables are sorted by key so
|
|
77
|
+
* declaration order never affects the result.
|
|
78
|
+
*/
|
|
79
|
+
export declare function canonicalizeRegistry(registry: SyncTableRegistry): CanonicalTable[];
|
|
80
|
+
/** A stable string serialization of the canonical registry shape. */
|
|
81
|
+
export declare function canonicalRegistryString(registry: SyncTableRegistry): string;
|
|
82
|
+
/**
|
|
83
|
+
* A stable fingerprint (hex) of the registry's shape. Identical shapes — even with
|
|
84
|
+
* tables declared in a different order — produce the same fingerprint; any
|
|
85
|
+
* structural change produces a different one.
|
|
86
|
+
*/
|
|
87
|
+
export declare function fingerprintRegistry(registry: SyncTableRegistry): string;
|
|
88
|
+
/**
|
|
89
|
+
* The **read contract** of a single sync table: the subset of its canonical shape that decides what
|
|
90
|
+
* data streams down and how a row is identified and filtered — synced-table name, columns, primary key
|
|
91
|
+
* (and any local-PK override), column omission, and the shape (electric table + row filter). It is the
|
|
92
|
+
* stable identity a writable entry shares with its {@link asReadonly} projection.
|
|
93
|
+
*
|
|
94
|
+
* Deliberately EXCLUDES the two axes a per-client projection may legitimately differ on:
|
|
95
|
+
* - **write capability** — `mode`, the overlay/journal projection, `managedFields`, `conflictPolicy`,
|
|
96
|
+
* `writeMode` (one client writes the table, another only reads it);
|
|
97
|
+
* - **lifecycle orchestration** — `consistencyGroup`, `subscription`, `retention` (a client may
|
|
98
|
+
* eager- or lazy-load, or group differently, without changing the data it sees).
|
|
99
|
+
*
|
|
100
|
+
* What it pins is the data itself: two registries that present "the same" logical table to different
|
|
101
|
+
* clients must agree here, or those clients are silently seeing different rows/columns. As with the
|
|
102
|
+
* full registry fingerprint, the `customWhere` *body* is invisible — only its presence and the
|
|
103
|
+
* consumer-bumped {@link RowFilterSpec.revision} participate, so bump `revision` to force a divergence
|
|
104
|
+
* a logic-only change would otherwise hide.
|
|
105
|
+
*/
|
|
106
|
+
export interface CanonicalReadContract {
|
|
107
|
+
syncedTable: string;
|
|
108
|
+
primaryKey: string[];
|
|
109
|
+
localPrimaryKey: string[] | null;
|
|
110
|
+
columns: CanonicalColumn[];
|
|
111
|
+
omitColumns: string[];
|
|
112
|
+
shape: {
|
|
113
|
+
tableName: string;
|
|
114
|
+
shapeKey: string;
|
|
115
|
+
electricTable: string | null;
|
|
116
|
+
rowFilter: CanonicalRowFilter | null;
|
|
117
|
+
} | null;
|
|
118
|
+
}
|
|
119
|
+
/** The canonical {@link CanonicalReadContract} of a sync table entry (see the interface for what it omits). */
|
|
120
|
+
export declare function canonicalizeReadContract(entry: SyncTableEntry): CanonicalReadContract;
|
|
121
|
+
/** A stable string serialization of a table's {@link CanonicalReadContract}. */
|
|
122
|
+
export declare function canonicalReadContractString(entry: SyncTableEntry): string;
|
|
123
|
+
/**
|
|
124
|
+
* A stable fingerprint (hex) of a table's {@link CanonicalReadContract}. Equal for a writable entry and
|
|
125
|
+
* its {@link asReadonly} projection; the basis of the projection-consistency invariant
|
|
126
|
+
* (`assertReadContractPreserved`).
|
|
127
|
+
*/
|
|
128
|
+
export declare function fingerprintReadContract(entry: SyncTableEntry): string;
|
|
129
|
+
/**
|
|
130
|
+
* FNV-1a over UTF-8 bytes, returned as 16 hex chars. Pure and dependency-free so it runs
|
|
131
|
+
* identically in the browser and in Bun (no crypto import). A fingerprint, not a security
|
|
132
|
+
* primitive — used both for the registry shape fingerprint (ADR-0004) and for the apply-function
|
|
133
|
+
* DDL fingerprint embedded in the generated migration (ADR-0018).
|
|
134
|
+
*/
|
|
135
|
+
export declare function hashString(input: string): string;
|
|
136
|
+
//# sourceMappingURL=fingerprint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fingerprint.d.ts","sourceRoot":"","sources":["../src/fingerprint.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;;;;;;;;GAWG;AAEH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,eAAe,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IACjC,OAAO,EAAE,eAAe,EAAE,CAAC;IAC3B,UAAU,EAAE;QACV,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;QAC3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AC;IAC3B,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,KAAK,EAAE;QACL,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;QAC7B,SAAS,EAAE,kBAAkB,GAAG,IAAI,CAAC;KACtC,GAAG,IAAI,CAAC;CACV;AAED,+GAA+G;AAC/G,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,cAAc,GAAG,qBAAqB,CAoBrF;AAED,gFAAgF;AAChF,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,cAAc,GAAG,MAAM,CAEzE;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,cAAc,GAAG,MAAM,CAErE;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAShD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
export * from "./apply-strategy";
|
|
1
2
|
export * from "./common";
|
|
2
3
|
export * from "./config";
|
|
4
|
+
export * from "./convergence-model";
|
|
5
|
+
export * from "./fingerprint";
|
|
3
6
|
export * from "./registry";
|
|
7
|
+
export * from "./registry-diff";
|
|
4
8
|
export * from "./mutation";
|
|
9
|
+
export * from "./projection";
|
|
5
10
|
export * from "./runtime";
|
|
11
|
+
export * from "./sql-defaults";
|
|
12
|
+
export * from "./sql-identifier";
|
|
6
13
|
export * from "./supabase-rls";
|
|
7
14
|
//# sourceMappingURL=index.d.ts.map
|