@pgxsinkit/client 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 +11 -0
- package/dist/convergence.d.ts +76 -0
- package/dist/convergence.d.ts.map +1 -0
- package/dist/debug.d.ts +34 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/index.d.ts +421 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7214 -2234
- package/dist/index.js.map +74 -44
- package/dist/lazy-guard.d.ts +58 -0
- package/dist/lazy-guard.d.ts.map +1 -0
- package/dist/local-store.d.ts +56 -0
- package/dist/local-store.d.ts.map +1 -0
- package/dist/local-tables.d.ts +707 -0
- package/dist/local-tables.d.ts.map +1 -0
- package/dist/mutation-state.d.ts +62 -0
- package/dist/mutation-state.d.ts.map +1 -0
- package/dist/mutation.d.ts +117 -4
- package/dist/mutation.d.ts.map +1 -1
- package/dist/schema.d.ts +49 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/shape-sync.d.ts +180 -0
- package/dist/shape-sync.d.ts.map +1 -0
- package/dist/sync/apply.d.ts +51 -0
- package/dist/sync/apply.d.ts.map +1 -0
- package/dist/sync/copy.d.ts +43 -0
- package/dist/sync/copy.d.ts.map +1 -0
- package/dist/sync/drizzle-executor.d.ts +5 -0
- package/dist/sync/drizzle-executor.d.ts.map +1 -0
- package/dist/sync/index.d.ts +32 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/metadata-tables.d.ts +131 -0
- package/dist/sync/metadata-tables.d.ts.map +1 -0
- package/dist/sync/nudge.d.ts +49 -0
- package/dist/sync/nudge.d.ts.map +1 -0
- package/dist/sync/shape-inbox.d.ts +215 -0
- package/dist/sync/shape-inbox.d.ts.map +1 -0
- package/dist/sync/subscription-state.d.ts +39 -0
- package/dist/sync/subscription-state.d.ts.map +1 -0
- package/dist/sync/tags.d.ts +130 -0
- package/dist/sync/tags.d.ts.map +1 -0
- package/dist/sync/types.d.ts +118 -0
- package/dist/sync/types.d.ts.map +1 -0
- package/dist/sync-auth.d.ts +53 -0
- package/dist/sync-auth.d.ts.map +1 -0
- package/dist/worker/attach-sync-client.d.ts +75 -0
- package/dist/worker/attach-sync-client.d.ts.map +1 -0
- package/dist/worker/define-sync-worker.d.ts +75 -0
- package/dist/worker/define-sync-worker.d.ts.map +1 -0
- package/dist/worker/live-diff.d.ts +44 -0
- package/dist/worker/live-diff.d.ts.map +1 -0
- package/dist/worker/protocol.d.ts +260 -0
- package/dist/worker/protocol.d.ts.map +1 -0
- package/dist/worker/token-cache.d.ts +26 -0
- package/dist/worker/token-cache.d.ts.map +1 -0
- package/oxlint-plugin.js +143 -0
- package/package.json +15 -7
- package/skills/core/SKILL.md +197 -0
- package/skills/operating/SKILL.md +243 -0
- package/src/convergence.ts +197 -0
- package/src/debug.ts +151 -0
- package/src/index.ts +1104 -44
- package/src/lazy-guard.ts +121 -0
- package/src/local-store.ts +193 -0
- package/src/local-tables.ts +546 -0
- package/src/mutation-state.ts +90 -0
- package/src/mutation.ts +1868 -562
- package/src/schema.ts +352 -27
- package/src/shape-sync.ts +477 -0
- package/src/sync/apply.ts +530 -0
- package/src/sync/copy.ts +206 -0
- package/src/sync/drizzle-executor.ts +48 -0
- package/src/sync/index.ts +1076 -0
- package/src/sync/metadata-tables.ts +67 -0
- package/src/sync/nudge.ts +110 -0
- package/src/sync/shape-inbox.ts +566 -0
- package/src/sync/subscription-state.ts +135 -0
- package/src/sync/tags.ts +365 -0
- package/src/sync/types.ts +132 -0
- package/src/sync-auth.ts +117 -0
- package/src/worker/attach-sync-client.ts +558 -0
- package/src/worker/define-sync-worker.ts +596 -0
- package/src/worker/live-diff.ts +140 -0
- package/src/worker/protocol.ts +325 -0
- package/src/worker/token-cache.ts +84 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-tables.d.ts","sourceRoot":"","sources":["../src/local-tables.ts"],"names":[],"mappings":"AAEA,OAAO,EAYL,KA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;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,OAAO,EAAE,CAAA;KAAE,CAAC,CAAC;CACxE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,SAAS,iBAAiB,EAAE,IAAI,SAAS,MAAM,GAAG,MAAM,SAAS,EAC3G,QAAQ,EAAE,SAAS,EACnB,QAAQ,EAAE,IAAI,GACb,WAAW,CAkBb;AAED,+FAA+F;AAC/F,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,cAAc,CAY7E"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mutation-journal state machine (ADR-0005, extended by ADR-0006).
|
|
3
|
+
*
|
|
4
|
+
* The runtime is large; previously the legal status transitions were implicit,
|
|
5
|
+
* scattered across SQL writes. This is the one named place that defines them, so the
|
|
6
|
+
* rules are testable and a future change extends a single table. ADR-0006 splits the
|
|
7
|
+
* old single `failed` into transient (retryable `failed`) and permanent (`quarantined`).
|
|
8
|
+
*/
|
|
9
|
+
export type MutationStatus = "pending" | "sending" | "acked" | "failed" | "quarantined" | "conflicted" | "rejected";
|
|
10
|
+
/**
|
|
11
|
+
* Legal transitions. `acked` is terminal at the journal level — it is cleared by
|
|
12
|
+
* reconciliation once the synced echo reaches the acknowledged server timestamp, not
|
|
13
|
+
* by another status transition. `quarantined` is terminal too: the server will never
|
|
14
|
+
* accept the mutation as-is, so it is surfaced (callback + diagnostics), never retried.
|
|
15
|
+
* `conflicted` (ADR-0015) is likewise terminal but a *different* outcome: the write was
|
|
16
|
+
* well-formed but **stale** (an external write interleaved) and the table's `reject-if-stale`
|
|
17
|
+
* policy declined it. The optimistic Overlay is KEPT (the user's edit is not lost) and the
|
|
18
|
+
* conflict is surfaced via `onConflict`; resolution is an ordinary NEW mutation, and `discard`
|
|
19
|
+
* clears the overlay + this entry. It is never retried as-is (the base would still be stale).
|
|
20
|
+
* `rejected` (ADR-0022) is terminal as well, and the inverse of `conflicted` on disposition: the
|
|
21
|
+
* authoritative endpoint declined the whole pessimistic write-**unit** for a business reason (a
|
|
22
|
+
* capacity/quota/uniqueness rule the client could not evaluate locally), so the optimistic Overlay
|
|
23
|
+
* is **auto-discarded** for every member of the unit and the typed reason is surfaced via `onReject`.
|
|
24
|
+
* Never retried (the server's answer is authoritative).
|
|
25
|
+
*
|
|
26
|
+
* - `pending -> sending` a flush claims the row and posts it.
|
|
27
|
+
* - `sending -> acked` the server acknowledged the mutation.
|
|
28
|
+
* - `sending -> failed` transient transport/server error (network, 5xx, …).
|
|
29
|
+
* - `sending -> quarantined` structural 4xx rejection the server will never accept.
|
|
30
|
+
* - `sending -> conflicted` a stale write the reject-if-stale policy declined (ADR-0015).
|
|
31
|
+
* - `sending -> rejected` a business rejection from the authoritative endpoint (ADR-0022).
|
|
32
|
+
* - `sending -> pending` recoverSending re-queues an in-flight row on startup.
|
|
33
|
+
* - `failed -> pending` retryFailed re-queues a failed row.
|
|
34
|
+
* - `failed -> quarantined` the hard max-attempts cap is reached; stop retrying.
|
|
35
|
+
*/
|
|
36
|
+
export declare const MUTATION_TRANSITIONS: {
|
|
37
|
+
readonly pending: readonly ["sending"];
|
|
38
|
+
readonly sending: readonly ["acked", "failed", "quarantined", "conflicted", "rejected", "pending"];
|
|
39
|
+
readonly failed: readonly ["pending", "quarantined"];
|
|
40
|
+
readonly quarantined: readonly [];
|
|
41
|
+
readonly conflicted: readonly [];
|
|
42
|
+
readonly rejected: readonly [];
|
|
43
|
+
readonly acked: readonly [];
|
|
44
|
+
};
|
|
45
|
+
export declare function isValidMutationTransition(from: MutationStatus, to: MutationStatus): boolean;
|
|
46
|
+
export declare function assertValidMutationTransition(from: MutationStatus, to: MutationStatus): void;
|
|
47
|
+
/**
|
|
48
|
+
* The hard cap on send attempts (ADR-0005 congestion policy). A mutation that has
|
|
49
|
+
* failed this many times — even with transient-looking errors — is given up on and
|
|
50
|
+
* quarantined, so a permanently-unreachable server never produces an unbounded retry
|
|
51
|
+
* loop. The consumer can raise/lower it via `createSyncClient({ maxMutationAttempts })`.
|
|
52
|
+
*/
|
|
53
|
+
export declare const DEFAULT_MAX_MUTATION_ATTEMPTS = 10;
|
|
54
|
+
/**
|
|
55
|
+
* Classify a flush failure into its durable journal state (ADR-0006 decision 4):
|
|
56
|
+
* - `quarantined` (terminal): a structural 4xx rejection — retrying is pointless, so it
|
|
57
|
+
* is surfaced rather than retry-looped (the silent-data-loss case the runtime *can*
|
|
58
|
+
* catch at send time).
|
|
59
|
+
* - `failed` (retryable): network/transport (no status), any 5xx, or a transient 4xx.
|
|
60
|
+
*/
|
|
61
|
+
export declare function classifyFailureStatus(httpStatus: number | null | undefined): "failed" | "quarantined";
|
|
62
|
+
//# sourceMappingURL=mutation-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutation-state.d.ts","sourceRoot":"","sources":["../src/mutation-state.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,QAAQ,GAAG,aAAa,GAAG,YAAY,GAAG,UAAU,CAAC;AAEpH;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;CAQqC,CAAC;AAEvE,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,cAAc,GAAG,OAAO,CAE3F;AAED,wBAAgB,6BAA6B,CAAC,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,cAAc,GAAG,IAAI,CAI5F;AAED;;;;;GAKG;AACH,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAShD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,QAAQ,GAAG,aAAa,CAUrG"}
|
package/dist/mutation.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import type { MutationDiagnostics, SyncTableCreateInput, SyncTableName, SyncTableRecord, SyncTableRegistry, SyncTableUpdateInput } from "@pgxsinkit/contracts";
|
|
2
|
-
|
|
1
|
+
import type { MutationAck, MutationDiagnostics, SyncTableCreateInput, SyncTableName, SyncTableRecord, SyncTableRegistry, SyncTableUpdateInput, WriteMode } from "@pgxsinkit/contracts";
|
|
2
|
+
import { type MutationStatus } from "./mutation-state";
|
|
3
|
+
export type { MutationStatus } from "./mutation-state";
|
|
3
4
|
export type MutationKind = "create" | "update" | "delete";
|
|
5
|
+
/**
|
|
6
|
+
* The dynamic write-unit tag (ADR-0022 §2) a `transaction({ mode })` block stamps onto the mutations
|
|
7
|
+
* authored within it: a shared `id` grouping the co-committed mutations, and the unit's `mode`. Persisted
|
|
8
|
+
* on each journal row (`write_unit` / `write_mode`); absent for the default path, where the flusher derives
|
|
9
|
+
* mode + unit from the table's static consistency group.
|
|
10
|
+
*/
|
|
11
|
+
export interface WriteUnit {
|
|
12
|
+
id: string;
|
|
13
|
+
mode: WriteMode;
|
|
14
|
+
}
|
|
4
15
|
export type MutationBatchItem<TRegistry extends SyncTableRegistry> = {
|
|
5
16
|
[TKey in SyncTableName<TRegistry>]: {
|
|
6
17
|
table: TKey;
|
|
@@ -11,6 +22,13 @@ export type MutationBatchItem<TRegistry extends SyncTableRegistry> = {
|
|
|
11
22
|
kind: "update";
|
|
12
23
|
entityKey: Record<string, string>;
|
|
13
24
|
patch: SyncTableUpdateInput<TRegistry, TKey>;
|
|
25
|
+
/**
|
|
26
|
+
* A **blind** update (ADR-0022 addendum): plan a journal row ONLY — no local base row is required
|
|
27
|
+
* and no optimistic overlay is written. Set exclusively by the transaction handle's `updateBlind`;
|
|
28
|
+
* valid only when the write routes pessimistically (a `pessimistic` unit, or a statically-pessimistic
|
|
29
|
+
* table), and rejected at enqueue otherwise. Absent/false for an ordinary update.
|
|
30
|
+
*/
|
|
31
|
+
blind?: boolean;
|
|
14
32
|
} | {
|
|
15
33
|
table: TKey;
|
|
16
34
|
kind: "delete";
|
|
@@ -32,6 +50,8 @@ export interface MutationDetail {
|
|
|
32
50
|
nextRetryAtUs: string | null;
|
|
33
51
|
serverUpdatedAtUs: string | null;
|
|
34
52
|
updatedAtUs: string;
|
|
53
|
+
/** Registry fingerprint (ADR-0004) the mutation was authored under, or null if unstamped. */
|
|
54
|
+
registryVersion: string | null;
|
|
35
55
|
}
|
|
36
56
|
export interface MutationDb {
|
|
37
57
|
exec: (sql: string) => Promise<unknown>;
|
|
@@ -44,13 +64,85 @@ export interface CreateMutationRuntimeOptions<TRegistry extends SyncTableRegistr
|
|
|
44
64
|
registry: TRegistry;
|
|
45
65
|
writeUrl: string;
|
|
46
66
|
getAuthToken?: () => Promise<string | undefined>;
|
|
67
|
+
/** Static headers added to every write request (e.g. a Supabase Cloud `apikey`); see {@link CreateSyncClientOptions.requestHeaders}. */
|
|
68
|
+
requestHeaders?: Record<string, string>;
|
|
69
|
+
/**
|
|
70
|
+
* Registry fingerprint (ADR-0004) stamped onto each enqueued mutation as its
|
|
71
|
+
* `registry_version`, so a version-boundary crossing is known before sending (ADR-0006).
|
|
72
|
+
*/
|
|
73
|
+
registryVersion?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Hard cap on send attempts before a still-failing mutation is quarantined
|
|
76
|
+
* (ADR-0005 congestion policy). Defaults to {@link DEFAULT_MAX_MUTATION_ATTEMPTS}.
|
|
77
|
+
*/
|
|
78
|
+
maxMutationAttempts?: number;
|
|
79
|
+
/**
|
|
80
|
+
* Max mutations drained per flush HTTP request; a flush loops slices until the journal is empty.
|
|
81
|
+
* Defaults to {@link DEFAULT_FLUSH_BATCH_SIZE}. Primarily a test seam (a small value exercises the
|
|
82
|
+
* multi-slice drain path without enqueueing hundreds of mutations).
|
|
83
|
+
*/
|
|
84
|
+
flushBatchSize?: number;
|
|
85
|
+
/**
|
|
86
|
+
* Invoked after a flush whenever mutations transition to `quarantined` (terminal,
|
|
87
|
+
* permanently rejected). Receives the newly-quarantined details so the app can surface
|
|
88
|
+
* them, then either re-author + resubmit or roll back via {@link MutationRuntime.discardQuarantined}
|
|
89
|
+
* (clears the kept overlay + quarantined journal rows, so the entity accepts new mutations again).
|
|
90
|
+
* The library never silently drops these (ADR-0006 decision 4).
|
|
91
|
+
*/
|
|
92
|
+
onQuarantine?: (quarantined: MutationDetail[]) => void | Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Invoked after a flush whenever mutations transition to `conflicted` — a stale write the
|
|
95
|
+
* server declined under the `reject-if-stale` Conflict policy (ADR-0015). Distinct from
|
|
96
|
+
* `onQuarantine` (structural rejection): the optimistic Overlay is KEPT, so the app surfaces a
|
|
97
|
+
* resolution/diff UI and resolves each as a new write (or `discardConflict`s it). Never silent.
|
|
98
|
+
*/
|
|
99
|
+
onConflict?: (conflicted: MutationDetail[]) => void | Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Invoked after an authoritative (pessimistic) flush whenever a write-unit was `rejected` (ADR-0022) —
|
|
102
|
+
* a business decline the client could not evaluate locally (capacity/quota/uniqueness). The inverse of
|
|
103
|
+
* `onConflict`: the optimistic Overlay was **auto-discarded** for every member of the unit, so the app
|
|
104
|
+
* surfaces the typed reason (e.g. "full") rather than a resolve/diff UI. Never silent.
|
|
105
|
+
*/
|
|
106
|
+
onReject?: (rejected: MutationDetail[]) => void | Promise<void>;
|
|
47
107
|
}
|
|
48
108
|
export interface MutationRuntime<TRegistry extends SyncTableRegistry> {
|
|
109
|
+
/** The registry fingerprint (ADR-0004) this runtime stamps onto enqueued mutations. */
|
|
110
|
+
registryVersion: string;
|
|
49
111
|
create: <TKey extends SyncTableName<TRegistry>>(table: TKey, input: SyncTableCreateInput<TRegistry, TKey>) => Promise<void>;
|
|
50
112
|
update: <TKey extends SyncTableName<TRegistry>>(table: TKey, entityKey: Record<string, string>, patch: SyncTableUpdateInput<TRegistry, TKey>) => Promise<void>;
|
|
51
113
|
delete: <TKey extends SyncTableName<TRegistry>>(table: TKey, entityKey: Record<string, string>) => Promise<void>;
|
|
52
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Enqueue an atomic batch of mutations. An optional {@link WriteUnit} tags every row of the batch as one
|
|
116
|
+
* dynamic write-unit (ADR-0022 §2) — the `transaction({ mode })` block passes it; the per-table
|
|
117
|
+
* `create`/`update`/`delete` helpers do not (their write-mode comes from the static group at flush).
|
|
118
|
+
*/
|
|
119
|
+
batch: (items: ReadonlyArray<MutationBatchItem<TRegistry>>, unit?: WriteUnit) => Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Discard a `conflicted` entity (ADR-0015): clear its conflicted journal entries and the kept
|
|
122
|
+
* optimistic Overlay, so the Read model falls back to the synced (server) value. Use when the user
|
|
123
|
+
* abandons their stale edit instead of resolving it as a new write. No-op for an entity with no
|
|
124
|
+
* conflicted entry.
|
|
125
|
+
*/
|
|
126
|
+
discardConflict: <TKey extends SyncTableName<TRegistry>>(table: TKey, entityKey: Record<string, string>) => Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Discard a `quarantined` entity (ADR-0006): clear its quarantined journal entries and the kept
|
|
129
|
+
* optimistic Overlay, so the Read model falls back to the synced (server) value and the entity
|
|
130
|
+
* accepts new mutations again. The rollback path for a permanently-rejected write (e.g. an RLS
|
|
131
|
+
* policy denial routed to quarantine). Symmetric to {@link discardConflict}. No-op for an entity
|
|
132
|
+
* with no quarantined entry; the overlay is kept when another still-owed write depends on it.
|
|
133
|
+
*/
|
|
134
|
+
discardQuarantined: <TKey extends SyncTableName<TRegistry>>(table: TKey, entityKey: Record<string, string>) => Promise<void>;
|
|
53
135
|
flush: (table?: SyncTableName<TRegistry>) => Promise<void>;
|
|
136
|
+
/**
|
|
137
|
+
* Flush one pessimistic write-unit (ADR-0022) to the authoritative endpoint and apply its per-mutation
|
|
138
|
+
* result inline: `acked` clears via the synced echo, `conflicted` keeps the overlay (surfaced), and
|
|
139
|
+
* `rejected` auto-discards the overlay for the whole unit (surfaced via `onReject`). A foreground
|
|
140
|
+
* operation — it resolves once the server has decided. Throws on transport failure (the overlay is kept
|
|
141
|
+
* for a retry). Returns the server acks. Used by the client `transaction({ mode: "pessimistic" })` block.
|
|
142
|
+
*/
|
|
143
|
+
flushUnit: (unitId: string) => Promise<{
|
|
144
|
+
acks: MutationAck[];
|
|
145
|
+
}>;
|
|
54
146
|
reconcile: (table?: SyncTableName<TRegistry>) => Promise<void>;
|
|
55
147
|
retryFailed: (table?: SyncTableName<TRegistry>) => Promise<void>;
|
|
56
148
|
recoverSending: (table?: SyncTableName<TRegistry>) => Promise<void>;
|
|
@@ -59,7 +151,28 @@ export interface MutationRuntime<TRegistry extends SyncTableRegistry> {
|
|
|
59
151
|
createOptimisticRecord: <TKey extends SyncTableName<TRegistry>>(table: TKey, input: SyncTableCreateInput<TRegistry, TKey>) => SyncTableRecord<TRegistry, TKey>;
|
|
60
152
|
}
|
|
61
153
|
export declare function nowMicroseconds(): string;
|
|
154
|
+
/**
|
|
155
|
+
* Best-effort decode of a JWT's claims payload, for stamping `authClaim` managed fields into the
|
|
156
|
+
* optimistic overlay (the value the server will independently stamp from the same claim path). This is
|
|
157
|
+
* **not** a verification — the token is trusted only for a local, optimistic projection that the server
|
|
158
|
+
* re-stamps authoritatively on apply; a forged claim could never make the server attribute the row
|
|
159
|
+
* differently. Returns `undefined` for a malformed token.
|
|
160
|
+
*/
|
|
161
|
+
export declare function decodeJwtClaims(token: string): Record<string, unknown> | undefined;
|
|
162
|
+
/**
|
|
163
|
+
* Read a claim at a JSON path out of a decoded claims object, returning it as a string (the form the
|
|
164
|
+
* optimistic overlay column expects, mirroring the server's `jsonb #>>` text extraction). Returns
|
|
165
|
+
* `undefined` if the path is absent or the leaf is not a string/number.
|
|
166
|
+
*/
|
|
167
|
+
export declare function readJwtClaimPath(claims: Record<string, unknown>, claimPath: string[]): string | undefined;
|
|
62
168
|
export declare function computeBackoffDelayMs(attemptCount: number): number;
|
|
63
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Retry delay with equal jitter around the {@link computeBackoffDelayMs} ceiling: half
|
|
171
|
+
* the ceiling plus a random share of the other half. Spreads retries so a fleet of
|
|
172
|
+
* clients does not stampede the server in lockstep after an outage (ADR-0005 congestion
|
|
173
|
+
* policy). `random` is injectable for deterministic tests.
|
|
174
|
+
*/
|
|
175
|
+
export declare function computeRetryDelayMs(attemptCount: number, random?: () => number): number;
|
|
176
|
+
export declare function computeNextRetryAtUs(nowUs: string, attemptCount: number, random?: () => number): string;
|
|
64
177
|
export declare function createMutationRuntime<TRegistry extends SyncTableRegistry>(options: CreateMutationRuntimeOptions<TRegistry>): MutationRuntime<TRegistry>;
|
|
65
178
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"mutation.d.ts","sourceRoot":"","sources":["../src/mutation.ts"],"names":[],"mappings":"AA0EA,OAAO,KAAK,EAEV,WAAW,EACX,mBAAmB,EAEnB,oBAAoB,EAEpB,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,oBAAoB,EACpB,SAAS,EACV,MAAM,sBAAsB,CAAC;AAa9B,OAAO,EAIL,KAAK,cAAc,EACpB,MAAM,kBAAkB,CAAC;AAE1B,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,MAAM,MAAM,iBAAiB,CAAC,SAAS,SAAS,iBAAiB,IAAI;KAClE,IAAI,IAAI,aAAa,CAAC,SAAS,CAAC,GAC7B;QACE,KAAK,EAAE,IAAI,CAAC;QACZ,IAAI,EAAE,QAAQ,CA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BAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,MAAoB,UAE3G;AAED,wBAAgB,qBAAqB,CAAC,SAAS,SAAS,iBAAiB,EACvE,OAAO,EAAE,4BAA4B,CAAC,SAAS,CAAC,GAC/C,eAAe,CAAC,SAAS,CAAC,CA21B5B"}
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
+
import { type AnyPgTable } from "drizzle-orm/pg-core";
|
|
1
2
|
import { type SyncTableRegistry } from "@pgxsinkit/contracts";
|
|
3
|
+
/**
|
|
4
|
+
* Local key/value metadata table. Holds the registry fingerprint the local store was
|
|
5
|
+
* provisioned under (ADR-0006), so a registry change is detected on boot and the read
|
|
6
|
+
* cache is rebuilt — rather than relying on a hand-bumped `idb://…-vN` suffix.
|
|
7
|
+
*/
|
|
8
|
+
export declare const LOCAL_META_TABLE = "pgxsinkit_local_meta";
|
|
9
|
+
export declare const REGISTRY_FINGERPRINT_KEY = "registry_fingerprint";
|
|
2
10
|
export declare function generateLocalSchemaSql<TRegistry extends SyncTableRegistry>(registry: TRegistry): string;
|
|
11
|
+
/**
|
|
12
|
+
* Render `CREATE TABLE IF NOT EXISTS` (plus a `CREATE INDEX IF NOT EXISTS` per declared index) for a
|
|
13
|
+
* Drizzle `pgTable` / `pgSchema.table`, through the SAME column/PK/type machinery
|
|
14
|
+
* ({@link buildTableColumnSql} → {@link buildColumnDefinition} → {@link mapColumnType}) that
|
|
15
|
+
* {@link generateLocalSchemaSql} uses for every other local relation. The table is schema-qualified
|
|
16
|
+
* when it declares a schema and bare otherwise (per the table object). Column types come from each
|
|
17
|
+
* column's `getSQLType()`, NOT NULL and single-column PRIMARY KEY inline, composite PKs as a trailing
|
|
18
|
+
* clause. Enum column types are qualified into the table's own schema — the metadata pgTables carry
|
|
19
|
+
* none, but the path is shared with the local-store generator so it must stay honest.
|
|
20
|
+
*
|
|
21
|
+
* ADR-0029 D3: the metadata-store DDL renders from its `metadata-tables.ts` pgTables through this,
|
|
22
|
+
* making the pgTable the single source. The OUTPUT is tier-③-by-nature runtime DDL (schema/table
|
|
23
|
+
* identifiers assembled at construction), but it is GENERATED from the model, never hand-maintained.
|
|
24
|
+
*/
|
|
25
|
+
export declare function renderCreateTableSql(table: AnyPgTable): string[];
|
|
26
|
+
/**
|
|
27
|
+
* Drop the reconstructible **read cache** — the synced tables and their read-model views
|
|
28
|
+
* and reconcile triggers/functions — while preserving the authority tables (overlay,
|
|
29
|
+
* journal, the local-meta table). Re-running {@link generateLocalSchemaSql} then rebuilds
|
|
30
|
+
* the synced tables at the current shape, and a re-sync refills them (ADR-0006). This is
|
|
31
|
+
* the named drop primitive a returning-online upgrade uses after a clean drain.
|
|
32
|
+
*/
|
|
33
|
+
export declare function buildDropReadCacheSql<TRegistry extends SyncTableRegistry>(registry: TRegistry): string;
|
|
34
|
+
/**
|
|
35
|
+
* Wipe the **entire** local store provisioned from the registry — the read cache plus the
|
|
36
|
+
* authority tables (overlay, journal, sequences), the enum types, and the local-meta row.
|
|
37
|
+
* This is the full teardown reused by `destroy()` (ADR-0005 decision 5) and by the
|
|
38
|
+
* drain-then-drop upgrade once a clean drain has confirmed nothing is owed (ADR-0006).
|
|
39
|
+
*/
|
|
40
|
+
export declare function buildWipeLocalStoreSql<TRegistry extends SyncTableRegistry>(registry: TRegistry): string;
|
|
41
|
+
/**
|
|
42
|
+
* Clean-truncate a single table's local read cache so a desynced `lazy` relation (ADR-0021 §2) returns
|
|
43
|
+
* to an empty, dormant state: the synced table and — for a writable table — its optimistic overlay and
|
|
44
|
+
* the journal, plus a sequence restart. It does NOT drop the cluster (the table/views/trigger persist),
|
|
45
|
+
* so a later re-reference re-streams into it without a schema rebuild. Ephemeral-aware: bare/`pg_temp`
|
|
46
|
+
* names for an ephemeral cluster, schema-qualified for a persistent one — mirroring
|
|
47
|
+
* {@link generateLocalSchemaSql}. The caller (`desync`) first stops the group's stream (so nothing
|
|
48
|
+
* re-populates mid-truncate) and refuses when the journal owes unsettled writes (so this never drops
|
|
49
|
+
* un-acked local intent).
|
|
50
|
+
*/
|
|
51
|
+
export declare function buildDesyncTableSql<TRegistry extends SyncTableRegistry>(registry: TRegistry, tableKey: string): string;
|
|
3
52
|
//# sourceMappingURL=schema.d.ts.map
|
package/dist/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAErF,OAAO,EAUL,KAAK,iBAAiB,EACvB,MAAM,sBAAsB,CAAC;AAyB9B;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,yBAAyB,CAAC;AACvD,eAAO,MAAM,wBAAwB,yBAAyB,CAAC;AAI/D,wBAAgB,sBAAsB,CAAC,SAAS,SAAS,iBAAiB,EAAE,QAAQ,EAAE,SAAS,UAuO9F;AA0CD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,EAAE,CAchE;AAoQD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,SAAS,iBAAiB,EAAE,QAAQ,EAAE,SAAS,GAAG,MAAM,CAkBtG;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,SAAS,iBAAiB,EAAE,QAAQ,EAAE,SAAS,GAAG,MAAM,CA8BvG;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,SAAS,iBAAiB,EACrE,QAAQ,EAAE,SAAS,EACnB,QAAQ,EAAE,MAAM,GACf,MAAM,CAkCR"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { ExternalHeadersRecord } from "@electric-sql/client";
|
|
2
|
+
import type { PGliteInterface } from "@electric-sql/pglite";
|
|
3
|
+
import { type Retention, type SubscriptionTiming, type SyncConfigInput, type SyncTableRegistry } from "@pgxsinkit/contracts";
|
|
4
|
+
import type { SyncNamespaceObj } from "./sync";
|
|
5
|
+
export interface ShapeSyncSpec {
|
|
6
|
+
electricUrl: string;
|
|
7
|
+
tableName: string;
|
|
8
|
+
schema?: string;
|
|
9
|
+
shapeKey: string;
|
|
10
|
+
primaryKey: string[];
|
|
11
|
+
electricTable?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Consistency group (ADR-0009 decision 2). Specs sharing a group sync on one `MultiShapeStream`
|
|
14
|
+
* and commit atomically. Absent → the table is its own singleton group keyed by its `shapeKey`.
|
|
15
|
+
*/
|
|
16
|
+
consistencyGroup?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Subscription timing (ADR-0021). Absent → `eager`. A `lazy` spec's group is held out of the eager
|
|
19
|
+
* boot set and started on demand via {@link StartConfiguredSyncResult.ensureGroupStarted}. A group's
|
|
20
|
+
* members all agree (registry-validated), so the group's timing is any member's.
|
|
21
|
+
*/
|
|
22
|
+
subscription?: SubscriptionTiming;
|
|
23
|
+
/**
|
|
24
|
+
* Retention (ADR-0021). Absent → `persistent`. Only a `persistent` `lazy` group is promoted to the
|
|
25
|
+
* eager set on activation (its activation is permanent across boots); an `ephemeral` group's activation
|
|
26
|
+
* is session-scoped by construction, so it is never promoted/persisted.
|
|
27
|
+
*/
|
|
28
|
+
retention?: Retention;
|
|
29
|
+
}
|
|
30
|
+
export interface ConfiguredShapeSyncSpec extends ShapeSyncSpec {
|
|
31
|
+
key: string;
|
|
32
|
+
}
|
|
33
|
+
export interface StartConfiguredSyncOptions {
|
|
34
|
+
syncConfig: SyncConfigInput;
|
|
35
|
+
/**
|
|
36
|
+
* The sync registry (ADR-0029 D1): the engine's sole per-table spec source. Each shape's `tableKey`
|
|
37
|
+
* (its `syncConfig` table key) resolves against it for the local table, PKs, apply strategy, and
|
|
38
|
+
* column types — no table-scoped fact is threaded from `syncConfig` into the engine.
|
|
39
|
+
*/
|
|
40
|
+
registry: SyncTableRegistry;
|
|
41
|
+
/**
|
|
42
|
+
* Shape request headers shared by every member shape. Values may be async functions resolved per
|
|
43
|
+
* request (ADR-0013) — the read-path `Authorization` token is one such function.
|
|
44
|
+
*/
|
|
45
|
+
shapeHeaders?: ExternalHeadersRecord;
|
|
46
|
+
onInitialSync?: () => void;
|
|
47
|
+
onTableInitialSync?: (tableKey: string) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Fresh-store prefetch overlap (ADR-0032 S4). When present, every EAGER group starts its shape streams
|
|
50
|
+
* and buffers their catch-up into the memory inbox immediately, gating all commits until this promise
|
|
51
|
+
* resolves — so the network catch-up overlaps the local boot phases (schema/journal/reconcile) instead
|
|
52
|
+
* of running after them. Set ONLY by `createSyncClient` on a provably-fresh store (its `freshStore`
|
|
53
|
+
* hint); absent → the sequential path. Passed straight through to the engine's `syncShapesToTables`.
|
|
54
|
+
*/
|
|
55
|
+
dbReady?: Promise<void>;
|
|
56
|
+
/** Commit-level error surfacing (ADR-0009 decision 5): a sync commit exhausted its retries. */
|
|
57
|
+
onSyncError?: (error: Error) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Notified when a shape hits an auth error (401/403) and the read path is retrying for a fresh
|
|
60
|
+
* token (ADR-0013 Phase 3). The runtime surfaces a distinct `auth-needed` status from this.
|
|
61
|
+
*/
|
|
62
|
+
onAuthError?: () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Notified when a shape hits a NON-auth stream error (#4): a transient 5xx/429/network (retried) or
|
|
65
|
+
* a structural 4xx (stops the stream). The runtime surfaces a `degraded` status from this so it
|
|
66
|
+
* never silently believes the read path is live while it has actually stalled.
|
|
67
|
+
*/
|
|
68
|
+
onReadStreamError?: (error: Error) => void;
|
|
69
|
+
/**
|
|
70
|
+
* Notified when a shape successfully delivers a batch — i.e. a fetch just succeeded (ADR-0013
|
|
71
|
+
* Phase 3). The runtime uses this to clear an `auth-needed`/`degraded` status once sync resumes.
|
|
72
|
+
*/
|
|
73
|
+
onSyncActivity?: () => void;
|
|
74
|
+
/**
|
|
75
|
+
* Consistency-group keys of `lazy` groups that were activated on a previous boot and persisted their
|
|
76
|
+
* activation flag (ADR-0021 §2 — `lazy + persistent` promotion). These join the eager boot set as if
|
|
77
|
+
* they were declared `eager`, so a once-activated durable lazy group resumes without re-evaluation.
|
|
78
|
+
* Computed by the caller from the local meta table (the sync engine does no DB read of its own).
|
|
79
|
+
*/
|
|
80
|
+
promotedGroups?: ReadonlySet<string>;
|
|
81
|
+
/**
|
|
82
|
+
* Invoked when a `persistent` `lazy` group is activated on demand (not promoted at boot), so the
|
|
83
|
+
* caller can persist its activation flag for the next boot (ADR-0021 §2). Never fired for an
|
|
84
|
+
* `ephemeral` group (its activation is session-scoped and leaves no durable trace).
|
|
85
|
+
*/
|
|
86
|
+
onLazyActivated?: (groupKey: string) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Invoked exactly once per consistency group the moment it becomes up-to-date (ADR-0032 decision 6 —
|
|
89
|
+
* per-group readiness). Fires for eager, promoted, and on-demand `lazy` groups alike, so a client can
|
|
90
|
+
* surface progressive per-group paint (and the worker bridge can fan a `groupReady` event to tabs)
|
|
91
|
+
* without the all-eager-groups `onInitialSync` gate. The matching promise is {@link StartConfiguredSyncResult.groupReady}.
|
|
92
|
+
*/
|
|
93
|
+
onGroupReady?: (groupKey: string) => void;
|
|
94
|
+
}
|
|
95
|
+
type SyncEnginePGlite = PGliteInterface & {
|
|
96
|
+
electric: SyncNamespaceObj;
|
|
97
|
+
};
|
|
98
|
+
export interface ShapeSyncResult {
|
|
99
|
+
unsubscribe: () => void;
|
|
100
|
+
readonly isUpToDate: boolean;
|
|
101
|
+
}
|
|
102
|
+
export interface StartConfiguredSyncResult {
|
|
103
|
+
unsubscribe: () => void;
|
|
104
|
+
tables: Record<string, ShapeSyncResult>;
|
|
105
|
+
/**
|
|
106
|
+
* Start a `lazy` consistency group (ADR-0021) held out of the eager boot set. Idempotent and
|
|
107
|
+
* single-flight: concurrent calls share one start; a started, unknown, or eager group resolves
|
|
108
|
+
* immediately. Resolves once the group's initial sync completes — the seam the live-query layer
|
|
109
|
+
* calls on first reference to a lazy table. After it resolves, the group's member `tables` entries
|
|
110
|
+
* report `isUpToDate` live.
|
|
111
|
+
*/
|
|
112
|
+
ensureGroupStarted: (groupKey: string) => Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Stop a group's stream and return it to dormant (ADR-0021 §2): tear down its live subscription and
|
|
115
|
+
* clear the runtime so {@link isTableStarted} reports false again and a later {@link ensureGroupStarted}
|
|
116
|
+
* re-subscribes from scratch — the inverse of an on-demand activation. Used by the client's `desync`
|
|
117
|
+
* to halt a lazy group before truncating its read cache. A no-op for an unknown or never-started group.
|
|
118
|
+
* The temp cluster of an `ephemeral` group is NOT dropped here (that is the deferred idle-eviction); the
|
|
119
|
+
* cluster simply stops being fed.
|
|
120
|
+
*/
|
|
121
|
+
stopGroup: (groupKey: string) => void;
|
|
122
|
+
/** The consistency-group key a table belongs to — maps a queried table to its {@link ensureGroupStarted}. */
|
|
123
|
+
groupKeyForTable: (tableKey: string) => string | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* Whether a table's consistency group has started and completed its initial sync, so its reads are
|
|
126
|
+
* hydrated (ADR-0021). False for a `lazy` group still held out of the boot set. Drives the read-path
|
|
127
|
+
* tripwire — a lazy relation read while this is false would return empty/stale rows.
|
|
128
|
+
*/
|
|
129
|
+
isTableStarted: (tableKey: string) => boolean;
|
|
130
|
+
/**
|
|
131
|
+
* A promise that resolves the moment the given consistency group is up-to-date (ADR-0032 decision 6).
|
|
132
|
+
* Resolves immediately for an already-ready group; stays pending for a `lazy` group until it is
|
|
133
|
+
* activated (via {@link ensureGroupStarted}) and catches up. An unknown group resolves immediately
|
|
134
|
+
* (nothing to await). Per-group readiness the client re-exposes as `client.groupReady(table)`.
|
|
135
|
+
*/
|
|
136
|
+
groupReady: (groupKey: string) => Promise<void>;
|
|
137
|
+
/** Whether the given consistency group has completed its initial sync (synchronous peek at {@link groupReady}). */
|
|
138
|
+
isGroupReady: (groupKey: string) => boolean;
|
|
139
|
+
/** Every consistency-group key this config declares — the domain of {@link groupReady}/{@link isGroupReady}. */
|
|
140
|
+
groupKeys: () => string[];
|
|
141
|
+
}
|
|
142
|
+
export declare function buildShapeUrl(electricUrl: string, table: string): string;
|
|
143
|
+
export declare function buildShapeConfig(input: ShapeSyncSpec): {
|
|
144
|
+
shape: {
|
|
145
|
+
url: string;
|
|
146
|
+
};
|
|
147
|
+
table: string;
|
|
148
|
+
schema?: string;
|
|
149
|
+
primaryKey: string[];
|
|
150
|
+
shapeKey: string;
|
|
151
|
+
};
|
|
152
|
+
export declare function buildConfiguredShapeSpecs(input: SyncConfigInput): ConfiguredShapeSyncSpec[];
|
|
153
|
+
export declare function startConfiguredSync(pg: SyncEnginePGlite, input: StartConfiguredSyncOptions): Promise<StartConfiguredSyncResult>;
|
|
154
|
+
interface StartGroupSyncOptions {
|
|
155
|
+
groupKey: string;
|
|
156
|
+
specs: ConfiguredShapeSyncSpec[];
|
|
157
|
+
registry: SyncTableRegistry;
|
|
158
|
+
headers?: ExternalHeadersRecord;
|
|
159
|
+
/** Fresh-store prefetch overlap gate (ADR-0032 S4) — forwarded to the engine's `syncShapesToTables`. */
|
|
160
|
+
dbReady?: Promise<void>;
|
|
161
|
+
onGroupInitialSync?: () => void;
|
|
162
|
+
onSyncError?: (error: Error) => void;
|
|
163
|
+
onAuthError?: () => void;
|
|
164
|
+
onReadStreamError?: (error: Error) => void;
|
|
165
|
+
onSyncActivity?: () => void;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Sync one consistency group on a single `MultiShapeStream` (ADR-0009 decision 2). All member
|
|
169
|
+
* shapes share the group's subscription key and commit atomically at a shared LSN frontier; each
|
|
170
|
+
* shape keeps its own apply strategy and column types (resolved per-shape inside the engine).
|
|
171
|
+
*/
|
|
172
|
+
export declare function startGroupSync(pg: SyncEnginePGlite, input: StartGroupSyncOptions): Promise<import("./sync").SyncShapesToTablesResult>;
|
|
173
|
+
/**
|
|
174
|
+
* The subscription key for a spec's consistency group (ADR-0009 decision 2): the explicit
|
|
175
|
+
* `consistencyGroup` when set, else the table's own `shapeKey` (a singleton group). One persisted
|
|
176
|
+
* subscription-state row exists per group key, so subscription reset enumerates these.
|
|
177
|
+
*/
|
|
178
|
+
export declare function groupSubscriptionKey(spec: Pick<ConfiguredShapeSyncSpec, "consistencyGroup" | "shapeKey">): string;
|
|
179
|
+
export {};
|
|
180
|
+
//# sourceMappingURL=shape-sync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape-sync.d.ts","sourceRoot":"","sources":["../src/shape-sync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EACL,KAAK,SAAS,EACd,KAAK,kBAAkB,EACvB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EAEvB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAG/C,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;OAIG;IACH,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC;;;;OAIG;IACH,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB;AAED,MAAM,WAAW,uBAAwB,SAAQ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KAAK,KAAK,IAAI,CAAC;IACrC,WAAW,CAAC,EAAE,MAAM,IAAI,CAAC;IACzB,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAC3C,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;CAC7B;AAED;;;;GAIG;AACH,wBAAsB,cAAc,CAAC,EAAE,EAAE,gBAAgB,EAAE,KAAK,EAAE,qBAAqB,sDAsCtF;AAgCD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,IAAI,CAAC,uBAAuB,EAAE,kBAAkB,GAAG,UAAU,CAAC,GAAG,MAAM,CAEjH"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ChangeMessage, Row } from "@electric-sql/client";
|
|
2
|
+
import type { PGliteInterface, Transaction } from "@electric-sql/pglite";
|
|
3
|
+
import type { ApplyTarget } from "../local-tables";
|
|
4
|
+
import type { InsertChangeMessage } from "./types";
|
|
5
|
+
export interface ApplyMessageToTableOptions {
|
|
6
|
+
pg: PGliteInterface | Transaction;
|
|
7
|
+
target: ApplyTarget;
|
|
8
|
+
message: ChangeMessage<Row<unknown>>;
|
|
9
|
+
debug: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function applyMessageToTable({ pg, target, message, debug }: ApplyMessageToTableOptions): Promise<import("@electric-sql/pglite").Results<never> | undefined>;
|
|
12
|
+
export interface BulkApplyMessagesToTableOptions {
|
|
13
|
+
pg: PGliteInterface | Transaction;
|
|
14
|
+
target: ApplyTarget;
|
|
15
|
+
messages: InsertChangeMessage[];
|
|
16
|
+
debug: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function applyInsertsToTable({ pg, target, messages, debug }: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Bulk **upsert** for tagged-subquery move-in rows (ADR-0024). A move-in is an existing row ENTERING the
|
|
21
|
+
* shape — unlike a CDC `insert` (a brand-new row) it may already be present locally via an independent
|
|
22
|
+
* grant, or be re-delivered on a resume from before the move-in's offset. So it is applied idempotently:
|
|
23
|
+
* `INSERT … ON CONFLICT (pk) DO UPDATE` refreshes to the move-in's authoritative value (or `DO NOTHING`
|
|
24
|
+
* for a pk-only table). The plain-`INSERT` invariant of the CDC path — a genuine PK collision must
|
|
25
|
+
* surface ({@link applyInsertsToTable}) — is intentionally NOT used here, because for a move-in a present
|
|
26
|
+
* row is expected, not a bug. Batched by parameter count (move-in volume is incremental, not a backfill).
|
|
27
|
+
*/
|
|
28
|
+
export declare function applyUpsertsToTable({ pg, target, messages, debug }: BulkKeyedApplyOptions): Promise<void>;
|
|
29
|
+
export declare function applyMessagesToTableWithJson({ pg, target, messages, debug }: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
30
|
+
export declare function applyMessagesToTableWithCopy({ pg, target, messages, debug }: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
31
|
+
export interface BulkKeyedApplyOptions {
|
|
32
|
+
pg: PGliteInterface | Transaction;
|
|
33
|
+
target: ApplyTarget;
|
|
34
|
+
/** Folded messages: for deletes the value carries the PK; for updates the PK plus merged columns. */
|
|
35
|
+
messages: ChangeMessage<Row<unknown>>[];
|
|
36
|
+
debug: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Bulk DELETE by primary key via `DELETE … USING json_to_recordset(…)` (ADR-0014 Phase 3). Safe
|
|
40
|
+
* because the read-path fold already left **one row per PK** — no same-PK duplicate in the source.
|
|
41
|
+
*/
|
|
42
|
+
export declare function applyBulkDeletesToTable({ pg, target, messages, debug }: BulkKeyedApplyOptions): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Bulk UPDATE via `UPDATE … FROM json_to_recordset(…)` (ADR-0014 Phase 3), **grouped by the set of
|
|
45
|
+
* non-PK columns** each row carries. Electric's default replica sends only the changed columns, so
|
|
46
|
+
* two rows in one batch can set different columns; one `UPDATE … FROM` per distinct column-set keeps
|
|
47
|
+
* each statement uniform. Safe from the same-PK join hazard because the fold already left one row per
|
|
48
|
+
* PK, so no PK appears twice within a group.
|
|
49
|
+
*/
|
|
50
|
+
export declare function applyBulkUpdatesToTable({ pg, target, messages, debug }: BulkKeyedApplyOptions): Promise<void>;
|
|
51
|
+
//# sourceMappingURL=apply.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../../src/sync/apply.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAMzE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGnD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AA4BnD,MAAM,WAAW,0BAA0B;IACzC,EAAE,EAAE,eAAe,GAAG,WAAW,CAAC;IAClC,MAAM,EAAE,WAAW,CAAC;IACpB,OAAO,EAAE,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;IACrC,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,0BAA0B,sEA+BnG;AAED,MAAM,WAAW,+BAA+B;IAC9C,EAAE,EAAE,eAAe,GAAG,WAAW,CAAC;IAClC,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,mBAAmB,EAAE,CAAC;IAChC,KAAK,EAAE,OAAO,CAAC;CAChB;AAyDD,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,+BAA+B,iBA2GzG;AAED;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,qBAAqB,iBAsC/F;AAqBD,wBAAsB,4BAA4B,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,+BAA+B,iBA6BlH;AAsBD,wBAAsB,4BAA4B,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,+BAA+B,iBAuClH;AAoBD,MAAM,WAAW,qBAAqB;IACpC,EAAE,EAAE,eAAe,GAAG,WAAW,CAAC;IAClC,MAAM,EAAE,WAAW,CAAC;IACpB,qGAAqG;IACrG,QAAQ,EAAE,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;IACxC,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,qBAAqB,iBA+BnG;AAED;;;;;;GAMG;AACH,wBAAsB,uBAAuB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,qBAAqB,iBAgDnG"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialization of JavaScript values into a PostgreSQL `COPY ... WITH (FORMAT
|
|
3
|
+
* text)` stream.
|
|
4
|
+
*
|
|
5
|
+
* The Electric client parses the wire values it receives into native JS values
|
|
6
|
+
* before they reach this module: `int2`/`int4`/`float4`/`float8` become
|
|
7
|
+
* `number`, `int8` becomes `bigint`, `bool` becomes `boolean`, `json`/`jsonb`
|
|
8
|
+
* become parsed objects/arrays, array columns become (possibly nested) JS
|
|
9
|
+
* arrays, and every other type is left as its raw Postgres text representation
|
|
10
|
+
* (a `string`). To feed those values back into `COPY` we have to reverse that:
|
|
11
|
+
* turn each value into the exact text Postgres' input functions expect, then
|
|
12
|
+
* apply the COPY framing.
|
|
13
|
+
*
|
|
14
|
+
* Rather than invent an escaping scheme (the previous CSV-based approach broke
|
|
15
|
+
* on arrays, JSON, embedded delimiters, etc.) this is a faithful port of the
|
|
16
|
+
* two relevant PostgreSQL backend routines:
|
|
17
|
+
*
|
|
18
|
+
* - `CopyAttributeOutText` (src/backend/commands/copyto.c) — field escaping
|
|
19
|
+
* - `array_out` (src/backend/utils/adt/arrayfuncs.c) — array literals
|
|
20
|
+
*
|
|
21
|
+
* The TEXT format is used (not CSV) because it is what Postgres itself emits
|
|
22
|
+
* internally, has a single well-defined escaping algorithm, and round-trips
|
|
23
|
+
* every built-in type.
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Serialize a single value into one COPY TEXT field. `null`/`undefined` become
|
|
27
|
+
* the NULL marker (`\N`); everything else is converted to text and escaped.
|
|
28
|
+
*
|
|
29
|
+
* `udtName` is the column's Postgres `udt_name`. When supplied it disambiguates
|
|
30
|
+
* `json`/`jsonb` columns; when omitted the value's runtime type is used (which
|
|
31
|
+
* cannot tell a `jsonb` array from a SQL array).
|
|
32
|
+
*/
|
|
33
|
+
export declare function serializeCopyValue(value: unknown, udtName?: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Serialize a list of row objects into the body of a `COPY ... FROM` request in
|
|
36
|
+
* TEXT format. Columns are emitted in the given order, fields are tab
|
|
37
|
+
* separated, and rows are newline separated.
|
|
38
|
+
*
|
|
39
|
+
* `columnTypes` optionally maps a column name to its Postgres `udt_name`; pass
|
|
40
|
+
* it so `json`/`jsonb` columns serialize correctly.
|
|
41
|
+
*/
|
|
42
|
+
export declare function generateCopyData(rows: ReadonlyArray<Record<string, unknown>>, columns: ReadonlyArray<string>, columnTypes?: Readonly<Record<string, string | undefined>>): string;
|
|
43
|
+
//# sourceMappingURL=copy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copy.d.ts","sourceRoot":"","sources":["../../src/sync/copy.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAkJH;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAO3E;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC5C,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,EAC9B,WAAW,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GACzD,MAAM,CAIR"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { PGliteInterface, Transaction } from "@electric-sql/pglite";
|
|
2
|
+
import { type PgliteDatabase } from "drizzle-orm/pglite";
|
|
3
|
+
/** A (memoized) Drizzle handle over an open PGlite connection or transaction — engine-internal. */
|
|
4
|
+
export declare function drizzleOverPg(pg: PGliteInterface | Transaction): PgliteDatabase<never>;
|
|
5
|
+
//# sourceMappingURL=drizzle-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drizzle-executor.d.ts","sourceRoot":"","sources":["../../src/sync/drizzle-executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,eAAe,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACjF,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAsClE,mGAAmG;AACnG,wBAAgB,aAAa,CAAC,EAAE,EAAE,eAAe,GAAG,WAAW,GAAG,cAAc,CAAC,KAAK,CAAC,CAOtF"}
|