@super-line/store-sync-pglite 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,282 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ syncPgliteStoreServer: () => syncPgliteStoreServer
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_pglite = require("@electric-sql/pglite");
37
+ var import_live = require("@electric-sql/pglite/live");
38
+ var import_pglite_sync = require("@electric-sql/pglite-sync");
39
+ var import_postgres = __toESM(require("postgres"), 1);
40
+ var import_core = require("@super-line/core");
41
+ var import_store = require("@super-store/store");
42
+ var IDENT = /^[A-Za-z_][A-Za-z0-9_]*$/;
43
+ var SERVER_ORIGIN = "server";
44
+ var BASELINE_ORIGIN = "sl-baseline";
45
+ var b64 = (u) => {
46
+ let s = "";
47
+ for (const byte of u) s += String.fromCharCode(byte);
48
+ return btoa(s);
49
+ };
50
+ var fromB64 = (s) => {
51
+ const bin = atob(s);
52
+ const u = new Uint8Array(bin.length);
53
+ for (let i = 0; i < bin.length; i++) u[i] = bin.charCodeAt(i);
54
+ return u;
55
+ };
56
+ async function syncPgliteStoreServer(opts) {
57
+ const meta = opts.table ?? "resources";
58
+ if (!IDENT.test(meta)) throw new Error(`Invalid table name: ${meta}`);
59
+ const ups = `${meta}_updates`;
60
+ if (ups.length > 63) throw new Error(`Table name too long: "${meta}" \u2014 "${ups}" exceeds Postgres' 63-char limit`);
61
+ const ddlMeta = `CREATE TABLE IF NOT EXISTS "${meta}" (id text PRIMARY KEY, access jsonb NOT NULL, origin text, data jsonb)`;
62
+ const ddlUps = `CREATE TABLE IF NOT EXISTS "${ups}" (seq bigserial PRIMARY KEY, res_id text NOT NULL, update text NOT NULL, origin text)`;
63
+ const sql = (0, import_postgres.default)(opts.pgUrl, { prepare: false, onnotice: () => {
64
+ } });
65
+ const RACE_OK = /* @__PURE__ */ new Set(["42P07", "42710", "23505"]);
66
+ for (const ddl of [ddlMeta, ddlUps]) {
67
+ try {
68
+ await sql.unsafe(ddl);
69
+ } catch (err) {
70
+ if (!RACE_OK.has(err.code ?? "")) throw err;
71
+ }
72
+ }
73
+ const asJson = (v) => sql.json(v);
74
+ const ownsDb = !opts.db;
75
+ const db = opts.db ?? await import_pglite.PGlite.create({ extensions: { live: import_live.live, sync: (0, import_pglite_sync.electricSync)() } });
76
+ await db.exec(ddlMeta);
77
+ await db.exec(ddlUps);
78
+ const changeCbs = /* @__PURE__ */ new Set();
79
+ const deleteCbs = /* @__PURE__ */ new Set();
80
+ const onError = opts.onError ?? ((err, ctx) => console.error(`[store-sync-pglite] ${ctx.op} failed for ${ctx.id} (write not replicated):`, err));
81
+ const docs = /* @__PURE__ */ new Map();
82
+ let currentOrigin = SERVER_ORIGIN;
83
+ const getDoc = (id) => {
84
+ const existing = docs.get(id);
85
+ if (existing) return existing;
86
+ const d = new import_store.StoreValue({}, opts.resolveOptions?.(id));
87
+ d.encodeState();
88
+ d.onUpdate((update, m) => {
89
+ if (!m.local) return;
90
+ const origin = currentOrigin;
91
+ void sql`INSERT INTO ${sql(ups)} (res_id, update, origin) VALUES (${id}, ${b64(update)}, ${origin})`.catch((err) => onError(err, { op: "append", id }));
92
+ });
93
+ docs.set(id, d);
94
+ return d;
95
+ };
96
+ const withOrigin = (origin, fn) => {
97
+ currentOrigin = origin;
98
+ try {
99
+ fn();
100
+ } finally {
101
+ currentOrigin = SERVER_ORIGIN;
102
+ }
103
+ };
104
+ const foldFromCentral = async (id) => {
105
+ const d = getDoc(id);
106
+ const rows = await sql`SELECT update FROM ${sql(ups)} WHERE res_id = ${id} ORDER BY seq`;
107
+ for (const r of rows) d.applyUpdate(fromB64(r.update));
108
+ return d;
109
+ };
110
+ const compactCfg = opts.compact === false ? null : { everyN: opts.compact?.everyNUpdates ?? 200, debounceMs: opts.compact?.debounceMs ?? 2e3 };
111
+ const appendsSince = /* @__PURE__ */ new Map();
112
+ const compacting = /* @__PURE__ */ new Set();
113
+ const compactTimers = /* @__PURE__ */ new Map();
114
+ const compact = async (id) => {
115
+ await sql.begin(async (tx) => {
116
+ const rows = await tx`SELECT seq, update FROM ${tx(ups)} WHERE res_id = ${id} ORDER BY seq`;
117
+ if (rows.length < 2) return;
118
+ const maxSeq = rows[rows.length - 1]?.seq;
119
+ const doc = new import_store.StoreValue({}, opts.resolveOptions?.(id));
120
+ for (const r of rows) doc.applyUpdate(fromB64(r.update));
121
+ const baseline = b64(doc.encodeState());
122
+ const snapshot = doc.getSnapshot();
123
+ doc.dispose();
124
+ await tx`INSERT INTO ${tx(ups)} (res_id, update, origin) VALUES (${id}, ${baseline}, ${BASELINE_ORIGIN})`;
125
+ await tx`DELETE FROM ${tx(ups)} WHERE res_id = ${id} AND seq <= ${maxSeq}`;
126
+ await tx`UPDATE ${tx(meta)} SET data = ${asJson(snapshot)} WHERE id = ${id}`;
127
+ });
128
+ };
129
+ const scheduleCompact = (id) => {
130
+ if (!compactCfg) return;
131
+ const n = (appendsSince.get(id) ?? 0) + 1;
132
+ appendsSince.set(id, n);
133
+ const fire = () => {
134
+ compactTimers.delete(id);
135
+ appendsSince.set(id, 0);
136
+ if (compacting.has(id)) return;
137
+ compacting.add(id);
138
+ void compact(id).catch((err) => onError(err, { op: "append", id })).finally(() => compacting.delete(id));
139
+ };
140
+ const existing = compactTimers.get(id);
141
+ if (existing) clearTimeout(existing);
142
+ if (n >= compactCfg.everyN) {
143
+ fire();
144
+ return;
145
+ }
146
+ const t = setTimeout(fire, compactCfg.debounceMs);
147
+ t.unref?.();
148
+ compactTimers.set(id, t);
149
+ };
150
+ const upsSub = await db.live.changes(`SELECT seq, res_id, update, origin FROM "${ups}"`, [], "seq", (changes) => {
151
+ for (const ch of changes) {
152
+ if (ch.__op__ !== "INSERT" && ch.__op__ !== "UPDATE") continue;
153
+ try {
154
+ getDoc(ch.res_id).applyUpdate(fromB64(ch.update));
155
+ if (ch.origin === BASELINE_ORIGIN) continue;
156
+ const change = { id: ch.res_id, update: ch.update, origin: ch.origin ?? "" };
157
+ for (const cb of changeCbs) cb(change);
158
+ scheduleCompact(ch.res_id);
159
+ } catch (err) {
160
+ onError(err, { op: "append", id: ch.res_id });
161
+ }
162
+ }
163
+ });
164
+ const metaSub = await db.live.changes(`SELECT id FROM "${meta}"`, [], "id", (changes) => {
165
+ for (const ch of changes) {
166
+ if (ch.__op__ !== "DELETE") continue;
167
+ docs.get(ch.id)?.dispose();
168
+ docs.delete(ch.id);
169
+ for (const cb of deleteCbs) cb(ch.id);
170
+ }
171
+ });
172
+ const shapes = opts.electricUrl && db.sync ? await Promise.all(
173
+ [meta, ups].map(
174
+ (t) => db.sync.syncShapeToTable({
175
+ shape: { url: opts.electricUrl, params: { table: t } },
176
+ table: t,
177
+ primaryKey: t === meta ? ["id"] : ["seq"],
178
+ shapeKey: null
179
+ })
180
+ )
181
+ ) : [];
182
+ return {
183
+ clustering: "self",
184
+ model: "crdt",
185
+ async read(id) {
186
+ const rows = await sql`SELECT access::text AS access FROM ${sql(meta)} WHERE id = ${id}`;
187
+ const row = rows[0];
188
+ if (!row) return void 0;
189
+ const doc = docs.get(id) ?? await foldFromCentral(id);
190
+ return { id, data: b64(doc.encodeState()), accessRules: JSON.parse(row.access) };
191
+ },
192
+ async create(id, data, accessRules) {
193
+ const seedDoc = new import_store.StoreValue(data ?? {}, opts.resolveOptions?.(id));
194
+ const seed = b64(seedDoc.encodeState());
195
+ const snapshot = seedDoc.getSnapshot();
196
+ seedDoc.dispose();
197
+ await sql.begin(async (tx) => {
198
+ const res = await tx`INSERT INTO ${tx(meta)} (id, access, origin, data)
199
+ VALUES (${id}, ${asJson(accessRules)}, ${null}, ${asJson(snapshot)})
200
+ ON CONFLICT (id) DO NOTHING`;
201
+ if (res.count === 0) throw new import_core.SuperLineError("CONFLICT", `Resource already exists: ${id}`);
202
+ await tx`INSERT INTO ${tx(ups)} (res_id, update, origin) VALUES (${id}, ${seed}, ${null})`;
203
+ });
204
+ getDoc(id).applyUpdate(fromB64(seed));
205
+ },
206
+ async apply(change) {
207
+ if (typeof change.update !== "string") {
208
+ const rows = await sql`SELECT 1 FROM ${sql(meta)} WHERE id = ${change.id}`;
209
+ if (rows.count === 0) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
210
+ withOrigin(change.origin, () => void getDoc(change.id).update(change.update));
211
+ return;
212
+ }
213
+ const res = await sql`INSERT INTO ${sql(ups)} (res_id, update, origin)
214
+ SELECT ${change.id}, ${change.update}, ${change.origin}
215
+ WHERE EXISTS (SELECT 1 FROM ${sql(meta)} WHERE id = ${change.id})`;
216
+ if (res.count === 0) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
217
+ },
218
+ open(id, openOpts) {
219
+ const doc = getDoc(id);
220
+ const origin = openOpts?.origin ?? SERVER_ORIGIN;
221
+ const subs = /* @__PURE__ */ new Set();
222
+ return {
223
+ getSnapshot: () => doc.getSnapshot(),
224
+ subscribe: (cb) => {
225
+ const off = doc.subscribe(cb);
226
+ subs.add(off);
227
+ return () => {
228
+ off();
229
+ subs.delete(off);
230
+ };
231
+ },
232
+ set: (value) => withOrigin(origin, () => void doc.set(value)),
233
+ update: (partial) => withOrigin(origin, () => void doc.update(partial)),
234
+ // Surgical key removal: read live state, drop the path, set() (diff-and-patch) — the only delete-capable
235
+ // surface (update MERGES, so it can never remove a key). Atomic in-process.
236
+ delete: (path) => withOrigin(origin, () => void doc.set((0, import_core.removeAtPath)(doc.getSnapshot(), path))),
237
+ close: () => {
238
+ for (const off of subs) off();
239
+ subs.clear();
240
+ }
241
+ };
242
+ },
243
+ async setAccess(id, accessRules) {
244
+ const res = await sql`UPDATE ${sql(meta)} SET access = ${asJson(accessRules)} WHERE id = ${id}`;
245
+ if (res.count === 0) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${id}`);
246
+ },
247
+ async delete(id) {
248
+ await sql.begin(async (tx) => {
249
+ await tx`DELETE FROM ${tx(meta)} WHERE id = ${id}`;
250
+ await tx`DELETE FROM ${tx(ups)} WHERE res_id = ${id}`;
251
+ });
252
+ },
253
+ async list() {
254
+ const rows = await sql`SELECT id FROM ${sql(meta)}`;
255
+ return rows.map((r) => r.id);
256
+ },
257
+ onChange(cb) {
258
+ changeCbs.add(cb);
259
+ return () => changeCbs.delete(cb);
260
+ },
261
+ onDelete(cb) {
262
+ deleteCbs.add(cb);
263
+ return () => deleteCbs.delete(cb);
264
+ },
265
+ async close() {
266
+ for (const t of compactTimers.values()) clearTimeout(t);
267
+ compactTimers.clear();
268
+ try {
269
+ await upsSub.unsubscribe();
270
+ await metaSub.unsubscribe();
271
+ for (const s of shapes) s.unsubscribe();
272
+ if (ownsDb) await db.close();
273
+ } finally {
274
+ await sql.end();
275
+ }
276
+ }
277
+ };
278
+ }
279
+ // Annotate the CommonJS export names for ESM import in node:
280
+ 0 && (module.exports = {
281
+ syncPgliteStoreServer
282
+ });
@@ -0,0 +1,51 @@
1
+ import { PGliteWithLive } from '@electric-sql/pglite/live';
2
+ import { ServerStore } from '@super-line/core';
3
+
4
+ /** Per-resource super-store config (mode + opaque paths). Supply the SAME resolver to the client's
5
+ * `syncStoreClient` so both halves build each resource's Yjs doc identically (no mode drift). */
6
+ interface DocOptions {
7
+ mode?: 'shallow' | 'document';
8
+ opaque?: string[];
9
+ }
10
+ /** Options for {@link syncPgliteStoreServer}. */
11
+ interface SyncPgliteStoreOptions {
12
+ /** Connection string for the central Postgres — source of truth for the op-log + strong ACL/existence. */
13
+ pgUrl: string;
14
+ /** Electric shape endpoint (e.g. `http://localhost:3000/v1/shape`). Omit to disable sync (tests feed the replica). */
15
+ electricUrl?: string;
16
+ /** Table prefix: creates `<table>` (existence + ACL) and `<table>_updates` (the Yjs op-log). Default `resources`. */
17
+ table?: string;
18
+ /** Advanced/testing: supply the local PGlite replica (needs the `live` extension; add `electricSync` for real sync). */
19
+ db?: PGliteWithLive;
20
+ /** Per-resource doc mode — must match the client's `syncStoreClient({ resolveOptions })`. */
21
+ resolveOptions?: (id: string) => DocOptions | undefined;
22
+ /**
23
+ * Op-log compaction: fold the log → materialize `<table>.data` (SQL-queryable board) + a baseline row →
24
+ * trim superseded rows. Bounds op-log growth and keeps a (debounced, eventually-consistent) snapshot in
25
+ * `<table>.data`. `false` disables it (pure append-only log). Single-writer per resource across the cluster.
26
+ */
27
+ compact?: false | {
28
+ everyNUpdates?: number;
29
+ debounceMs?: number;
30
+ };
31
+ /**
32
+ * Called when a background op-log append (a server co-write through `open()`/`apply`-object) fails to persist.
33
+ * Those writes are synchronous (`ServerReplica` returns void), so the INSERT can't reject to the caller — this
34
+ * is the only place the failure is observable. Defaults to `console.error`.
35
+ */
36
+ onError?: (err: unknown, ctx: {
37
+ op: 'append';
38
+ id: string;
39
+ }) => void;
40
+ }
41
+ /**
42
+ * The self-clustering, **CRDT** server half (`clustering:'self'`, `model:'crdt'`). Writes append a Yjs delta
43
+ * to a central `<table>_updates` op-log; Electric streams the op-log to each node's in-memory PGlite replica,
44
+ * whose `live.changes` folds every delta into a per-resource super-store doc and surfaces it through
45
+ * {@link ServerStore.onChange} — which core fans to LOCAL subscribers only. Strong ACL/existence reads hit
46
+ * central Postgres. `open()` returns a reactive {@link ServerReplica} over the in-memory doc (the agent
47
+ * co-writer). Pair with `syncStoreClient()` on the client.
48
+ */
49
+ declare function syncPgliteStoreServer(opts: SyncPgliteStoreOptions): Promise<ServerStore>;
50
+
51
+ export { type DocOptions, type SyncPgliteStoreOptions, syncPgliteStoreServer };
@@ -0,0 +1,51 @@
1
+ import { PGliteWithLive } from '@electric-sql/pglite/live';
2
+ import { ServerStore } from '@super-line/core';
3
+
4
+ /** Per-resource super-store config (mode + opaque paths). Supply the SAME resolver to the client's
5
+ * `syncStoreClient` so both halves build each resource's Yjs doc identically (no mode drift). */
6
+ interface DocOptions {
7
+ mode?: 'shallow' | 'document';
8
+ opaque?: string[];
9
+ }
10
+ /** Options for {@link syncPgliteStoreServer}. */
11
+ interface SyncPgliteStoreOptions {
12
+ /** Connection string for the central Postgres — source of truth for the op-log + strong ACL/existence. */
13
+ pgUrl: string;
14
+ /** Electric shape endpoint (e.g. `http://localhost:3000/v1/shape`). Omit to disable sync (tests feed the replica). */
15
+ electricUrl?: string;
16
+ /** Table prefix: creates `<table>` (existence + ACL) and `<table>_updates` (the Yjs op-log). Default `resources`. */
17
+ table?: string;
18
+ /** Advanced/testing: supply the local PGlite replica (needs the `live` extension; add `electricSync` for real sync). */
19
+ db?: PGliteWithLive;
20
+ /** Per-resource doc mode — must match the client's `syncStoreClient({ resolveOptions })`. */
21
+ resolveOptions?: (id: string) => DocOptions | undefined;
22
+ /**
23
+ * Op-log compaction: fold the log → materialize `<table>.data` (SQL-queryable board) + a baseline row →
24
+ * trim superseded rows. Bounds op-log growth and keeps a (debounced, eventually-consistent) snapshot in
25
+ * `<table>.data`. `false` disables it (pure append-only log). Single-writer per resource across the cluster.
26
+ */
27
+ compact?: false | {
28
+ everyNUpdates?: number;
29
+ debounceMs?: number;
30
+ };
31
+ /**
32
+ * Called when a background op-log append (a server co-write through `open()`/`apply`-object) fails to persist.
33
+ * Those writes are synchronous (`ServerReplica` returns void), so the INSERT can't reject to the caller — this
34
+ * is the only place the failure is observable. Defaults to `console.error`.
35
+ */
36
+ onError?: (err: unknown, ctx: {
37
+ op: 'append';
38
+ id: string;
39
+ }) => void;
40
+ }
41
+ /**
42
+ * The self-clustering, **CRDT** server half (`clustering:'self'`, `model:'crdt'`). Writes append a Yjs delta
43
+ * to a central `<table>_updates` op-log; Electric streams the op-log to each node's in-memory PGlite replica,
44
+ * whose `live.changes` folds every delta into a per-resource super-store doc and surfaces it through
45
+ * {@link ServerStore.onChange} — which core fans to LOCAL subscribers only. Strong ACL/existence reads hit
46
+ * central Postgres. `open()` returns a reactive {@link ServerReplica} over the in-memory doc (the agent
47
+ * co-writer). Pair with `syncStoreClient()` on the client.
48
+ */
49
+ declare function syncPgliteStoreServer(opts: SyncPgliteStoreOptions): Promise<ServerStore>;
50
+
51
+ export { type DocOptions, type SyncPgliteStoreOptions, syncPgliteStoreServer };
package/dist/index.js ADDED
@@ -0,0 +1,247 @@
1
+ // src/index.ts
2
+ import { PGlite } from "@electric-sql/pglite";
3
+ import { live } from "@electric-sql/pglite/live";
4
+ import { electricSync } from "@electric-sql/pglite-sync";
5
+ import postgres from "postgres";
6
+ import { SuperLineError, removeAtPath } from "@super-line/core";
7
+ import { StoreValue } from "@super-store/store";
8
+ var IDENT = /^[A-Za-z_][A-Za-z0-9_]*$/;
9
+ var SERVER_ORIGIN = "server";
10
+ var BASELINE_ORIGIN = "sl-baseline";
11
+ var b64 = (u) => {
12
+ let s = "";
13
+ for (const byte of u) s += String.fromCharCode(byte);
14
+ return btoa(s);
15
+ };
16
+ var fromB64 = (s) => {
17
+ const bin = atob(s);
18
+ const u = new Uint8Array(bin.length);
19
+ for (let i = 0; i < bin.length; i++) u[i] = bin.charCodeAt(i);
20
+ return u;
21
+ };
22
+ async function syncPgliteStoreServer(opts) {
23
+ const meta = opts.table ?? "resources";
24
+ if (!IDENT.test(meta)) throw new Error(`Invalid table name: ${meta}`);
25
+ const ups = `${meta}_updates`;
26
+ if (ups.length > 63) throw new Error(`Table name too long: "${meta}" \u2014 "${ups}" exceeds Postgres' 63-char limit`);
27
+ const ddlMeta = `CREATE TABLE IF NOT EXISTS "${meta}" (id text PRIMARY KEY, access jsonb NOT NULL, origin text, data jsonb)`;
28
+ const ddlUps = `CREATE TABLE IF NOT EXISTS "${ups}" (seq bigserial PRIMARY KEY, res_id text NOT NULL, update text NOT NULL, origin text)`;
29
+ const sql = postgres(opts.pgUrl, { prepare: false, onnotice: () => {
30
+ } });
31
+ const RACE_OK = /* @__PURE__ */ new Set(["42P07", "42710", "23505"]);
32
+ for (const ddl of [ddlMeta, ddlUps]) {
33
+ try {
34
+ await sql.unsafe(ddl);
35
+ } catch (err) {
36
+ if (!RACE_OK.has(err.code ?? "")) throw err;
37
+ }
38
+ }
39
+ const asJson = (v) => sql.json(v);
40
+ const ownsDb = !opts.db;
41
+ const db = opts.db ?? await PGlite.create({ extensions: { live, sync: electricSync() } });
42
+ await db.exec(ddlMeta);
43
+ await db.exec(ddlUps);
44
+ const changeCbs = /* @__PURE__ */ new Set();
45
+ const deleteCbs = /* @__PURE__ */ new Set();
46
+ const onError = opts.onError ?? ((err, ctx) => console.error(`[store-sync-pglite] ${ctx.op} failed for ${ctx.id} (write not replicated):`, err));
47
+ const docs = /* @__PURE__ */ new Map();
48
+ let currentOrigin = SERVER_ORIGIN;
49
+ const getDoc = (id) => {
50
+ const existing = docs.get(id);
51
+ if (existing) return existing;
52
+ const d = new StoreValue({}, opts.resolveOptions?.(id));
53
+ d.encodeState();
54
+ d.onUpdate((update, m) => {
55
+ if (!m.local) return;
56
+ const origin = currentOrigin;
57
+ void sql`INSERT INTO ${sql(ups)} (res_id, update, origin) VALUES (${id}, ${b64(update)}, ${origin})`.catch((err) => onError(err, { op: "append", id }));
58
+ });
59
+ docs.set(id, d);
60
+ return d;
61
+ };
62
+ const withOrigin = (origin, fn) => {
63
+ currentOrigin = origin;
64
+ try {
65
+ fn();
66
+ } finally {
67
+ currentOrigin = SERVER_ORIGIN;
68
+ }
69
+ };
70
+ const foldFromCentral = async (id) => {
71
+ const d = getDoc(id);
72
+ const rows = await sql`SELECT update FROM ${sql(ups)} WHERE res_id = ${id} ORDER BY seq`;
73
+ for (const r of rows) d.applyUpdate(fromB64(r.update));
74
+ return d;
75
+ };
76
+ const compactCfg = opts.compact === false ? null : { everyN: opts.compact?.everyNUpdates ?? 200, debounceMs: opts.compact?.debounceMs ?? 2e3 };
77
+ const appendsSince = /* @__PURE__ */ new Map();
78
+ const compacting = /* @__PURE__ */ new Set();
79
+ const compactTimers = /* @__PURE__ */ new Map();
80
+ const compact = async (id) => {
81
+ await sql.begin(async (tx) => {
82
+ const rows = await tx`SELECT seq, update FROM ${tx(ups)} WHERE res_id = ${id} ORDER BY seq`;
83
+ if (rows.length < 2) return;
84
+ const maxSeq = rows[rows.length - 1]?.seq;
85
+ const doc = new StoreValue({}, opts.resolveOptions?.(id));
86
+ for (const r of rows) doc.applyUpdate(fromB64(r.update));
87
+ const baseline = b64(doc.encodeState());
88
+ const snapshot = doc.getSnapshot();
89
+ doc.dispose();
90
+ await tx`INSERT INTO ${tx(ups)} (res_id, update, origin) VALUES (${id}, ${baseline}, ${BASELINE_ORIGIN})`;
91
+ await tx`DELETE FROM ${tx(ups)} WHERE res_id = ${id} AND seq <= ${maxSeq}`;
92
+ await tx`UPDATE ${tx(meta)} SET data = ${asJson(snapshot)} WHERE id = ${id}`;
93
+ });
94
+ };
95
+ const scheduleCompact = (id) => {
96
+ if (!compactCfg) return;
97
+ const n = (appendsSince.get(id) ?? 0) + 1;
98
+ appendsSince.set(id, n);
99
+ const fire = () => {
100
+ compactTimers.delete(id);
101
+ appendsSince.set(id, 0);
102
+ if (compacting.has(id)) return;
103
+ compacting.add(id);
104
+ void compact(id).catch((err) => onError(err, { op: "append", id })).finally(() => compacting.delete(id));
105
+ };
106
+ const existing = compactTimers.get(id);
107
+ if (existing) clearTimeout(existing);
108
+ if (n >= compactCfg.everyN) {
109
+ fire();
110
+ return;
111
+ }
112
+ const t = setTimeout(fire, compactCfg.debounceMs);
113
+ t.unref?.();
114
+ compactTimers.set(id, t);
115
+ };
116
+ const upsSub = await db.live.changes(`SELECT seq, res_id, update, origin FROM "${ups}"`, [], "seq", (changes) => {
117
+ for (const ch of changes) {
118
+ if (ch.__op__ !== "INSERT" && ch.__op__ !== "UPDATE") continue;
119
+ try {
120
+ getDoc(ch.res_id).applyUpdate(fromB64(ch.update));
121
+ if (ch.origin === BASELINE_ORIGIN) continue;
122
+ const change = { id: ch.res_id, update: ch.update, origin: ch.origin ?? "" };
123
+ for (const cb of changeCbs) cb(change);
124
+ scheduleCompact(ch.res_id);
125
+ } catch (err) {
126
+ onError(err, { op: "append", id: ch.res_id });
127
+ }
128
+ }
129
+ });
130
+ const metaSub = await db.live.changes(`SELECT id FROM "${meta}"`, [], "id", (changes) => {
131
+ for (const ch of changes) {
132
+ if (ch.__op__ !== "DELETE") continue;
133
+ docs.get(ch.id)?.dispose();
134
+ docs.delete(ch.id);
135
+ for (const cb of deleteCbs) cb(ch.id);
136
+ }
137
+ });
138
+ const shapes = opts.electricUrl && db.sync ? await Promise.all(
139
+ [meta, ups].map(
140
+ (t) => db.sync.syncShapeToTable({
141
+ shape: { url: opts.electricUrl, params: { table: t } },
142
+ table: t,
143
+ primaryKey: t === meta ? ["id"] : ["seq"],
144
+ shapeKey: null
145
+ })
146
+ )
147
+ ) : [];
148
+ return {
149
+ clustering: "self",
150
+ model: "crdt",
151
+ async read(id) {
152
+ const rows = await sql`SELECT access::text AS access FROM ${sql(meta)} WHERE id = ${id}`;
153
+ const row = rows[0];
154
+ if (!row) return void 0;
155
+ const doc = docs.get(id) ?? await foldFromCentral(id);
156
+ return { id, data: b64(doc.encodeState()), accessRules: JSON.parse(row.access) };
157
+ },
158
+ async create(id, data, accessRules) {
159
+ const seedDoc = new StoreValue(data ?? {}, opts.resolveOptions?.(id));
160
+ const seed = b64(seedDoc.encodeState());
161
+ const snapshot = seedDoc.getSnapshot();
162
+ seedDoc.dispose();
163
+ await sql.begin(async (tx) => {
164
+ const res = await tx`INSERT INTO ${tx(meta)} (id, access, origin, data)
165
+ VALUES (${id}, ${asJson(accessRules)}, ${null}, ${asJson(snapshot)})
166
+ ON CONFLICT (id) DO NOTHING`;
167
+ if (res.count === 0) throw new SuperLineError("CONFLICT", `Resource already exists: ${id}`);
168
+ await tx`INSERT INTO ${tx(ups)} (res_id, update, origin) VALUES (${id}, ${seed}, ${null})`;
169
+ });
170
+ getDoc(id).applyUpdate(fromB64(seed));
171
+ },
172
+ async apply(change) {
173
+ if (typeof change.update !== "string") {
174
+ const rows = await sql`SELECT 1 FROM ${sql(meta)} WHERE id = ${change.id}`;
175
+ if (rows.count === 0) throw new SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
176
+ withOrigin(change.origin, () => void getDoc(change.id).update(change.update));
177
+ return;
178
+ }
179
+ const res = await sql`INSERT INTO ${sql(ups)} (res_id, update, origin)
180
+ SELECT ${change.id}, ${change.update}, ${change.origin}
181
+ WHERE EXISTS (SELECT 1 FROM ${sql(meta)} WHERE id = ${change.id})`;
182
+ if (res.count === 0) throw new SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
183
+ },
184
+ open(id, openOpts) {
185
+ const doc = getDoc(id);
186
+ const origin = openOpts?.origin ?? SERVER_ORIGIN;
187
+ const subs = /* @__PURE__ */ new Set();
188
+ return {
189
+ getSnapshot: () => doc.getSnapshot(),
190
+ subscribe: (cb) => {
191
+ const off = doc.subscribe(cb);
192
+ subs.add(off);
193
+ return () => {
194
+ off();
195
+ subs.delete(off);
196
+ };
197
+ },
198
+ set: (value) => withOrigin(origin, () => void doc.set(value)),
199
+ update: (partial) => withOrigin(origin, () => void doc.update(partial)),
200
+ // Surgical key removal: read live state, drop the path, set() (diff-and-patch) — the only delete-capable
201
+ // surface (update MERGES, so it can never remove a key). Atomic in-process.
202
+ delete: (path) => withOrigin(origin, () => void doc.set(removeAtPath(doc.getSnapshot(), path))),
203
+ close: () => {
204
+ for (const off of subs) off();
205
+ subs.clear();
206
+ }
207
+ };
208
+ },
209
+ async setAccess(id, accessRules) {
210
+ const res = await sql`UPDATE ${sql(meta)} SET access = ${asJson(accessRules)} WHERE id = ${id}`;
211
+ if (res.count === 0) throw new SuperLineError("NOT_FOUND", `No resource: ${id}`);
212
+ },
213
+ async delete(id) {
214
+ await sql.begin(async (tx) => {
215
+ await tx`DELETE FROM ${tx(meta)} WHERE id = ${id}`;
216
+ await tx`DELETE FROM ${tx(ups)} WHERE res_id = ${id}`;
217
+ });
218
+ },
219
+ async list() {
220
+ const rows = await sql`SELECT id FROM ${sql(meta)}`;
221
+ return rows.map((r) => r.id);
222
+ },
223
+ onChange(cb) {
224
+ changeCbs.add(cb);
225
+ return () => changeCbs.delete(cb);
226
+ },
227
+ onDelete(cb) {
228
+ deleteCbs.add(cb);
229
+ return () => deleteCbs.delete(cb);
230
+ },
231
+ async close() {
232
+ for (const t of compactTimers.values()) clearTimeout(t);
233
+ compactTimers.clear();
234
+ try {
235
+ await upsSub.unsubscribe();
236
+ await metaSub.unsubscribe();
237
+ for (const s of shapes) s.unsubscribe();
238
+ if (ownsDb) await db.close();
239
+ } finally {
240
+ await sql.end();
241
+ }
242
+ }
243
+ };
244
+ }
245
+ export {
246
+ syncPgliteStoreServer
247
+ };
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@super-line/store-sync-pglite",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Self-clustering CRDT Store for super-line — an append-only Yjs op-log in central Postgres that ElectricSQL ships to a per-node PGlite replica, folded into in-memory super-store docs. Postgres+Electric is the only fan-out infra; no adapter.",
6
+ "license": "MIT",
7
+ "author": "Mert",
8
+ "keywords": [
9
+ "super-line",
10
+ "store",
11
+ "crdt",
12
+ "yjs",
13
+ "pglite",
14
+ "postgres",
15
+ "electric",
16
+ "sync",
17
+ "self",
18
+ "collaboration"
19
+ ],
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/mertdogar/super-line.git",
23
+ "directory": "packages/store-sync-pglite"
24
+ },
25
+ "homepage": "https://mertdogar.github.io/super-line/",
26
+ "bugs": "https://github.com/mertdogar/super-line/issues",
27
+ "main": "./dist/index.cjs",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "import": {
33
+ "types": "./dist/index.d.ts",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "require": {
37
+ "types": "./dist/index.d.cts",
38
+ "default": "./dist/index.cjs"
39
+ }
40
+ }
41
+ },
42
+ "files": [
43
+ "dist"
44
+ ],
45
+ "sideEffects": false,
46
+ "engines": {
47
+ "node": ">=18"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "dependencies": {
53
+ "@super-store/store": "^0.3.0",
54
+ "@electric-sql/pglite": "^0.5.3",
55
+ "@electric-sql/pglite-sync": "^0.6.3",
56
+ "postgres": "^3.4.9",
57
+ "@super-line/core": "^0.8.0"
58
+ },
59
+ "devDependencies": {
60
+ "@electric-sql/pglite-socket": "^0.2.6"
61
+ },
62
+ "scripts": {
63
+ "build": "tsup"
64
+ }
65
+ }