Better HN
topnewbestaskshowjobs
Log inSign up

ndrean

9 karma · joined Dec 29, 2019 (6y ago) · 13 submissions

Submissions

comment 1w ago · link · parent

So I solved the ordering problem (parent/child associations). Nothing is lost, only the refused write, with a verdict. I changed the API with an additional "after" key, documented.

comment 1w ago · link · parent

FYI, I added CDRT support (the same as PowerSync) and an example. It is agnostic, so you bring in your prefered library. It is not optimal though, I don't clean Postgres. I will push this.

comment 2w ago · link · parent

I understand. I needed rules, so LWW like Cassandra covered probably 80% of use cases. Otherwise, it is like Electric, all left to you.

comment 2w ago · link · parent

CRDT is useful when the application has a meaningful way to merge concurrent edits, if any. They don' replace database constraints or business rules. With LWW, you have a predictable winner when 2 devices replace the same value. Merging changes does not automatically preserve a unique realisation: you need server validaation. LWW loses concurrent intents, but for many PG-backed applications, explicit/nderstandable conflict outcomes matters more than automatic edits preservation. BTW, what do you mean by dead comments?

comment 2w ago · link · parent

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`.

comment 2w ago · link · parent

ZeBridge delivers to the client the (PG) enabled tables, replicates the schema locally (SQLite, PGLite, DuckDB tables), seeds them, and handles Postgres supported migrations. Local mutations are optimistic, durably queued (locally) and on (re)connection submitted to Postgres for the authoritative decision, and then CDC are echoed back with a verdict.

ElectricSQL is a Postgres-to-client read synchronization engine. It can operate without a local SQL database: it streams selected data as shapes and the selected rows can be maintained as JS objects or maintained by the dev as materialised into a local database. The reverse flow is implemented by the application and its backend API; libraries can add optimistic updates and offline write handling. Electric then synchronizes the resulting Postgres changes back to clients.

You also have PowerSync: it synchronizes the backend data into a local SQLite database using a schema supplied by the application. By default, records are stored as JSON and exposed through SQL views. It also offers native SQLite storage: managed tables, currently in alpha, are created and migrated automatically, while raw tables require developer-managed creation and migrations. Local writes take effect immediately and enter a durable upload queue. The SDK invokes a developer-provided uploadData() function to send those writes to the application’s backend API, which validates and applies them to Postgres. Authoritative changes then return through PowerSync’s synchronization stream.

In conclusion, ZeBridge is therefore closer to PowerSync in its local database and queued-write model, but the responsibilities differ. ZeBridge delivers server-derived schemas and integrates mutation submission and verdict handling. PowerSync manages local SQLite and the upload queue while the application supplies its client schema and backend write logic. Electric focuses on distributing Postgres changes, leaving client storage and the write path to the surrounding application stack.

comment 2w ago · link · parent

Right, it isn’t transparent. Unlike a CRDT, not every write is guaranteed to be accepted. Every write is applied locally at once, durably queued, and sent to PostgreSQL when online. The guarantee is that no write is lost, and each one ends with an explicit verdict: accepted, or refused with the reason and rolled back locally.

8.

Show HN: ZeBridge – PostgreSQL replicas on devices, synchronized over NATS(github.com ↗)

1 points · by ndrean · 2w ago · discuss

comment 3w ago · link · parent

I built ZeBridge to keep a local database on each device (SQLite, PGlite or DuckDB) in sync with a central PostgreSQL, for apps that must keep working on a bad connection.

It's aimed at B2B operational apps (fleets, depots, field teams), where each customer organisation is a tenant. It isn't built for consumer apps with one tenant per user.

How it works: a bridge written in Zig reads PostgreSQL's logical replication and publishes changes to NATS JetStream, one stream per tenant. Clients seed from snapshots kept in the NATS object store, then follow the stream. Writes made offline wait in a local outbox. When the device reconnects they go to PostgreSQL, which applies your rules (functions, row-level security) and sends back a verdict for each one. PostgreSQL stays the authority; NATS only carries the data.

The same NATS connection also reaches your own services. The live demo uses this to ask a routing service for a truck's route: https://bridge.zebridge.eu/demo (five trucks on a map of 16,173 charge points; open it in two tabs).

Clients: TypeScript (browser and Node), and a native Zig library with a C ABI, wrapped for Swift, Android, React Native, Flutter and Python.

10.

ZeBridge – PostgreSQL replicas on devices, synchronized over NATS(github.com ↗)

30 points · by ndrean · 3w ago · 15 comments

comment 8mo ago · link · parent

Sanitize your HTML on steroids. You may not need heavy tools to sanitize HTML and CSS. Thanks to the power of Zig and Lexbor, I built Zanitizer: a fast and lightweight sanitizer that parses the actual HTML and CSS ASTs natively.

No more JSDOM. Up to 15 times faster. Tested against 229 known OWASP/H5SC threats.

Available as a ~350kB WASM drop-in for Node/Browser or a 1.2MB standalone CLI.

12.

Sanitize HTML up to x15 vs. DOMPurify(github.com ↗)

2 points · by ndrean · 8mo ago · 1 comment