Lesson 11 of 28 · javascript
Data Persistence – electron-store, SQLite & LowDB
Duration: 11 mins
Data Persistence – electron-store, SQLite & LowDB
electron-store (key/value JSON)
npm install electron-store
const Store = require('electron-store');
const store = new Store();
store.set('settings.theme', 'dark');
console.log(store.get('settings.theme'));
Data is saved in a plain JSON file inside app.getPath('userData').
SQLite (via better‑sqlite3)
npm install better-sqlite3
const Database = require('better-sqlite3');
const db = new Database(path.join(app.getPath('userData'), 'app.db'));
db.exec('CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, title TEXT, body TEXT)');
function addNote(title, body){
const stmt = db.prepare('INSERT INTO notes (title, body) VALUES (?, ?)');
return stmt.run(title, body).lastInsertRowid;
}
SQLite is great for relational data and fast queries.
LowDB (tiny JSON database)
npm install lowdb
const { Low, JSONFile } = require('lowdb');
const adapter = new JSONFile(path.join(app.getPath('userData'), 'db.json'));
const db = new Low(adapter);
await db.read();
db.data ||= { notes: [] };
db.data.notes.push({ id: Date.now(), text: 'Hello' });
await db.write();
LowDB is perfect for small prototype apps.