Lesson 11 of 28
javascriptData Persistence – electron-store, SQLite & LowDB
11 mins
Course progress39%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 11 of 28
javascript11 mins
Keep these ideas in mind as you work through the lesson.
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').
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.
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.