Skip to main content

electron-db 0.16: A Tiny JSON Database for Electron, Now with Promises and Speed

electron-db 0.16 is out: the small JSON-file database for Electron now has async/await, answers lookups by id in a hundredth of a millisecond, and saves a thousand inserts in 22 ms instead of 55 seconds.

If you build a desktop app with Electron, you hit the same question early: where do I keep the app's data? A full database means native modules, rebuilds for every Electron version and a schema. Often you just need a few tables of records that survive a restart.

That is what electron-db is for. Each table is one readable JSON file in your app's data folder. There are no dependencies and no native code, so npm install is the whole setup, and the API reads like a tiny SQL: create a table, insert rows, then get, update and delete them with a where clause.

Getting started

Install it in your Electron app:

npm install electron-db

Then use it from the main process:

const db = require('electron-db');

db.createTable('customers', (succ, msg) => {
  console.log(succ, msg); // true 'Success!'
});

db.insertTableContent('customers', { name: 'Ana', city: 'Manila' }, (succ, msg, id) => {
  console.log('new row id', id);
});

db.getRows('customers', { city: 'Manila' }, (succ, rows) => {
  console.log(rows); // [ { name: 'Ana', city: 'Manila', id: 1792622745644 } ]
});

db.updateRow('customers', { name: 'Ana' }, { city: 'Cebu' }, (succ, msg, count) => {});
db.deleteRow('customers', { name: 'Ana' }, (succ, msg, count) => {});

The table lands in customers.json inside your app's user data folder, and you can open it in any editor. To keep it somewhere else, pass a folder as the second argument to any function: db.createTable('customers', '/path/to/data', callback).

New: async/await

Every function now has a promise version under db.promises. It takes the same arguments minus the callback, resolves with the result, and rejects with an Error:

const db = require('electron-db').promises;

await db.createTable('customers');

const id = await db.insertTableContent('customers', { name: 'Ana', age: 31 });
const adults = await db.getRows('customers', { age: (age) => age >= 18 });
const changed = await db.updateRow('customers', { id }, { city: 'Cebu' }); // 1
const removed = await db.deleteRow('customers', { id });                   // 1

A promise resolves only once its change is saved to disk. Changes started in the same tick share one file write, so bulk work is fast when you start it together:

// 1000 rows, one write
await Promise.all(items.map((item) => db.insertTableContent('records', item)));

The callback API is unchanged, so existing apps keep working. Both styles share the same data and can be mixed.

New: much faster on bigger tables

On a 100,000-row table (17 MB), lookups by id went from 26 ms to 0.01 ms, and 1000 inserts went from 55 seconds to 22 ms.

Until now, every call read and parsed the whole JSON file, and every change wrote it back, so each call took longer as the table grew. Version 0.16 keeps each table in memory after the first read. It reads the file again only when something else changes it on disk. Lookups by id use an index, and promise writes are combined.

Operation, 100,000 rows 0.15 0.16
getRows by id 26 ms 0.01 ms
getRows by another field 27 ms 2 ms
count 26 ms 0.01 ms
insertTableContent (callback) 54 ms 22 ms
updateRow / deleteRow by id (callback) 48 ms 21 ms
1000 inserts 55 s (callbacks) 22 ms (promises, Promise.all)

Measured on an Apple M5 Pro with Node 24. The files stay plain, pretty-printed JSON, and every save goes through a temporary file, so a crash mid-write cannot corrupt a table.

Also new, and fixed

This release also closes every open issue on GitHub.

  • Function conditions. A where value can be a function, so you can ask for ranges and patterns: { age: (a) => a >= 18 }. It works in getRows, updateRow and deleteRow.
  • Unique fields. insertWithUniqueCheck and updateWithUniqueCheck refuse a value another row already has, like a UNIQUE column in SQL.
  • The new id comes back. Inserts pass the new id to the callback and return it, so you no longer have to look the row up again.
  • Bulk insert. insertTableContents adds an array of rows with one write.
  • updateRow updates every match. It used to change only the last matching row.
  • deleteRow deletes only rows that match every condition. It used to remove rows that matched any one of them.
  • Inserts in a loop keep every row, each with its own id.
  • Full reference. The README now lists every function with its arguments and results.

