Comment

Comment by ndrean

I detailed it here: https://github.com/ndrean/ZeBridge/blob/main/COOPERATIVE_EDI...

So yes, the core of that wish is built: a small CRDT that PostgreSQL itself merges, so not a general-purpose library: not for cooperative editing at the text/character level as I don't need this. Two people typing in the same text field at once still end with one winner. For collaborative text you still want Yjs or Automerge.

The sql function `zebridge_enable(register_cols => ARRAY['doc'])` installs a BEFORE INSERT OR UPDATE trigger that merges each accepted write into the stored document register by register. The client runs the same merge code. It’s “CRDTs integrated in Postgres” without running sync functions on both sides.

Reads: a client seeds, records its position in each stream (NATS) so resumes where it stopped, off-line or not.

Writes: are applied locally plus into an outbox entry in one transaction, sent when online, and a verdict per write (accepted, stale or rejected). The result reaches every client through the change stream above. There’s no client-to-client protocol: PostgreSQL is the single merge point, and clients receive the merged row.

Conflicts: 2 levels, last-writer-wins per row, and an opt-in map of last-write-wins inside a json column. Two editors changing different fields of one record both survive. Two changing the same field: the newer edit wins. It works like Cassandra’s per-cell timestamps, or Figma’s per-property multiplayer. Each field of the document is a precise register {"v": value, "t": timestamp, "w": writer}. In PG, the trigger `zebridge_merge_registers_t` merges every accepted write, register by register. A write can only add or advance registers, so a late offline write can’t roll back fields others changed meanwhile. In the client library, `mergeRegisters`.

parent ndrean 15 days ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton