@catalyst-cloud/replicate 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export { applyDelta, truncateReplica, getCursor, setCursor } from "./replicate.js";
2
+ export type { ReplicaWriteDb, ReplicaChange, ToBindable } from "./replicate.js";
@@ -4,6 +4,4 @@
4
4
  // lands a change-feed record into a local SQLite replica over the portable `ReplicaWriteDb` handle, so
5
5
  // the IDENTICAL apply logic runs unchanged in the host-sync bun:sqlite replica and the browser OPFS
6
6
  // wasm replica — collapsing the two hand-maintained `apply.ts` twins into one source of truth.
7
-
8
- export { applyDelta, truncateReplica, getCursor, setCursor } from "./replicate";
9
- export type { ReplicaWriteDb, ReplicaChange, ToBindable } from "./replicate";
7
+ export { applyDelta, truncateReplica, getCursor, setCursor } from "./replicate.js";
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The portable replica WRITE seam — the write counterpart to read-model's `SqlExecutor`. bun:sqlite
3
+ * (host-sync) and sqlite-wasm (browser OPFS) each satisfy it via a thin adapter. Generic over the
4
+ * bindable type `B` because the two engines coerce binary/bigint differently (bun binds Uint8Array /
5
+ * bigint; wasm binds ArrayBuffer / number) — the runtime supplies its own `toBindable` so every bound
6
+ * value is already a `B`.
7
+ */
8
+ export interface ReplicaWriteDb<B> {
9
+ /** Execute a parameterized mutation; return the number of rows changed (sqlite3_changes()). */
10
+ run(sql: string, ...bindings: B[]): number;
11
+ /** Run a single-row query; return the first row as an object, or undefined. */
12
+ get(sql: string, ...bindings: B[]): Record<string, B> | undefined;
13
+ }
14
+ /** Coerce ONE wire JSON value to the runtime's bindable scalar `B` (booleans → 0/1, etc.). Runtime-
15
+ * specific (binary/bigint differ per engine), so it is injected rather than baked in. */
16
+ export type ToBindable<B> = (value: unknown) => B;
17
+ /** One change-feed record (a /snapshot line or a /changes row). accountId/seq are the caller's concern.
18
+ * `entity` is widened to string here (the runtime callers pass their stricter EntityName). */
19
+ export interface ReplicaChange {
20
+ entity: string;
21
+ op: "upsert" | "delete";
22
+ /** Full normalized row for "upsert"; for "delete" may be `{}` or carry just the PK columns. */
23
+ row: Record<string, unknown>;
24
+ /** The change_log.entity_id — the PK (composite PKs joined with ':'). Required to apply a delete. */
25
+ entityId?: string;
26
+ }
27
+ /**
28
+ * Apply ONE change-feed record to the replica. Returns true iff a row was actually written (a stale
29
+ * upsert rejected by the updated_at guard, or a delete of an already-absent row, returns false). Never
30
+ * throws on a well-formed record; throws only on an unknown entity (malformed wire data).
31
+ */
32
+ export declare function applyDelta<B>(db: ReplicaWriteDb<B>, change: ReplicaChange, toBindable: ToBindable<B>): boolean;
33
+ /**
34
+ * Wipe every replica entity table (but NOT the host-only `sync_meta` cursor table). Called before
35
+ * replaying a fresh /snapshot so a resync can't leave orphaned rows the new snapshot no longer contains.
36
+ */
37
+ export declare function truncateReplica<B>(db: ReplicaWriteDb<B>): void;
38
+ /** Read the persisted change-feed cursor (or null if a snapshot has never completed). */
39
+ export declare function getCursor<B>(db: ReplicaWriteDb<B>): number | null;
40
+ /** Persist the change-feed cursor (the last applied change_log.seq). */
41
+ export declare function setCursor<B>(db: ReplicaWriteDb<B>, cursor: number, toBindable: ToBindable<B>): void;
@@ -0,0 +1,112 @@
1
+ // @catalyst-cloud/replicate — the runtime-agnostic replica WRITE path (ADR-0002). The write
2
+ // counterpart to @catalyst-cloud/read-model: one `applyDelta` (+ cursor + truncate) that lands a
3
+ // change-feed record into a local SQLite replica, schema-driven from the one Drizzle SSOT
4
+ // (@catalyst-cloud/schema MIRROR_TABLE_META). The host-sync bun:sqlite replica and the browser OPFS
5
+ // wasm replica BOTH route through this — collapsing the two hand-maintained `apply.ts` twins into one,
6
+ // so they can never drift. Dependency-free / runtime-agnostic by design (NO bun:sqlite / node imports);
7
+ // the runtime engine adapts to the portable `ReplicaWriteDb` handle, exactly as the read-model's
8
+ // `SqlExecutor` is adapted per runtime.
9
+ //
10
+ // WIRE CONTRACT (shared — see apps/mirror/src/do/changefeed.ts):
11
+ // • op:"upsert" → row is the FULL normalized DO row; INSERT … ON CONFLICT(pk) DO UPDATE, last-write-
12
+ // wins by updated_at where present (DO NOTHING for a pure-join row like issue_labels).
13
+ // • op:"delete" → soft-delete (set removed_at) on tables with a removed_at column, else hard-delete;
14
+ // the PK is recovered from the row's PK columns or by splitting entityId on ':'.
15
+ import { MIRROR_TABLE_META } from "@catalyst-cloud/schema";
16
+ function quoteIdent(ident) {
17
+ return `"${ident.replace(/"/g, '""')}"`;
18
+ }
19
+ function metaFor(entity) {
20
+ return MIRROR_TABLE_META[entity];
21
+ }
22
+ /**
23
+ * Apply ONE change-feed record to the replica. Returns true iff a row was actually written (a stale
24
+ * upsert rejected by the updated_at guard, or a delete of an already-absent row, returns false). Never
25
+ * throws on a well-formed record; throws only on an unknown entity (malformed wire data).
26
+ */
27
+ export function applyDelta(db, change, toBindable) {
28
+ const meta = metaFor(change.entity);
29
+ if (!meta)
30
+ throw new Error(`applyDelta: unknown entity ${String(change.entity)}`);
31
+ if (change.op === "delete")
32
+ return applyDelete(db, change, meta, toBindable);
33
+ return applyUpsert(db, change, meta, toBindable);
34
+ }
35
+ function applyUpsert(db, change, meta, toBindable) {
36
+ const table = change.entity;
37
+ const cols = Object.keys(change.row);
38
+ if (cols.length === 0)
39
+ return false; // malformed wire upsert — skip rather than emit invalid SQL.
40
+ const pkSet = new Set(meta.pk);
41
+ const nonPkCols = cols.filter((c) => !pkSet.has(c));
42
+ const hasUpdatedAt = cols.includes("updated_at");
43
+ const colList = cols.map(quoteIdent).join(", ");
44
+ const placeholders = cols.map(() => "?").join(", ");
45
+ const conflictTarget = meta.pk.map(quoteIdent).join(", ");
46
+ let conflictClause;
47
+ if (nonPkCols.length === 0) {
48
+ // Pure join/edge row (issue_labels): PK is the whole row — no-op on conflict.
49
+ conflictClause = `ON CONFLICT(${conflictTarget}) DO NOTHING`;
50
+ }
51
+ else {
52
+ const setClause = nonPkCols
53
+ .map((c) => `${quoteIdent(c)} = excluded.${quoteIdent(c)}`)
54
+ .join(", ");
55
+ // Last-write-wins by updated_at — mirrors the DO's guard so out-of-order deltas can't regress.
56
+ const guard = hasUpdatedAt
57
+ ? ` WHERE excluded.updated_at > ${quoteIdent(table)}.updated_at`
58
+ : "";
59
+ conflictClause = `ON CONFLICT(${conflictTarget}) DO UPDATE SET ${setClause}${guard}`;
60
+ }
61
+ const sql = `INSERT INTO ${quoteIdent(table)} (${colList}) VALUES (${placeholders}) ${conflictClause}`;
62
+ return db.run(sql, ...cols.map((c) => toBindable(change.row[c]))) > 0;
63
+ }
64
+ function applyDelete(db, change, meta, toBindable) {
65
+ const table = change.entity;
66
+ const pkVals = pkValuesFor(change, meta, toBindable);
67
+ if (!pkVals)
68
+ return false; // can't locate the row → nothing to do.
69
+ const where = meta.pk.map((c) => `${quoteIdent(c)} = ?`).join(" AND ");
70
+ if (meta.softDelete) {
71
+ // Soft-delete: set removed_at, but only if currently live (idempotent re-delete is a no-op).
72
+ return (db.run(`UPDATE ${quoteIdent(table)} SET removed_at = ? WHERE ${where} AND removed_at IS NULL`, toBindable(Date.now()), ...pkVals) > 0);
73
+ }
74
+ // Hard-delete (join/edge/GitHub tables have no removed_at).
75
+ return db.run(`DELETE FROM ${quoteIdent(table)} WHERE ${where}`, ...pkVals) > 0;
76
+ }
77
+ /** Resolve the PK column values for a delete from the row's PK fields first, then entityId, else null.
78
+ * Every value is routed through `toBindable` so the result is uniformly `B[]`. */
79
+ function pkValuesFor(change, meta, toBindable) {
80
+ const fromRow = meta.pk.map((c) => change.row[c]);
81
+ if (fromRow.every((v) => v !== undefined && v !== null)) {
82
+ return fromRow.map(toBindable);
83
+ }
84
+ if (change.entityId != null) {
85
+ const parts = change.entityId.split(":");
86
+ if (parts.length === meta.pk.length)
87
+ return parts.map((p) => toBindable(p));
88
+ }
89
+ return null;
90
+ }
91
+ /**
92
+ * Wipe every replica entity table (but NOT the host-only `sync_meta` cursor table). Called before
93
+ * replaying a fresh /snapshot so a resync can't leave orphaned rows the new snapshot no longer contains.
94
+ */
95
+ export function truncateReplica(db) {
96
+ for (const entity of Object.keys(MIRROR_TABLE_META)) {
97
+ db.run(`DELETE FROM ${quoteIdent(entity)}`);
98
+ }
99
+ }
100
+ /** Read the persisted change-feed cursor (or null if a snapshot has never completed). */
101
+ export function getCursor(db) {
102
+ const row = db.get("SELECT value FROM sync_meta WHERE key = 'cursor'");
103
+ if (!row)
104
+ return null;
105
+ const n = Number(row["value"]);
106
+ return Number.isFinite(n) ? n : null;
107
+ }
108
+ /** Persist the change-feed cursor (the last applied change_log.seq). */
109
+ export function setCursor(db, cursor, toBindable) {
110
+ db.run("INSERT INTO sync_meta (key, value) VALUES (?, ?) " +
111
+ "ON CONFLICT(key) DO UPDATE SET value = excluded.value", toBindable("cursor"), toBindable(String(cursor)));
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@catalyst-cloud/replicate",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "description": "Runtime-agnostic replica write path — applyDelta / truncateReplica / cursor over a portable ReplicaWriteDb (ADR-0002). Shared by the host-sync bun:sqlite replica and the browser OPFS replica so the two apply paths can't drift.",
6
6
  "license": "MIT",
@@ -10,18 +10,23 @@
10
10
  "directory": "packages/replicate"
11
11
  },
