@pgxsinkit/client 0.2.7 → 0.5.0
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/LICENSE +21 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +81 -0
- package/README.md +11 -3
- package/dist/boot-report.d.ts +11 -8
- package/dist/boot-report.d.ts.map +1 -1
- package/dist/circuits/envelope-to-change.d.ts +39 -0
- package/dist/circuits/envelope-to-change.d.ts.map +1 -0
- package/dist/circuits/group-sync.d.ts +123 -0
- package/dist/circuits/group-sync.d.ts.map +1 -0
- package/dist/circuits/shape-group.d.ts +77 -0
- package/dist/circuits/shape-group.d.ts.map +1 -0
- package/dist/circuits/stream-inbox.d.ts +126 -0
- package/dist/circuits/stream-inbox.d.ts.map +1 -0
- package/dist/circuits/stream-source.d.ts +96 -0
- package/dist/circuits/stream-source.d.ts.map +1 -0
- package/dist/circuits/subscription-client.d.ts +143 -0
- package/dist/circuits/subscription-client.d.ts.map +1 -0
- package/dist/circuits/sync-engine.d.ts +131 -0
- package/dist/circuits/sync-engine.d.ts.map +1 -0
- package/dist/debug.d.ts +0 -10
- package/dist/debug.d.ts.map +1 -1
- package/dist/event-lane.d.ts +32 -1
- package/dist/event-lane.d.ts.map +1 -1
- package/dist/export-data.d.ts +3 -3
- package/dist/export-dump.d.ts +9 -9
- package/dist/export-dump.d.ts.map +1 -1
- package/dist/export-store.d.ts +13 -13
- package/dist/index.d.ts +237 -113
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7037 -6665
- package/dist/index.js.map +46 -43
- package/dist/local-store.d.ts +1 -1
- package/dist/local-store.d.ts.map +1 -1
- package/dist/local-tables.d.ts +59 -1
- package/dist/local-tables.d.ts.map +1 -1
- package/dist/mutation.d.ts +2 -2
- package/dist/mutation.d.ts.map +1 -1
- package/dist/mutations-api.d.ts.map +1 -1
- package/dist/opfs-effects.d.ts +30 -2
- package/dist/opfs-effects.d.ts.map +1 -1
- package/dist/schema.d.ts +12 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/store-boot.d.ts +30 -2
- package/dist/store-boot.d.ts.map +1 -1
- package/dist/store-build.d.ts +19 -0
- package/dist/store-build.d.ts.map +1 -0
- package/dist/store-engine.d.ts +22 -0
- package/dist/store-engine.d.ts.map +1 -0
- package/dist/store-meta.d.ts +29 -3
- package/dist/store-meta.d.ts.map +1 -1
- package/dist/store-ownership-lock.d.ts +33 -0
- package/dist/store-ownership-lock.d.ts.map +1 -0
- package/dist/store-path.d.ts +40 -28
- package/dist/store-path.d.ts.map +1 -1
- package/dist/sync/apply.d.ts +17 -25
- package/dist/sync/apply.d.ts.map +1 -1
- package/dist/sync/copy.d.ts +84 -8
- package/dist/sync/copy.d.ts.map +1 -1
- package/dist/sync/drizzle-executor.d.ts +4 -4
- package/dist/sync/drizzle-executor.d.ts.map +1 -1
- package/dist/sync/fold.d.ts +40 -0
- package/dist/sync/fold.d.ts.map +1 -0
- package/dist/sync/metadata-tables.d.ts +25 -1
- package/dist/sync/metadata-tables.d.ts.map +1 -1
- package/dist/sync/subscription-state.d.ts +26 -7
- package/dist/sync/subscription-state.d.ts.map +1 -1
- package/dist/sync/types.d.ts +9 -129
- package/dist/sync/types.d.ts.map +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +4 -3
- package/dist/testing.js.map +4 -4
- package/dist/worker/attach-sync-client.d.ts +27 -14
- package/dist/worker/attach-sync-client.d.ts.map +1 -1
- package/dist/worker/define-sync-worker.d.ts +52 -19
- package/dist/worker/define-sync-worker.d.ts.map +1 -1
- package/dist/worker/engine-control.d.ts +53 -0
- package/dist/worker/engine-control.d.ts.map +1 -1
- package/dist/worker/live-query-manager.d.ts +3 -3
- package/dist/worker/live-query-manager.d.ts.map +1 -1
- package/dist/worker/live-query-params-guard.d.ts +5 -5
- package/dist/worker/protocol.d.ts +26 -7
- package/dist/worker/protocol.d.ts.map +1 -1
- package/package.json +11 -9
- package/skills/core/SKILL.md +145 -63
- package/skills/operating/SKILL.md +221 -221
- package/skills/operating/references/postgres-builds.md +48 -0
- package/src/boot-report.ts +13 -12
- package/src/circuits/envelope-to-change.ts +211 -0
- package/src/circuits/group-sync.ts +1047 -0
- package/src/circuits/shape-group.ts +158 -0
- package/src/circuits/stream-inbox.ts +225 -0
- package/src/circuits/stream-source.ts +191 -0
- package/src/circuits/subscription-client.ts +353 -0
- package/src/circuits/sync-engine.ts +643 -0
- package/src/debug.ts +1 -85
- package/src/event-lane.ts +132 -14
- package/src/export-data.ts +4 -4
- package/src/export-dump.ts +24 -28
- package/src/export-store.ts +17 -17
- package/src/index.ts +797 -544
- package/src/lazy-guard.ts +1 -1
- package/src/live-rows-sql.ts +1 -1
- package/src/local-store.ts +2 -2
- package/src/local-tables.ts +76 -5
- package/src/mutation.ts +9 -8
- package/src/mutations-api.ts +2 -2
- package/src/opfs-effects.ts +74 -10
- package/src/schema.ts +126 -29
- package/src/store-boot.ts +71 -7
- package/src/store-build.ts +37 -0
- package/src/store-engine.ts +94 -0
- package/src/store-lifecycle.ts +1 -1
- package/src/store-meta.ts +155 -4
- package/src/store-ownership-lock.ts +109 -0
- package/src/store-path.ts +56 -40
- package/src/sync/apply.ts +104 -156
- package/src/sync/copy.ts +116 -21
- package/src/sync/drizzle-executor.ts +13 -14
- package/src/sync/fold.ts +94 -0
- package/src/sync/metadata-tables.ts +43 -2
- package/src/sync/subscription-state.ts +28 -10
- package/src/sync/types.ts +13 -141
- package/src/testing.ts +4 -4
- package/src/worker/attach-sync-client.ts +150 -57
- package/src/worker/define-sync-worker.ts +325 -87
- package/src/worker/engine-control.ts +89 -0
- package/src/worker/engine-router.ts +1 -1
- package/src/worker/live-query-fingerprint.ts +4 -4
- package/src/worker/live-query-manager.ts +12 -12
- package/src/worker/live-query-params-guard.ts +10 -10
- package/src/worker/protocol.ts +38 -7
- package/dist/read-stream-stall.d.ts +0 -54
- package/dist/read-stream-stall.d.ts.map +0 -1
- package/dist/shape-sync.d.ts +0 -207
- package/dist/shape-sync.d.ts.map +0 -1
- package/dist/sync/index.d.ts +0 -32
- package/dist/sync/index.d.ts.map +0 -1
- package/dist/sync/nudge.d.ts +0 -49
- package/dist/sync/nudge.d.ts.map +0 -1
- package/dist/sync/shape-inbox.d.ts +0 -215
- package/dist/sync/shape-inbox.d.ts.map +0 -1
- package/dist/sync/tags.d.ts +0 -142
- package/dist/sync/tags.d.ts.map +0 -1
- package/dist/sync-auth.d.ts +0 -53
- package/dist/sync-auth.d.ts.map +0 -1
- package/src/read-stream-stall.ts +0 -102
- package/src/shape-sync.ts +0 -533
- package/src/sync/index.ts +0 -1124
- package/src/sync/nudge.ts +0 -110
- package/src/sync/shape-inbox.ts +0 -566
- package/src/sync/tags.ts +0 -390
- package/src/sync-auth.ts +0 -117
package/dist/store-path.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { StorageDescription } from "@pgxsinkit/pgwasm";
|
|
1
2
|
/**
|
|
2
3
|
* Thrown when a `storePath` is not a plain name (ADR-0036 decision 1): it carries a URL scheme (`://`) or
|
|
3
4
|
* is empty/whitespace-only. A distinct type — not a bare `Error` — so a caller can `instanceof`-branch the
|
|
@@ -8,14 +9,15 @@ export declare class InvalidStorePathError extends Error {
|
|
|
8
9
|
constructor(message: string);
|
|
9
10
|
}
|
|
10
11
|
/**
|
|
11
|
-
* Thrown when a caller-owned
|
|
12
|
-
* {@link CreateSyncClientOptions.
|
|
13
|
-
* `
|
|
14
|
-
* `memory
|
|
15
|
-
* consumer is never left guessing which store to
|
|
12
|
+
* Thrown when a caller-owned store handed to {@link CreateSyncClientOptions.pgwasmInstance} /
|
|
13
|
+
* {@link CreateSyncClientOptions.precreatedPgwasm} is PROVABLY non-persistent (ADR-0036 decision 4): its
|
|
14
|
+
* `storage` is memory (pgwasm's default — the bare `createPgwasm({ build })` a copy-paste reaches — or an
|
|
15
|
+
* explicit `memory://`), or a filesystem that explicitly reports itself non-persistent. Names both the why (durability
|
|
16
|
+
* semantics assume a persisted store) and the two exits, so a consumer is never left guessing which store to
|
|
17
|
+
* hand us instead.
|
|
16
18
|
*/
|
|
17
19
|
export declare class NonPersistentStoreError extends Error {
|
|
18
|
-
constructor(observed:
|
|
20
|
+
constructor(observed: NonPersistentStorage);
|
|
19
21
|
}
|
|
20
22
|
/**
|
|
21
23
|
* Thrown when a restore (`restoreFrom`, ADR-0035 decision 6) targets a store path whose backend store ALREADY
|
|
@@ -52,10 +54,10 @@ export type WithTestStoreMarker = {
|
|
|
52
54
|
/** Read the internal testing marker off an options object, or `undefined` when none was stamped. */
|
|
53
55
|
export declare function readTestStoreMarker(options: unknown): TestStoreMarker | undefined;
|
|
54
56
|
/**
|
|
55
|
-
* What {@link
|
|
57
|
+
* What {@link createPgwasmClient} (and any store-opening seam) accepts as its store argument: a plain
|
|
56
58
|
* `storePath` string, or the option object a testing helper mints (`memoryStoreForTests(...)`) — carrying a
|
|
57
59
|
* `storePath` plus the invisible internal marker. Accepting the object form is what lets the testing
|
|
58
|
-
* helper's output flow STRAIGHT into `
|
|
60
|
+
* helper's output flow STRAIGHT into `createPgwasmClient(memoryStoreForTests("x"))` without a consumer ever
|
|
59
61
|
* naming a backend.
|
|
60
62
|
*/
|
|
61
63
|
export type StorePathInput = string | ({
|
|
@@ -68,7 +70,7 @@ export declare function normaliseStorePathInput(input: StorePathInput): {
|
|
|
68
70
|
};
|
|
69
71
|
/**
|
|
70
72
|
* The environment seam the backend derivation reads — factored out so a browser derivation is fakeable in
|
|
71
|
-
* a Bun unit test (there is no `indexedDB` there). `hasIndexedDb` is the browser/worker signal:
|
|
73
|
+
* a Bun unit test (there is no `indexedDB` there). `hasIndexedDb` is the browser/worker signal: pgwasm's
|
|
72
74
|
* IndexedDB backend needs `indexedDB`, which a browser tab AND a Web/Shared/dedicated worker expose but
|
|
73
75
|
* Bun/Node do not — so it distinguishes "run in the browser" (idb) from "run on the server" (filesystem)
|
|
74
76
|
* without a DOM-lib dependency.
|
|
@@ -86,7 +88,7 @@ export interface StoreEnv {
|
|
|
86
88
|
hasOpfsSyncAccess?: boolean;
|
|
87
89
|
}
|
|
88
90
|
/**
|
|
89
|
-
* Resolve a plain {@link storePath} to the
|
|
91
|
+
* Resolve a plain {@link storePath} to the pgwasm dataDir URL the store opens at — the ONE derivation point
|
|
90
92
|
* (ADR-0036 decision 2, amended by ADR-0049). A browser worker with a proven OPFS sync-access grant resolves
|
|
91
93
|
* to `opfs://<storePath>`; another browser context (`indexedDB` present) resolves to `idb://<storePath>`;
|
|
92
94
|
* Bun/Node resolves to `file://<storePath>` (relative paths use the working directory). The `backendOverride`
|
|
@@ -97,15 +99,15 @@ export interface StoreEnv {
|
|
|
97
99
|
* internal plumbing; do not surface it to consumers as something to imitate.
|
|
98
100
|
*
|
|
99
101
|
* CRITICAL (ADR-0036 decision 5, probed on PGlite 0.5.4): memory selection is ALWAYS the scheme-selected
|
|
100
|
-
* `memory://` form, NEVER
|
|
102
|
+
* `memory://` form, NEVER pgwasm's explicit `fs: new MemoryFS()` option — `dumpDataDir` from an explicit-`fs`
|
|
101
103
|
* instance silently omits relation files created after initdb, so a restored clone raises "relation does not
|
|
102
104
|
* exist". Callers that need a memory store must route through this function, never construct `MemoryFS`.
|
|
103
105
|
*
|
|
104
106
|
* ADR-0049 (D1) adds the `opfs://<storePath>` form: when the placement probe granted a sync-access handle in
|
|
105
107
|
* the executing scope (`env.hasOpfsSyncAccess`), the browser store lives on `opfs-repacked`. Precedence:
|
|
106
108
|
* memory override (test/ephemeral) → `opfs://` (probe granted) → `idb://` (browser, handle denied) →
|
|
107
|
-
* `file://` (Bun/Node). `opfs://` is TOOLKIT-INTERNAL plumbing —
|
|
108
|
-
* {@link
|
|
109
|
+
* `file://` (Bun/Node). `opfs://` is TOOLKIT-INTERNAL plumbing — pgwasm does NOT accept it as a `dataDir`;
|
|
110
|
+
* {@link createPgwasmClient} (plan step 10) interprets it via the opfs-repacked factory + the OPFS namespace
|
|
109
111
|
* builders below. Like every URL resolved here, it never leaks to consumers.
|
|
110
112
|
*/
|
|
111
113
|
export declare function resolveStoreDataDir(storePath: string, backendOverride?: "memory", env?: StoreEnv): string;
|
|
@@ -132,8 +134,8 @@ export declare function storeIdentityComponent(storePath: string): string;
|
|
|
132
134
|
* The toolkit-owned OPFS container at the root (ADR-0049 D6). Its child namespaces (`stores`, `commitments`,
|
|
133
135
|
* `probe`) are DISJOINT so two valid identities can never contend for one OPFS entry (e.g. store `"foo"`'s
|
|
134
136
|
* commitment sentinel vs a store literally named `"foo.committed"`). This module is the ONLY place that knows
|
|
135
|
-
* these names — the same rule that keeps {@link storeIndexedDbDatabaseName} the sole
|
|
136
|
-
*
|
|
137
|
+
* these names — the same rule that keeps {@link storeIndexedDbDatabaseName} the client's sole route to the
|
|
138
|
+
* IndexedDB database names.
|
|
137
139
|
*/
|
|
138
140
|
export declare const OPFS_TOOLKIT_CONTAINER = "pgxsinkit";
|
|
139
141
|
/**
|
|
@@ -148,6 +150,13 @@ export declare function opfsStoreDirectoryPath(storePath: string): readonly [str
|
|
|
148
150
|
* string, for the same `getDirectoryHandle`-chain reason.
|
|
149
151
|
*/
|
|
150
152
|
export declare function opfsCommitmentSentinelPath(storePath: string): readonly [string, string, string];
|
|
153
|
+
/**
|
|
154
|
+
* The Web Lock name of a store's OPFS OWNERSHIP (`store-ownership-lock.ts`): held EXCLUSIVELY by the context
|
|
155
|
+
* that holds the store directory's sync-access handles, from before it opens them until after it closes them,
|
|
156
|
+
* and taken by anything that deletes the directory. Keyed on the same injective identity as the OPFS paths, so
|
|
157
|
+
* two valid identities never share a lock.
|
|
158
|
+
*/
|
|
159
|
+
export declare function storeOwnershipLockName(storePath: string): string;
|
|
151
160
|
/**
|
|
152
161
|
* OPFS path (segment array) of the placement-probe scratch namespace: `pgxsinkit/probe` (ADR-0049 D6). No
|
|
153
162
|
* identity — the probe is per-SharedWorker-scope, not per-store. Segments, not a joined string.
|
|
@@ -155,22 +164,25 @@ export declare function opfsCommitmentSentinelPath(storePath: string): readonly
|
|
|
155
164
|
export declare function opfsProbeDirectoryPath(): readonly [string, string];
|
|
156
165
|
/**
|
|
157
166
|
* The IndexedDB database name a browser store occupies (ADR-0036) — a browser-only OPERATIONAL helper for
|
|
158
|
-
* orphan GC / corrupt-store deletion (`indexedDB.deleteDatabase(...)`), NOT part of the create path.
|
|
159
|
-
* maps `idb://<storePath>` to the IndexedDB database `/
|
|
160
|
-
* joined with the path after the scheme
|
|
161
|
-
* consumer that GCs its own stores routes that
|
|
162
|
-
* than re-deriving the
|
|
167
|
+
* orphan GC / corrupt-store deletion (`indexedDB.deleteDatabase(...)`), NOT part of the create path. pgwasm
|
|
168
|
+
* maps `idb://<storePath>` to the IndexedDB database `/pgwasm/<storePath>` (the IDBFS mount point: its root
|
|
169
|
+
* `/pgwasm` joined with the path after the scheme), and the name comes from `@pgxsinkit/pgwasm-c`'s own
|
|
170
|
+
* `idbDatabaseName`, never re-encoded here. Exposed so a consumer that GCs its own stores routes that
|
|
171
|
+
* pgwasm-internal naming knowledge through the library rather than re-deriving the prefix itself. Rejects a scheme-bearing/empty path exactly as
|
|
163
172
|
* {@link resolveStoreDataDir} does, so the two stay in lockstep.
|
|
164
173
|
*/
|
|
165
174
|
export declare function storeIndexedDbDatabaseName(storePath: string): string;
|
|
175
|
+
/** The provably-non-persistent storage shapes {@link classifyNonPersistentStorage} reports. */
|
|
176
|
+
export type NonPersistentStorage = "memory" | "non-persistent-vfs";
|
|
166
177
|
/**
|
|
167
|
-
* Classify a
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
178
|
+
* Classify a store's own `pg.storage` for the BYO refusal (ADR-0036 decision 4). Returns the offending shape
|
|
179
|
+
* when the store is PROVABLY non-persistent (`memory`, pgwasm's default or an explicit `memory://`; a `vfs`
|
|
180
|
+
* filesystem explicitly reporting `persistent: false`), or `null` when it passes: `idb`, `file`, a persistent
|
|
181
|
+
* `vfs` (the OPFS-repacked store) and a `vfs` that doesn't declare its persistence (a custom filesystem is
|
|
182
|
+
* the caller's own call). The guard catches the accidental non-persistent shapes; it is not a
|
|
183
|
+
* storage-backend whitelist.
|
|
172
184
|
*/
|
|
173
|
-
export declare function
|
|
185
|
+
export declare function classifyNonPersistentStorage(storage: StorageDescription): NonPersistentStorage | null;
|
|
174
186
|
/**
|
|
175
187
|
* Does either backend a browser boot could own for a plain {@link storePath} ALREADY exist? The fresh-target
|
|
176
188
|
* gate for a restore (ADR-0035 decision 6) — restore refuses a target that is already there. Per backend:
|
|
@@ -181,7 +193,7 @@ export declare function classifyNonPersistentDataDir(dataDir: string | undefined
|
|
|
181
193
|
* - **`file://`** (Bun/Node) — filesystem existence of the datadir directory. `node:fs` is imported
|
|
182
194
|
* DYNAMICALLY inside this branch, never at module top level, so a browser bundle (which only ever hits the
|
|
183
195
|
* `idb://` branch below) never pulls `node:fs` in. Relative paths resolve against the working directory,
|
|
184
|
-
* exactly as
|
|
196
|
+
* exactly as pgwasm's filesystem backend and {@link resolveStoreDataDir} do.
|
|
185
197
|
* - **`opfs://`** (browser, placement probe granted) — existence of the store DIRECTORY at
|
|
186
198
|
* `pgxsinkit/stores/<identity>` ({@link opfsStoreDirectoryPath}), walked via
|
|
187
199
|
* `navigator.storage.getDirectory()` and a `getDirectoryHandle` chain with `{ create: false }`. This is
|
|
@@ -196,7 +208,7 @@ export declare function classifyNonPersistentDataDir(dataDir: string | undefined
|
|
|
196
208
|
* ({@link storeIndexedDbDatabaseName}). BEST-EFFORT: `databases()` is unavailable on some engines (older
|
|
197
209
|
* Firefox, certain worker contexts); when absent we CANNOT prove existence, so we report `false` and let
|
|
198
210
|
* the restore proceed rather than fabricate a result — a real overlay collision would still surface as a
|
|
199
|
-
*
|
|
211
|
+
* pgwasm-level boot failure. We never fake a positive.
|
|
200
212
|
*/
|
|
201
213
|
export declare function storeTargetExists(storePath: string, backendOverride?: "memory", env?: StoreEnv): Promise<boolean>;
|
|
202
214
|
//# sourceMappingURL=store-path.d.ts.map
|
package/dist/store-path.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store-path.d.ts","sourceRoot":"","sources":["../src/store-path.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"store-path.d.ts","sourceRoot":"","sources":["../src/store-path.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAyB5D;;;;;GAKG;AACH,qBAAa,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;AAED;;;;;;;GAOG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;IAChD,YAAY,QAAQ,EAAE,oBAAoB,EAazC;CACF;AAED;;;;;;;GAOG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IACjD,YAAY,SAAS,EAAE,MAAM,EAQ5B;CACF;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,EAAE,OAAO,MAA0D,CAAC;AAEnG;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,cAAc,CAAC;AAExD,2GAA2G;AAC3G,MAAM,MAAM,mBAAmB,GAAG;IAAE,CAAC,kBAAkB,CAAC,CAAC,EAAE,eAAe,CAAA;CAAE,CAAC;AAE7E,oGAAoG;AACpG,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,eAAe,GAAG,SAAS,CAIjF;AAED;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,CAAC;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,mBAAmB,CAAC,CAAC;AAEpF,+GAA+G;AAC/G,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,cAAc,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,QAAQ,CAAA;CAAE,CAMhH;AAED;;;;;;GAMG;AACH,MAAM,WAAW,QAAQ;IACvB,YAAY,EAAE,OAAO,CAAC;IACtB;;;;;;;OAOG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AA+BD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,EACjB,eAAe,CAAC,EAAE,QAAQ,EAC1B,GAAG,GAAE,QAA2B,GAC/B,MAAM,CAKR;AAUD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CA8BhE;AAED;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,cAAc,CAAC;AAElD;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAE3F;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAE/F;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,IAAI,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAElE;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAYpE;AAED,+FAA+F;AAC/F,MAAM,MAAM,oBAAoB,GAAG,QAAQ,GAAG,oBAAoB,CAAC;AAEnE;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,kBAAkB,GAAG,oBAAoB,GAAG,IAAI,CAIrG;AAWD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,iBAAiB,CACrC,SAAS,EAAE,MAAM,EACjB,eAAe,CAAC,EAAE,QAAQ,EAC1B,GAAG,GAAE,QAA2B,GAC/B,OAAO,CAAC,OAAO,CAAC,CA2ClB"}
|
package/dist/sync/apply.d.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
1
|
+
import { type SyncChange } from "@pgxsinkit/contracts";
|
|
2
|
+
import type { Pgwasm, Transaction } from "@pgxsinkit/pgwasm";
|
|
3
3
|
import type { ApplyTarget } from "../local-tables";
|
|
4
|
-
import type {
|
|
4
|
+
import type { UpsertChangeMessage } from "./types";
|
|
5
5
|
export interface ApplyMessageToTableOptions {
|
|
6
|
-
pg:
|
|
6
|
+
pg: Pgwasm | Transaction;
|
|
7
7
|
target: ApplyTarget;
|
|
8
|
-
message:
|
|
8
|
+
message: SyncChange;
|
|
9
9
|
debug: boolean;
|
|
10
10
|
}
|
|
11
|
-
export declare function applyMessageToTable({ pg, target, message, debug }: ApplyMessageToTableOptions): Promise<import("@
|
|
11
|
+
export declare function applyMessageToTable({ pg, target, message, debug }: ApplyMessageToTableOptions): Promise<import("@pgxsinkit/pgwasm").Results<never>>;
|
|
12
12
|
export interface BulkApplyMessagesToTableOptions {
|
|
13
|
-
pg:
|
|
13
|
+
pg: Pgwasm | Transaction;
|
|
14
14
|
target: ApplyTarget;
|
|
15
|
-
messages:
|
|
15
|
+
messages: UpsertChangeMessage[];
|
|
16
16
|
debug: boolean;
|
|
17
17
|
}
|
|
18
18
|
export declare function applyInsertsToTable({ pg, target, messages, debug }: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
19
19
|
/**
|
|
20
|
-
* Bulk **upsert** for
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* for a pk-only table). The plain
|
|
25
|
-
*
|
|
26
|
-
*
|
|
20
|
+
* Bulk **upsert** — the applier for the wire's `upsert` verb (ADR-0058 decision 1). The engine states a
|
|
21
|
+
* row's new value rather than claiming anything about what preceded it, so an upsert routinely names a
|
|
22
|
+
* row already present locally: a subquery flip, a scope grant, a backfill replay, or a re-delivery on a
|
|
23
|
+
* resume from before its offset. So it is applied idempotently: `INSERT … ON CONFLICT (pk) DO UPDATE`
|
|
24
|
+
* refreshes to the authoritative value (or `DO NOTHING` for a pk-only table). The plain `INSERT` of the
|
|
25
|
+
* backfill path ({@link applyInsertsToTable}) is intentionally NOT used here, because a present row is
|
|
26
|
+
* expected, not a bug. Batched by parameter count (steady-state volume is incremental, not a backfill).
|
|
27
27
|
*/
|
|
28
28
|
export declare function applyUpsertsToTable({ pg, target, messages, debug }: BulkKeyedApplyOptions): Promise<void>;
|
|
29
29
|
export declare function applyMessagesToTableWithJson(options: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
@@ -40,10 +40,10 @@ export declare function applyMessagesToTableWithJson(options: BulkApplyMessagesT
|
|
|
40
40
|
export declare function applyUpsertsToTableWithJson(options: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
41
41
|
export declare function applyMessagesToTableWithCopy({ pg, target, messages, debug }: BulkApplyMessagesToTableOptions): Promise<void>;
|
|
42
42
|
export interface BulkKeyedApplyOptions {
|
|
43
|
-
pg:
|
|
43
|
+
pg: Pgwasm | Transaction;
|
|
44
44
|
target: ApplyTarget;
|
|
45
45
|
/** Folded messages: for deletes the value carries the PK; for updates the PK plus merged columns. */
|
|
46
|
-
messages:
|
|
46
|
+
messages: SyncChange[];
|
|
47
47
|
debug: boolean;
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
@@ -51,12 +51,4 @@ export interface BulkKeyedApplyOptions {
|
|
|
51
51
|
* because the read-path fold already left **one row per PK** — no same-PK duplicate in the source.
|
|
52
52
|
*/
|
|
53
53
|
export declare function applyBulkDeletesToTable({ pg, target, messages, debug }: BulkKeyedApplyOptions): Promise<void>;
|
|
54
|
-
/**
|
|
55
|
-
* Bulk UPDATE via `UPDATE … FROM json_to_recordset(…)` (ADR-0014 Phase 3), **grouped by the set of
|
|
56
|
-
* non-PK columns** each row carries. Electric's default replica sends only the changed columns, so
|
|
57
|
-
* two rows in one batch can set different columns; one `UPDATE … FROM` per distinct column-set keeps
|
|
58
|
-
* each statement uniform. Safe from the same-PK join hazard because the fold already left one row per
|
|
59
|
-
* PK, so no PK appears twice within a group.
|
|
60
|
-
*/
|
|
61
|
-
export declare function applyBulkUpdatesToTable({ pg, target, messages, debug }: BulkKeyedApplyOptions): Promise<void>;
|
|
62
54
|
//# sourceMappingURL=apply.d.ts.map
|
package/dist/sync/apply.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../../src/sync/apply.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../../src/sync/apply.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgC,KAAK,UAAU,EAAqC,MAAM,sBAAsB,CAAC;AAGxH,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGnD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AA4EnD,MAAM,WAAW,0BAA0B;IACzC,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,OAAO,EAAE,UAAU,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,0BAA0B,uDA0BnG;AAED,MAAM,WAAW,+BAA+B;IAC9C,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,mBAAmB,EAAE,CAAC;IAChC,KAAK,EAAE,OAAO,CAAC;CAChB;AAsHD,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,+BAA+B,iBA8CzG;AAyDD;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,qBAAqB,iBA+B/F;AAgGD,wBAAsB,4BAA4B,CAAC,OAAO,EAAE,+BAA+B,iBAE1F;AAED;;;;;;;;;GASG;AACH,wBAAsB,2BAA2B,CAAC,OAAO,EAAE,+BAA+B,iBAKzF;AAoBD,wBAAsB,4BAA4B,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,+BAA+B,iBA4BlH;AAqBD,MAAM,WAAW,qBAAqB;IACpC,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,qGAAqG;IACrG,QAAQ,EAAE,UAAU,EAAE,CAAC;IACvB,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,qBAAqB,iBA+BnG"}
|
package/dist/sync/copy.d.ts
CHANGED
|
@@ -2,14 +2,22 @@
|
|
|
2
2
|
* Serialization of JavaScript values into a PostgreSQL `COPY ... WITH (FORMAT
|
|
3
3
|
* text)` stream.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* `
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
5
|
+
* Values reach this module already decoded, so the runtime types it accepts are
|
|
6
|
+
* the decoded ones: `int2`/`int4`/`float4`/`float8` as `number`, `bool` as
|
|
7
|
+
* `boolean`, `json`/`jsonb` as parsed objects/arrays/scalars, `int8` as
|
|
8
|
+
* `bigint`, and every other type — **array columns included** — as its raw
|
|
9
|
+
* Postgres text representation (a `string`). To feed those values back into
|
|
10
|
+
* `COPY` we have to reverse that: turn each value into the exact text Postgres'
|
|
11
|
+
* input functions expect, then apply the COPY framing.
|
|
12
|
+
*
|
|
13
|
+
* Only the json ones are decoded on the way in, and by exactly one step:
|
|
14
|
+
* `envelopeToChange`'s `decodeJsonColumns` (`../circuits/envelope-to-change`).
|
|
15
|
+
* The wire itself carries EVERY non-int/float/bool cell as Postgres output text
|
|
16
|
+
* — a `jsonb` column included — so without that step the `JSON.stringify` below
|
|
17
|
+
* would encode already-encoded JSON a second time and the column would land as a
|
|
18
|
+
* JSON string scalar. An array column's `array_out` text is deliberately left
|
|
19
|
+
* alone: it passes through {@link valueToText} unchanged and `COPY`'s `array_in`
|
|
20
|
+
* parses it, which is why a `json[]` value here is a `string`, not a JS array.
|
|
13
21
|
*
|
|
14
22
|
* Rather than invent an escaping scheme (the previous CSV-based approach broke
|
|
15
23
|
* on arrays, JSON, embedded delimiters, etc.) this is a faithful port of the
|
|
@@ -22,6 +30,8 @@
|
|
|
22
30
|
* internally, has a single well-defined escaping algorithm, and round-trips
|
|
23
31
|
* every built-in type.
|
|
24
32
|
*/
|
|
33
|
+
import { type AnyPgTable } from "drizzle-orm/pg-core";
|
|
34
|
+
import type { RawStatement } from "../index";
|
|
25
35
|
/**
|
|
26
36
|
* Serialize a single value into one COPY TEXT field. `null`/`undefined` become
|
|
27
37
|
* the NULL marker (`\N`); everything else is converted to text and escaped.
|
|
@@ -29,6 +39,14 @@
|
|
|
29
39
|
* `udtName` is the column's Postgres `udt_name`. When supplied it disambiguates
|
|
30
40
|
* `json`/`jsonb` columns; when omitted the value's runtime type is used (which
|
|
31
41
|
* cannot tell a `jsonb` array from a SQL array).
|
|
42
|
+
*
|
|
43
|
+
* PUBLIC (re-exported from the package root) so an app that bulk-loads a table
|
|
44
|
+
* pgxsinkit does not manage writes the SAME COPY TEXT the applier does. The
|
|
45
|
+
* value contract is the one this module's header states: numbers/booleans/
|
|
46
|
+
* bigints as JS primitives, `json`/`jsonb` as **parsed** values (an object, an
|
|
47
|
+
* array, or a scalar — never pre-stringified JSON, which would land as a JSON
|
|
48
|
+
* string), a `Date` or its text for the temporal types, a JS array (or the
|
|
49
|
+
* Postgres array text) for an array column, and every other type as a `string`.
|
|
32
50
|
*/
|
|
33
51
|
export declare function serializeCopyValue(value: unknown, udtName?: string): string;
|
|
34
52
|
/**
|
|
@@ -38,6 +56,64 @@ export declare function serializeCopyValue(value: unknown, udtName?: string): st
|
|
|
38
56
|
*
|
|
39
57
|
* `columnTypes` optionally maps a column name to its Postgres `udt_name`; pass
|
|
40
58
|
* it so `json`/`jsonb` columns serialize correctly.
|
|
59
|
+
*
|
|
60
|
+
* PUBLIC (re-exported from the package root) for an app-owned, pgxsinkit-unmanaged
|
|
61
|
+
* table. Two contracts bind the caller:
|
|
62
|
+
*
|
|
63
|
+
* - **`columns` IS the column list of the COPY statement.** The fields are
|
|
64
|
+
* emitted in exactly this order, so the same array must be named — in the
|
|
65
|
+
* same order — in the `COPY <table> (...)` that ingests the bytes.
|
|
66
|
+
* {@link buildCopyFromBlobStatement} renders both from one array so they
|
|
67
|
+
* cannot drift.
|
|
68
|
+
* - **`columnTypes` keys are DB column names, values are Postgres `udt_name`s**
|
|
69
|
+
* (`text`, `int4`, `jsonb`, `timestamptz`, `_text` for `text[]`, …). Only the
|
|
70
|
+
* json ones (`json`/`jsonb`/`_json`/`_jsonb`) change behaviour — everything
|
|
71
|
+
* else dispatches on the value's runtime type — but passing the full map is
|
|
72
|
+
* harmless. A `json`/`jsonb` value MUST be the parsed value, per
|
|
73
|
+
* {@link serializeCopyValue}.
|
|
41
74
|
*/
|
|
42
75
|
export declare function generateCopyData(rows: ReadonlyArray<Record<string, unknown>>, columns: ReadonlyArray<string>, columnTypes?: Readonly<Record<string, string | undefined>>): string;
|
|
76
|
+
/** The inputs {@link buildCopyFromBlobStatement} renders a COPY-from-blob statement from. */
|
|
77
|
+
export interface CopyFromBlobStatementOptions {
|
|
78
|
+
/**
|
|
79
|
+
* The REAL Drizzle table object to load into — the identifier is read off it with `getTableConfig`,
|
|
80
|
+
* never taken as a hand-written string, so a rename can never leave a stale name in the SQL. A bare
|
|
81
|
+
* (schemaless) table renders unqualified and resolves through `search_path` (that is how the applier
|
|
82
|
+
* reaches an ephemeral relation in `pg_temp`).
|
|
83
|
+
*/
|
|
84
|
+
table: AnyPgTable;
|
|
85
|
+
/**
|
|
86
|
+
* The DB column names to load, in order. ONE array drives both halves of the statement: the COPY
|
|
87
|
+
* column list and the field order of the serialized body, so they cannot drift.
|
|
88
|
+
*/
|
|
89
|
+
columns: readonly string[];
|
|
90
|
+
/** The rows to load, keyed by DB column name (a key missing from a row is loaded as NULL). */
|
|
91
|
+
rows: ReadonlyArray<Record<string, unknown>>;
|
|
92
|
+
/**
|
|
93
|
+
* DB column name → Postgres `udt_name`, per {@link generateCopyData}. Needed for `json`/`jsonb`
|
|
94
|
+
* (and `_json`/`_jsonb`) columns; ignored for every other type.
|
|
95
|
+
*/
|
|
96
|
+
udtNames?: Readonly<Record<string, string | undefined>>;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Render the `COPY <table> (<columns>) FROM '/dev/blob' WITH (FORMAT text)` statement for `rows`,
|
|
100
|
+
* together with the COPY TEXT bytes pgwasm must read for it — a {@link RawStatement} whose `blob`
|
|
101
|
+
* carries the body (narrowed to a non-optional `blob`: this builder ALWAYS produces one).
|
|
102
|
+
*
|
|
103
|
+
* This is the ONE implementation of the COPY-from-blob load: the sync applier's bulk path
|
|
104
|
+
* (`applyMessagesToTableWithCopy`) and the public raw seam both call it, so an app-owned table
|
|
105
|
+
* bulk-loads on exactly the contract the applier is tested against.
|
|
106
|
+
*
|
|
107
|
+
* Hand it to `rawTransaction` (to bulk-load inside one local transaction, e.g. delete-then-COPY) or
|
|
108
|
+
* to `rawQuery`'s `blob` option for a single statement. On a worker-attached client the bytes are
|
|
109
|
+
* TRANSFERRED, not copied — see {@link RawStatement.blob}: the buffer is detached after the call.
|
|
110
|
+
*
|
|
111
|
+
* Tier ③ (ADR-0028 allow-list), exactly as the applier's copy of this statement was: `COPY … FROM
|
|
112
|
+
* '/dev/blob'` is pgwasm's blob-ingest grammar and has NO Drizzle builder form, so the statement text
|
|
113
|
+
* stays a raw string. Every identifier in it is still derived — the table from `getTableConfig`, each
|
|
114
|
+
* column through `quoteIdentifier` — so nothing in the string is a hand-written identifier.
|
|
115
|
+
*/
|
|
116
|
+
export declare function buildCopyFromBlobStatement({ table, columns, rows, udtNames, }: CopyFromBlobStatementOptions): RawStatement & {
|
|
117
|
+
blob: Uint8Array<ArrayBuffer>;
|
|
118
|
+
};
|
|
43
119
|
//# sourceMappingURL=copy.d.ts.map
|
package/dist/sync/copy.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copy.d.ts","sourceRoot":"","sources":["../../src/sync/copy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"copy.d.ts","sourceRoot":"","sources":["../../src/sync/copy.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,qBAAqB,CAAC;AAItE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAkJ7C;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAO3E;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;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;AAKD,6FAA6F;AAC7F,MAAM,WAAW,4BAA4B;IAC3C;;;;;OAKG;IACH,KAAK,EAAE,UAAU,CAAC;IAClB;;;OAGG;IACH,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3B,8FAA8F;IAC9F,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAC7C;;;OAGG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;CACzD;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,KAAK,EACL,OAAO,EACP,IAAI,EACJ,QAAQ,GACT,EAAE,4BAA4B,GAAG,YAAY,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,CAAA;CAAE,CAQjF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { type
|
|
3
|
-
/** A (memoized) Drizzle handle over an open
|
|
4
|
-
export declare function drizzleOverPg(pg:
|
|
1
|
+
import type { Pgwasm, Transaction } from "@pgxsinkit/pgwasm";
|
|
2
|
+
import { type PgwasmDatabase } from "@pgxsinkit/pgwasm/drizzle";
|
|
3
|
+
/** A (memoized) Drizzle handle over an open pgwasm connection or transaction — engine-internal. */
|
|
4
|
+
export declare function drizzleOverPg(pg: Pgwasm | Transaction): PgwasmDatabase<never>;
|
|
5
5
|
//# sourceMappingURL=drizzle-executor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drizzle-executor.d.ts","sourceRoot":"","sources":["../../src/sync/drizzle-executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"drizzle-executor.d.ts","sourceRoot":"","sources":["../../src/sync/drizzle-executor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAqCzE,mGAAmG;AACnG,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW,GAAG,cAAc,CAAC,KAAK,CAAC,CAO7E"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SyncChange } from "@pgxsinkit/contracts";
|
|
2
|
+
import type { UpsertChangeMessage } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* A drained shape batch folded to **one net operation per primary key** (ADR-0014 decision 1).
|
|
5
|
+
* Each PK appears in at most one of `deletes`/`upserts` — except a *re-created* PK
|
|
6
|
+
* (`[delete, … , upsert]`), which appears in both so the pre-existing row is cleared before the new
|
|
7
|
+
* one lands. The Sync applier therefore runs two bulk statements **in the order `deletes → upserts`**:
|
|
8
|
+
* that ordering is what makes a re-create drop local-only column state instead of inheriting it, and
|
|
9
|
+
* every other PK is touched by exactly one statement.
|
|
10
|
+
*/
|
|
11
|
+
export interface FoldedShapeBatch {
|
|
12
|
+
/** PKs to delete — net-`delete` keys, plus the clearing delete of each re-created key. */
|
|
13
|
+
deletes: SyncChange[];
|
|
14
|
+
/** Full rows to upsert — net-`upsert` keys, plus each re-created key's new row. */
|
|
15
|
+
upserts: UpsertChangeMessage[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Fold a drained shape batch (ordered by LSN, one shape) to one net operation per primary key
|
|
19
|
+
* (ADR-0014 decision 1), so the read path can bulk-apply without the `INSERT … SELECT
|
|
20
|
+
* json_to_recordset(…)` same-PK join hazard (which uses **one arbitrary** matching row when the
|
|
21
|
+
* source holds duplicate PKs). Each PK's ops are replayed in stream order down to:
|
|
22
|
+
*
|
|
23
|
+
* - **trailing `delete`** ⇒ a single DELETE;
|
|
24
|
+
* - **upserts, no preceding delete** ⇒ a single UPSERT of the net row;
|
|
25
|
+
* - **re-created** (`[delete, … , upsert, …]`, no trailing delete) ⇒ DELETE **and** UPSERT.
|
|
26
|
+
*
|
|
27
|
+
* The clearing DELETE on a re-create is not redundant against an upsert. `ON CONFLICT DO UPDATE`
|
|
28
|
+
* refreshes only the columns the shape projects; a row that genuinely left and re-entered must not
|
|
29
|
+
* keep local-only column values from its previous life, so it is removed first.
|
|
30
|
+
*
|
|
31
|
+
* There is no `updates` bucket and no malformed-batch case, because there is no third verb: the
|
|
32
|
+
* engine emits `upsert | delete` and nothing else ({@link SyncOperation}). The old
|
|
33
|
+
* "update after delete ⇒ throw" guard checked for a stream state the vocabulary can no longer
|
|
34
|
+
* express.
|
|
35
|
+
*
|
|
36
|
+
* Pure and dependency-free by design (ADR-0014 / ISS-06): property-tested against the oracle
|
|
37
|
+
* *fold-then-bulk ≡ ordered per-row apply* over random same-PK sequences and random initial state.
|
|
38
|
+
*/
|
|
39
|
+
export declare function foldChangeBatch(messages: SyncChange[]): FoldedShapeBatch;
|
|
40
|
+
//# sourceMappingURL=fold.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fold.d.ts","sourceRoot":"","sources":["../../src/sync/fold.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAW,MAAM,sBAAsB,CAAC;AAEhE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B,0FAA0F;IAC1F,OAAO,EAAE,UAAU,EAAE,CAAC;IACtB,mFAAmF;IACnF,OAAO,EAAE,mBAAmB,EAAE,CAAC;CAChC;AAkBD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,UAAU,EAAE,GAAG,gBAAgB,CAgBxE"}
|
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
import { pgSchema } from "drizzle-orm/pg-core";
|
|
2
2
|
import type { ShapeSubscriptionState } from "./subscription-state";
|
|
3
|
+
/**
|
|
4
|
+
* The pgxsinkit-owned metadata schema (ADR-0009 decision 6) — where the subscription cursor and the
|
|
5
|
+
* sync-origin GUC live. Ours, not the sync substrate's, which is why it is not named after one.
|
|
6
|
+
*
|
|
7
|
+
* The sync engine defaults its `metadataSchema` to this and `createSyncClient` never overrides it, so
|
|
8
|
+
* paths outside the engine can address the same relations by this constant.
|
|
9
|
+
*/
|
|
10
|
+
export declare const DEFAULT_METADATA_SCHEMA = "pgxsinkit";
|
|
11
|
+
/**
|
|
12
|
+
* Validate the metadata schema name at engine construction. The name is interpolated RAW into two
|
|
13
|
+
* positions a double-quoted identifier cannot cover:
|
|
14
|
+
* - GUC space — `SET LOCAL <schema>.syncing` (sync/index.ts) and `SET <schema>.syncing = false`
|
|
15
|
+
* (the metadata migration). GUC grammar takes a `namespace.name` of bare identifiers; it does not
|
|
16
|
+
* accept a quoted identifier the way a table/column position does, so the name cannot be safely
|
|
17
|
+
* quoted there.
|
|
18
|
+
* - the `CREATE SCHEMA <schema>` DDL identifier position.
|
|
19
|
+
* Rather than trust arbitrary caller input in those raw positions, restrict the name to a bare
|
|
20
|
+
* lowercase identifier and reject uppercase/exotic names outright. The default "pgxsinkit" passes.
|
|
21
|
+
*/
|
|
22
|
+
export declare function assertValidMetadataSchema(metadataSchema: string): void;
|
|
3
23
|
/**
|
|
4
24
|
* The two metadata-store relations the sync engine provisions at boot (ADR-0028 decision 4). The
|
|
5
25
|
* metadata schema name is dynamic (per-client config) but fixed at engine construction, so the tables
|
|
@@ -18,7 +38,11 @@ import type { ShapeSubscriptionState } from "./subscription-state";
|
|
|
18
38
|
export interface MetadataTables {
|
|
19
39
|
/** `<schema>.subscriptions_metadata` — one row per subscription key. */
|
|
20
40
|
readonly subscriptionsMetadata: ReturnType<typeof buildSubscriptionsMetadata>;
|
|
21
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* `<schema>.shape_row_tags` — the tagged-subquery reason-set store (ADR-0023), whose mechanism
|
|
43
|
+
* ADR-0057 retired. Still provisioned by `migrateSubscriptionMetadataTables`; nothing reads or
|
|
44
|
+
* writes it on the native path.
|
|
45
|
+
*/
|
|
22
46
|
readonly shapeRowTags: ReturnType<typeof buildShapeRowTags>;
|
|
23
47
|
}
|
|
24
48
|
declare function buildSubscriptionsMetadata(schema: ReturnType<typeof pgSchema>): import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata-tables.d.ts","sourceRoot":"","sources":["../../src/sync/metadata-tables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,QAAQ,EAAoB,MAAM,qBAAqB,CAAC;AAE/E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAEnE;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,cAAc;IAC7B,wEAAwE;IACxE,QAAQ,CAAC,qBAAqB,EAAE,UAAU,CAAC,OAAO,0BAA0B,CAAC,CAAC;IAC9E
|
|
1
|
+
{"version":3,"file":"metadata-tables.d.ts","sourceRoot":"","sources":["../../src/sync/metadata-tables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,QAAQ,EAAoB,MAAM,qBAAqB,CAAC;AAE/E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAEnE;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,cAAc,CAAC;AAQnD;;;;;;;;;;GAUG;AACH,wBAAgB,yBAAyB,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI,CAStE;AAED;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,cAAc;IAC7B,wEAAwE;IACxE,QAAQ,CAAC,qBAAqB,EAAE,UAAU,CAAC,OAAO,0BAA0B,CAAC,CAAC;IAC9E;;;;OAIG;IACH,QAAQ,CAAC,YAAY,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC;CAC7D;AAYD,iBAAS,0BAA0B,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAQtE;AAED,iBAAS,iBAAiB,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAa7D;AAID,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,cAAc,EAAE,MAAM,GAAG,cAAc,CAWxE;AAID;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,IAAI,cAAc,CASzD;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,GAAG,cAAc,CAEjG"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
1
|
+
import { type PredicateValue } from "@pgxsinkit/contracts";
|
|
2
|
+
import type { Pgwasm, Transaction } from "@pgxsinkit/pgwasm";
|
|
3
3
|
import { type Lsn, type SubscriptionKey } from "./types";
|
|
4
4
|
export interface SubscriptionState {
|
|
5
5
|
key: SubscriptionKey;
|
|
@@ -8,10 +8,29 @@ export interface SubscriptionState {
|
|
|
8
8
|
}
|
|
9
9
|
export interface ShapeSubscriptionState {
|
|
10
10
|
handle: string;
|
|
11
|
-
|
|
11
|
+
/** Opaque, per-stream, lexicographically ordered. Never compared across streams. */
|
|
12
|
+
offset: string;
|
|
13
|
+
/**
|
|
14
|
+
* The registry shape this entry is a reader of.
|
|
15
|
+
*
|
|
16
|
+
* Persisted so a later subscribe can say what a stored entry WAS, not merely that it exists. The
|
|
17
|
+
* map key is a logical shape name the client mints (shape key plus scope), which is enough to match
|
|
18
|
+
* an entry against a fresh grant but carries no way back to the registry — and the reconcile at
|
|
19
|
+
* subscribe (ADR-0055 decision 6) needs exactly that: a scope the control plane no longer grants has
|
|
20
|
+
* to be resolved to its spec before its rows can be cleared.
|
|
21
|
+
*/
|
|
22
|
+
shapeKey: string;
|
|
23
|
+
/**
|
|
24
|
+
* Shared tier only: the scope values this reader's grant named, in the shape's declared column
|
|
25
|
+
* order. Absent for the private tier, whose subject arrives in the claims instead.
|
|
26
|
+
*
|
|
27
|
+
* `readonly` because a persisted tuple is a record of what was granted, never something to edit —
|
|
28
|
+
* which also lets a grant's own `readonly PredicateValue[]` be stored without a defensive copy.
|
|
29
|
+
*/
|
|
30
|
+
scope?: readonly PredicateValue[];
|
|
12
31
|
}
|
|
13
32
|
export interface GetSubscriptionStateOptions {
|
|
14
|
-
readonly pg:
|
|
33
|
+
readonly pg: Pgwasm | Transaction;
|
|
15
34
|
readonly metadataSchema: string;
|
|
16
35
|
readonly subscriptionKey: SubscriptionKey;
|
|
17
36
|
/**
|
|
@@ -23,7 +42,7 @@ export interface GetSubscriptionStateOptions {
|
|
|
23
42
|
}
|
|
24
43
|
export declare function getSubscriptionState({ pg, metadataSchema, subscriptionKey, sessionScoped, }: GetSubscriptionStateOptions): Promise<SubscriptionState | null>;
|
|
25
44
|
export interface UpdateSubscriptionStateOptions {
|
|
26
|
-
pg:
|
|
45
|
+
pg: Pgwasm | Transaction;
|
|
27
46
|
metadataSchema: string;
|
|
28
47
|
subscriptionKey: SubscriptionKey;
|
|
29
48
|
shapeMetadata: Record<string, ShapeSubscriptionState>;
|
|
@@ -34,13 +53,13 @@ export interface UpdateSubscriptionStateOptions {
|
|
|
34
53
|
}
|
|
35
54
|
export declare function updateSubscriptionState({ pg, metadataSchema, subscriptionKey, shapeMetadata, lastLsn, debug, sessionScoped, }: UpdateSubscriptionStateOptions): Promise<void>;
|
|
36
55
|
export interface DeleteSubscriptionStateOptions {
|
|
37
|
-
pg:
|
|
56
|
+
pg: Pgwasm | Transaction;
|
|
38
57
|
metadataSchema: string;
|
|
39
58
|
subscriptionKey: SubscriptionKey;
|
|
40
59
|
}
|
|
41
60
|
export declare function deleteSubscriptionState({ pg, metadataSchema, subscriptionKey }: DeleteSubscriptionStateOptions): Promise<void>;
|
|
42
61
|
export interface MigrateSubscriptionMetadataTablesOptions {
|
|
43
|
-
pg:
|
|
62
|
+
pg: Pgwasm | Transaction;
|
|
44
63
|
metadataSchema: string;
|
|
45
64
|
}
|
|
46
65
|
export declare function migrateSubscriptionMetadataTables({ pg, metadataSchema, }: MigrateSubscriptionMetadataTablesOptions): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription-state.d.ts","sourceRoot":"","sources":["../../src/sync/subscription-state.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"subscription-state.d.ts","sourceRoot":"","sources":["../../src/sync/subscription-state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAmB,KAAK,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAG5E,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAK7D,OAAO,EAAE,KAAK,GAAG,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAC;AAEzD,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,eAAe,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IACvD,QAAQ,EAAE,GAAG,CAAC;CACf;AAED,MAAM,WAAW,sBAAsB;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,oFAAoF;IACpF,MAAM,EAAE,MAAM,CAAC;IACf;;;;;;;;OAQG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;CACnC;AAED,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IAClC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;IAC1C;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CAClC;AAED,wBAAsB,oBAAoB,CAAC,EACzC,EAAE,EACF,cAAc,EACd,eAAe,EACf,aAAqB,GACtB,EAAE,2BAA2B,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAiCjE;AAED,MAAM,WAAW,8BAA8B;IAC7C,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,eAAe,CAAC;IACjC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,CAAC;IACtD,OAAO,EAAE,GAAG,CAAC;IACb,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,4FAA4F;IAC5F,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,wBAAsB,uBAAuB,CAAC,EAC5C,EAAE,EACF,cAAc,EACd,eAAe,EACf,aAAa,EACb,OAAO,EACP,KAAK,EACL,aAAqB,GACtB,EAAE,8BAA8B,iBAchC;AAED,MAAM,WAAW,8BAA8B;IAC7C,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,eAAe,CAAC;CAClC;AAED,wBAAsB,uBAAuB,CAAC,EAAE,EAAE,EAAE,cAAc,EAAE,eAAe,EAAE,EAAE,8BAA8B,iBAUpH;AAED,MAAM,WAAW,wCAAwC;IACvD,EAAE,EAAE,MAAM,GAAG,WAAW,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,wBAAsB,iCAAiC,CAAC,EACtD,EAAE,EACF,cAAc,GACf,EAAE,wCAAwC,iBAsB1C"}
|