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
wherevalue can be a function, so you can ask for ranges and patterns:{ age: (a) => a >= 18 }. It works ingetRows,updateRowanddeleteRow. - Unique fields.
insertWithUniqueCheckandupdateWithUniqueCheckrefuse a value another row already has, like aUNIQUEcolumn 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.
insertTableContentsadds an array of rows with one write. updateRowupdates every match. It used to change only the last matching row.deleteRowdeletes 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
whereis refused instead of touching the first row. searchmatches plain text instead of a regular expression.valid()returnsfalsefor 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
Post a Comment