12
12
  "files": [
13
- "src"
13
+ "dist"
14
14
  ],
15
15
  "publishConfig": {
16
16
  "access": "public"
17
17
  },
18
18
  "exports": {
19
- ".": "./src/index.ts"
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js",
22
+ "default": "./dist/index.js"
23
+ }
20
24
  },
21
25
  "scripts": {
22
26
  "typecheck": "tsc --noEmit",
23
27
  "test": "vitest run",
24
- "test:coverage": "vitest run --coverage"
28
+ "test:coverage": "vitest run --coverage",
29
+ "build": "tsc -p tsconfig.build.json"
25
30
  },
26
31
  "dependencies": {
27
32
  "@catalyst-cloud/schema": "^0.1.0"
package/src/replicate.ts DELETED
@@ -1,184 +0,0 @@
1
- // @catalyst-cloud/replicate — the runtime-agnostic replica WRITE path (ADR-0002). The write
2
- // counterpart to @catalyst-cloud/read-model: one `applyDelta` (+ cursor + truncate) that lands a
3
- // change-feed record into a local SQLite replica, schema-driven from the one Drizzle SSOT
4
- // (@catalyst-cloud/schema MIRROR_TABLE_META). The host-sync bun:sqlite replica and the browser OPFS
5
- // wasm replica BOTH route through this — collapsing the two hand-maintained `apply.ts` twins into one,
6
- // so they can never drift. Dependency-free / runtime-agnostic by design (NO bun:sqlite / node imports);
7
- // the runtime engine adapts to the portable `ReplicaWriteDb` handle, exactly as the read-model's
8
- // `SqlExecutor` is adapted per runtime.
9
- //
10
- // WIRE CONTRACT (shared — see apps/mirror/src/do/changefeed.ts):
11
- // • op:"upsert" → row is the FULL normalized DO row; INSERT … ON CONFLICT(pk) DO UPDATE, last-write-
12
- // wins by updated_at where present (DO NOTHING for a pure-join row like issue_labels).
13
- // • op:"delete" → soft-delete (set removed_at) on tables with a removed_at column, else hard-delete;
14
- // the PK is recovered from the row's PK columns or by splitting entityId on ':'.
15
-
16
- import { MIRROR_TABLE_META, type TableMeta } from "@catalyst-cloud/schema";
17
-
18
- /**
19
- * The portable replica WRITE seam — the write counterpart to read-model's `SqlExecutor`. bun:sqlite
20
- * (host-sync) and sqlite-wasm (browser OPFS) each satisfy it via a thin adapter. Generic over the
21
- * bindable type `B` because the two engines coerce binary/bigint differently (bun binds Uint8Array /
22
- * bigint; wasm binds ArrayBuffer / number) — the runtime supplies its own `toBindable` so every bound
23
- * value is already a `B`.
24
- */
25
- export interface ReplicaWriteDb<B> {
26
- /** Execute a parameterized mutation; return the number of rows changed (sqlite3_changes()). */
27
- run(sql: string, ...bindings: B[]): number;
28
- /** Run a single-row query; return the first row as an object, or undefined. */
29
- get(sql: string, ...bindings: B[]): Record<string, B> | undefined;
30
- }
31
-
32
- /** Coerce ONE wire JSON value to the runtime's bindable scalar `B` (booleans → 0/1, etc.). Runtime-
33
- * specific (binary/bigint differ per engine), so it is injected rather than baked in. */
34
- export type ToBindable<B> = (value: unknown) => B;
35
-
36
- /** One change-feed record (a /snapshot line or a /changes row). accountId/seq are the caller's concern.
37
- * `entity` is widened to string here (the runtime callers pass their stricter EntityName). */
38
- export interface ReplicaChange {
39
- entity: string;
40
- op: "upsert" | "delete";
41
- /** Full normalized row for "upsert"; for "delete" may be `{}` or carry just the PK columns. */
42
- row: Record<string, unknown>;
43
- /** The change_log.entity_id — the PK (composite PKs joined with ':'). Required to apply a delete. */
44
- entityId?: string;
45
- }
46
-
47
- function quoteIdent(ident: string): string {
48
- return `"${ident.replace(/"/g, '""')}"`;
49
- }
50
-
51
- function metaFor(entity: string): TableMeta | undefined {
52
- return (MIRROR_TABLE_META as Record<string, TableMeta>)[entity];
53
- }
54
-
55
- /**
56
- * Apply ONE change-feed record to the replica. Returns true iff a row was actually written (a stale
57
- * upsert rejected by the updated_at guard, or a delete of an already-absent row, returns false). Never
58
- * throws on a well-formed record; throws only on an unknown entity (malformed wire data).
59
- */
60
- export function applyDelta<B>(
61
- db: ReplicaWriteDb<B>,
62
- change: ReplicaChange,
63
- toBindable: ToBindable<B>,
64
- ): boolean {
65
- const meta = metaFor(change.entity);
66
- if (!meta) throw new Error(`applyDelta: unknown entity ${String(change.entity)}`);
67
- if (change.op === "delete") return applyDelete(db, change, meta, toBindable);
68
- return applyUpsert(db, change, meta, toBindable);
69
- }
70
-
71
- function applyUpsert<B>(
72
- db: ReplicaWriteDb<B>,
73
- change: ReplicaChange,
74
- meta: TableMeta,
75
- toBindable: ToBindable<B>,
76
- ): boolean {
77
- const table = change.entity;
78
- const cols = Object.keys(change.row);
79
- if (cols.length === 0) return false; // malformed wire upsert — skip rather than emit invalid SQL.
80
-
81
- const pkSet = new Set<string>(meta.pk);
82
- const nonPkCols = cols.filter((c) => !pkSet.has(c));
83
- const hasUpdatedAt = cols.includes("updated_at");
84
-
85
- const colList = cols.map(quoteIdent).join(", ");
86
- const placeholders = cols.map(() => "?").join(", ");
87
- const conflictTarget = meta.pk.map(quoteIdent).join(", ");
88
-
89
- let conflictClause: string;
90
- if (nonPkCols.length === 0) {
91
- // Pure join/edge row (issue_labels): PK is the whole row — no-op on conflict.
92
- conflictClause = `ON CONFLICT(${conflictTarget}) DO NOTHING`;
93
- } else {
94
- const setClause = nonPkCols
95
- .map((c) => `${quoteIdent(c)} = excluded.${quoteIdent(c)}`)
96
- .join(", ");
97
- // Last-write-wins by updated_at — mirrors the DO's guard so out-of-order deltas can't regress.
98
- const guard = hasUpdatedAt
99
- ? ` WHERE excluded.updated_at > ${quoteIdent(table)}.updated_at`
100
- : "";
101
- conflictClause = `ON CONFLICT(${conflictTarget}) DO UPDATE SET ${setClause}${guard}`;
102
- }
103
-
104
- const sql = `INSERT INTO ${quoteIdent(table)} (${colList}) VALUES (${placeholders}) ${conflictClause}`;
105
- return db.run(sql, ...cols.map((c) => toBindable(change.row[c]))) > 0;
106
- }
107
-
108
- function applyDelete<B>(
109
- db: ReplicaWriteDb<B>,
110
- change: ReplicaChange,
111
- meta: TableMeta,
112
- toBindable: ToBindable<B>,
113
- ): boolean {
114
- const table = change.entity;
115
-
116
- const pkVals = pkValuesFor(change, meta, toBindable);
117
- if (!pkVals) return false; // can't locate the row → nothing to do.
118
-
119
- const where = meta.pk.map((c) => `${quoteIdent(c)} = ?`).join(" AND ");
120
-
121
- if (meta.softDelete) {
122
- // Soft-delete: set removed_at, but only if currently live (idempotent re-delete is a no-op).
123
- return (
124
- db.run(
125
- `UPDATE ${quoteIdent(table)} SET removed_at = ? WHERE ${where} AND removed_at IS NULL`,
126
- toBindable(Date.now()),
127
- ...pkVals,
128
- ) > 0
129
- );
130
- }
131
-
132
- // Hard-delete (join/edge/GitHub tables have no removed_at).
133
- return db.run(`DELETE FROM ${quoteIdent(table)} WHERE ${where}`, ...pkVals) > 0;
134
- }
135
-
136
- /** Resolve the PK column values for a delete from the row's PK fields first, then entityId, else null.
137
- * Every value is routed through `toBindable` so the result is uniformly `B[]`. */
138
- function pkValuesFor<B>(
139
- change: ReplicaChange,
140
- meta: TableMeta,
141
- toBindable: ToBindable<B>,
142
- ): B[] | null {
143
- const fromRow = meta.pk.map((c) => change.row[c]);
144
- if (fromRow.every((v) => v !== undefined && v !== null)) {
145
- return fromRow.map(toBindable);
146
- }
147
- if (change.entityId != null) {
148
- const parts = change.entityId.split(":");
149
- if (parts.length === meta.pk.length) return parts.map((p) => toBindable(p));
150
- }
151
- return null;
152
- }
153
-
154
- /**
155
- * Wipe every replica entity table (but NOT the host-only `sync_meta` cursor table). Called before
156
- * replaying a fresh /snapshot so a resync can't leave orphaned rows the new snapshot no longer contains.
157
- */
158
- export function truncateReplica<B>(db: ReplicaWriteDb<B>): void {
159
- for (const entity of Object.keys(MIRROR_TABLE_META)) {
160
- db.run(`DELETE FROM ${quoteIdent(entity)}`);
161
- }
162
- }
163
-
164
- /** Read the persisted change-feed cursor (or null if a snapshot has never completed). */
165
- export function getCursor<B>(db: ReplicaWriteDb<B>): number | null {
166
- const row = db.get("SELECT value FROM sync_meta WHERE key = 'cursor'");
167
- if (!row) return null;
168
- const n = Number(row["value"]);
169
- return Number.isFinite(n) ? n : null;
170
- }
171
-
172
- /** Persist the change-feed cursor (the last applied change_log.seq). */
173
- export function setCursor<B>(
174
- db: ReplicaWriteDb<B>,
175
- cursor: number,
176
- toBindable: ToBindable<B>,
177
- ): void {
178
- db.run(
179
- "INSERT INTO sync_meta (key, value) VALUES (?, ?) " +
180
- "ON CONFLICT(key) DO UPDATE SET value = excluded.value",
181
- toBindable("cursor"),
182
- toBindable(String(cursor)),
183
- );
184
- }