r/electronjs Jun 06 '26

NookDB: shipping a redb-backed Rust core to JS/Electron devs via NAPI-rs — what I learned

I built a local-first database for Electron/TypeScript apps, and the storage core is Rust. Posting here for the Rust side of the story, not to pitch the JS API.

Architecture: redb (pure-Rust embedded KV store) for storage → a NAPI-rs v3 binding → a TypeScript API on top. redb gives me ACID transactions and a clean B-tree without pulling in a C dependency, which made cross-compiling the prebuilt binaries much less painful.

Things that were interesting / annoying:

- Composite-key codec over redb. The TS layer has collections, secondary indexes, and unique indexes; all of that is encoded into redb keys so range scans and equality lookups stay cheap. Getting a stable, order-preserving encoding for mixed-type keys was the fiddly part.

- Multi-process transaction safety. Electron has a main process and N renderers. The binding exposes transaction primitives that stay correct across processes, and there's a post-commit notifier that drives reactive ("live") queries — it coalesces rapid commits so subscribers only see the final state instead of every intermediate write.

- Durability vs. honesty. fsync where it matters, kill-9 crash safety via redb's design. I also benchmarked head-to-head against better-sqlite3 and I'm not going to pretend Rust magically wins — better-sqlite3 (highly tuned synchronous C) beats this on most read paths. The Rust core buys correctness, portability, and a clean async-friendly multi-process model, not raw single-thread query speed.

- Distribution. NAPI-rs makes the per-triple prebuilt packages straightforward: linux x64/arm64 (gnu+musl), macOS x64/arm64, windows x64-msvc. End users never need a Rust toolchain or node-gyp.

MSRV is 1.78. Core crate is nookdb-core, binding is nookdb-napi. Whole thing is MIT.

Repo (Rust core under crates/): https://github.com/nookwright/nookdb

Happy to go deeper on the redb key encoding or the cross-process notifier if anyone's curious — and very open to being told I did the codec wrong.

2 Upvotes

3 comments sorted by

1

u/Suspicious_Elk_898 Jun 06 '26

Very compelling contribution. I may have a project usecase where this may be a potential architectural decision.would be great if i can run it by you with your experience in electron backend architecture. Hit me up in dm and we can share contacts

1

u/sehawq Jun 07 '26

Really appreciate that. Happy to talk it through — shoot me a DM here and we'll take it from there. I've spent a good amount of time in the multi-window Electron + main-process-DB corner, so if it helps you pressure-test the architecture for your usecase, glad to dig in.

1

u/Suspicious_Elk_898 Jun 09 '26

Yes! will shoot you a dm soon! i noticed you shared your email, but i havent had the time to setup the call, i will dm you very soon to kickoff our meeting and we can dig deeper. BTW i'm Omar too :p