When to use it, and when not to

electron-db fits app data up to tens of thousands of rows: customer lists, inventories, notes, history, records for a small business tool. You get it running in a minute, and you can read and fix the data by hand.

Every change still rewrites the whole file, about 3 ms at 10,000 rows and 20 ms at 100,000, and each table you use stays in memory. Only one process should write to a table at a time. For millions of rows, heavy write traffic or several processes writing at once, use SQLite through better-sqlite3. electron-db doesn't try to replace a real database; it covers the many apps that don't need one.

Try it

npm install electron-db@latest

Upgrading from 0.15 needs no code changes; the callback API and file format are the same. A few edge cases now behave more sensibly:

  • An empty where is refused instead of touching the first row.
  • search matches plain text instead of a regular expression.
  • valid() returns false for a missing table instead of throwing.

The code, docs and issue tracker are on GitHub, and the package is on npm. If electron-db saves you some setup, a star on GitHub helps others find it. Bug reports and pull requests are welcome.

Comments

Popular posts from this blog

Cursor AI Review: Is the AI Code Editor Worth It?

I've been using Cursor as my main code editor for a while now, and enough people have asked whether it's worth switching to that a proper review felt overdue. Short version: for me, yes — but with caveats. What is Cursor? Cursor is an AI-first code editor built as a fork of VS Code. That means every extension, theme, and keybinding you already use in VS Code works here, but with AI woven directly into the editing experience instead of bolted on as a plugin. It's made by Anysphere and can run models from OpenAI and Anthropic under the hood. What I like Tab completion is uncanny. Cursor predicts your next edit — not just the rest of the line, but the next change across the file. Once you get used to hitting Tab, going back to a plain editor feels slow. The Composer / Agent mode. You describe a change in plain language and it edits multiple files at once, showing you a diff to accept or reject. For refactors and boilerplate, this saves real time. It unde...

MacBook Pro M5 vs M5 Pro: Which One Should You Actually Buy?

Apple's latest 14-inch MacBook Pro comes in two very different flavors: the base M5 and the step-up M5 Pro . On paper they look similar — same gorgeous Liquid Retina XDR display, same design — but under the hood the gap is bigger than the names suggest. Here's a clear, no-hype breakdown, with concrete use cases so you can match the chip to your work. Quick spec comparison Spec M5 M5 Pro CPU 10-core (4 performance + 6 efficiency) Up to 18-core (6 performance + 12 efficiency) GPU 10-core Up to 20-core Neural Engine 16-core 16-core Memory bandwidth 153 GB/s 307 GB/s (roughly double) Unified memory 16 / 24 / 32 GB 24 / 48 / 64 GB Max storage Up to 4 TB SSD Up to 8 TB SSD Battery (video playback) Up to 24 hours Up to 22 hours Media engines Single encode/ProRes engine More encode/ProRes engines (higher configs) What actually changes between them More cores — the M5 Pro nearly doubles CPU cores and adds GPU cores, so sustained, multi-threaded work finishe...

Running a Server on a Mac Mini: Apple Silicon vs the Home-Server Field

The Mac Mini has quietly become one of the most interesting home-server boxes you can buy. It’s tiny, nearly silent, sips power, and Apple Silicon punches far above its weight. But is it actually the right machine to run your services on — or are you paying an Apple tax for a job a $400 mini PC does better? Let’s put it head-to-head. Why a Mac Mini makes a surprisingly good server Three things make Apple Silicon compelling as an always-on machine: Performance per watt. This is the headline. An M4 Mini idles at just a few watts and rarely pushes past ~35W under load, while delivering multicore performance that embarrasses machines drawing twice the power. Silence. Under typical server loads the fan is inaudible. If your “server” lives in a living room or bedroom, this matters more than any benchmark. Footprint. It’s the size of a coaster and runs cool, so it tucks anywhere. The honest catch It’s not all upside: macOS isn’t...