I've been scouring the Electron documentation to try and figure out how to persist data in an Electron app. For example, in iOS or OS X, you could use NSUserDefaults to store user settings and preferences. I would like to do something similar. How can I persist data in an Electron app?
9 Answers
NeDB is the only suggested or featured tool as an embedded persistent database for Electron by Electron, currently. - http://electron.atom.io/community/
It's also could be useful to store user settings if settings are complex.
Why NeDB could be a better solution on this case?
Embedded persistent or in memory database for Node.js, nw.js, Electron and browsers, 100% JavaScript, no binary dependency. API is a subset of MongoDB's and it's plenty fast. - NeDB
Creating or loading a database:
var Datastore = require('nedb')
, db = new Datastore({ filename: 'path/to/datafile', autoload: true });
// You can issue commands right away
Inserting a document:
var doc = { hello: 'world'
, n: 5
, today: new Date()
, nedbIsAwesome: true
, notthere: null
, notToBeSaved: undefined // Will not be saved
, fruits: [ 'apple', 'orange', 'pear' ]
, infos: { name: 'nedb' }
};
db.insert(doc, function (err, newDoc) { // Callback is optional
// newDoc is the newly inserted document, including its _id
// newDoc has no key called notToBeSaved since its value was undefined
});
Finding documents:
// Finding all inhabited planets in the solar system
db.find({ system: 'solar', inhabited: true }, function (err, docs) {
// docs is an array containing document Earth only
});
The list goes on...
Update - September 2019
As of 2019, this is no longer the valid answer. See the answers of @jviotti and @Tharanga below.
-
8
-
@mjwrazor sure, by the time states of the libraries can change. Feel free to send edits to this answer when you think the alternative libraries become mature and better than NeDB. Sep 20, 2017 at 18:17
-
2
-
1@RA. I have updated my answer to tell people, this is no longer the valid answer. Sep 1, 2019 at 23:00
-
1
There is an NPM module I wrote called electron-json-storage that is meant to abstract this out and provide a nice and easy interface to the developer.
The module internally reads/writes JSON to/from app.getPath('userData')
:
const storage = require('electron-json-storage');
// Write
storage.set('foobar', { foo: 'bar' }).then(function() {
// Read
storage.get('foobar').then(function(object) {
console.log(object.foo);
// will print "bar"
});
});
-
1jviotti, your example is showing Promises and on library callbacks are used. Thanks for sharing.– VjekoDec 1, 2016 at 7:36
-
Hi @Vjeko, the library initially used Promise, however we dropped support for them because of some technical issues (see github.com/jviotti/electron-json-storage/pull/9). You can promisify the module with something like Bluebird's promisifyAll() to get the same effect.– jviottiDec 4, 2016 at 3:25
There is a nice module for storing user data in elecron. It's called electron-store.
Installation
$ npm install electron-store
Sample usage (copied from github page)
const Store = require('electron-store');
const store = new Store();
store.set('unicorn', '🦄');
console.log(store.get('unicorn'));
//=> '🦄'
// Use dot-notation to access nested properties
store.set('foo.bar', true);
console.log(store.get('foo'));
//=> {bar: true}
store.delete('unicorn');
console.log(store.get('unicorn'));
//=> undefined
This module has many features and there are many advantages over window.localStorage
Electron views are built with Webkit which gives you access to the web based localstorage api. Good for simple and easy settings storage.
If you need something more powerful or need storage access from the main script, you can use one of the numerous node based storage modules. Personally I like lowdb.
With most node storage modules, you will need to provide a file location. Try:
var app = require('app');
app.getPath('userData');
-
Thanks, that's helpful. I'm also trying to wrap my head around where to put my model/controller logic. Do I write it in the same context as the browser window? Otherwise, how do I pass data from the electron js to the window js?– AndrewFeb 29, 2016 at 4:04
-
1Yes the browser window has full access to all that Electron offers. The main script is only really useful for starting up Electron, or if you're writing an app that primarily lives in the background.– TeakFeb 29, 2016 at 5:10
-
2Electron feels a bit strange at first, because of the way it merges Node and the DOM. It just feels strange having full access to all of node from inside a browser window.– TeakFeb 29, 2016 at 5:12
Since NeDB's latest release was 4 years ago and there are many open issues, I would not recommend it. But there are many other alternatives you can use now.
https://github.com/pubkey/rxdb (many features, observable queries)
https://github.com/pouchdb/pouchdb (simple but many open issues)
https://github.com/techfort/LokiJS (only in-memory storage)
https://github.com/typicode/lowdb (good for simple, small datasets)
You have multiple opions other than what mentioned in other answers.
If you want to store data in an SQL databse then you can https://github.com/mapbox/node-sqlite3
Or if you are storing configurations, You can store directly in OS's userData
storage.
const electron = require('electron');
const fs = require('fs');
const path = require('path');
const dataPath = electron.app.getPath('userData');
const filePath = path.join(dataPath, 'config.json');
function writeData(key, value){
let contents = parseData()
contents[key] = value;
fs.writeFileSync(filePath, JSON.stringify(contents));
}
function readData(key, value) {
let contents = parseData()
return contents[key]
}
function parseData(){
const defaultData = {}
try {
return JSON.parse(fs.readFileSync(filePath));
} catch(error) {
return defaultData;
}
}
There is a module that gives simple methods to get and set json files to this directory, creates subdirectories if needed and supports callbacks and promises:
https://github.com/ran-y/electron-storage
Readme:
Installation
$ npm install --save electron-storage
usage
const storage = require('electron-storage');
API
storage.get(filePath, cb)
storage.get(filePath, (err, data) => {
if (err) {
console.error(err)
} else {
console.log(data);
}
});
storage.get(filePath)
storage.get(filePath)
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
storage.set(filePath, data, cb)
storage.set(filePath, data, (err) => {
if (err) {
console.error(err)
}
});
storage.set(filePath, data)
storage.set(filePath, data)
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
storage.isPathExists(path, cb)
storage.isPathExists(path, (itDoes) => {
if (itDoes) {
console.log('pathDoesExists !')
}
});
storage.isPathExists(path)
storage.isPathExists(path)
.then(itDoes => {
if (itDoes) {
console.log('pathDoesExists !')
}
});
-
1This works great, especially for Electron 1.x applications that cannot use
electron-store
. The API is a little wonky with things likeisPathExists
but it does the job. Jun 29, 2018 at 5:07
You can go for Indexeddb, which is most likely suitable for client-side app needs due to:
- Its built-in versioning mechanism. Client-side applications often face version fragmentation as users don't usually update to new version at the same time. So checking version of existing database and update accordingly is a good idea.
- It's schemaless, which allows flexibility of adding more data to client's storage (which happen pretty often in my experience) without having to update database to a new version, unless you create new indices.
- It support wide ranges of data: basic types as well as blob data (file, images)
All in all it's a good choice. The only caveat is that chromium cores may automatically wipe out indexeddb to reclaim disk space when storage is under strain if navigator.storage.persist
is not set, or when the host machine is crashed leaving indexeddb in corrupted state.
-
Care to add some references to
the navigator.storage.persist api does not work on Electron
? Dec 18, 2018 at 14:02 -
1I don't have a reference. It is from my experience that sometimes the whole storage was wiped out on a client's machine had the host machine been crashed for some reason. Having said that, our use of indexeddb is pretty heavy compared to normal use. And I am updating my answer to clarify what I meant. Thank for pointing out.– vuamitomDec 18, 2018 at 15:10
-
1Another problem I have faced with IndexedDB, in electron specifically is that it doesn't work in node.js, so you need to actually maintain it from your renderer process.– RA.Jul 23, 2019 at 13:32
-
1Exactly. For our application, data resides in the renderer process for main UI window. Any read and updates happen on that process. That way the app is usable both on the browser and on Electron runtime.– vuamitomJul 24, 2019 at 2:28
-
all the browser storage can be wiped out at any time, due to storage limits and LRU purging of stored data as @vuamitom implied above. This includes local storage (and session storage of course since it is purged right away) but also IndexDB. I find it's suitable for temporary offline storage, until you can get the local data synced with something else. Aug 31, 2019 at 6:36
There are a plethora of ways for data persistence that can be used in Electron and choosing the right approach depends essentially on your use case(s). If it's only about saving application settings then you can use simple mechanisms such as flat files or HTML5 Storage APIs, for advanced data requirements you should opt for large scale database solutions such as MySQL or MongoDB (with or without ORMs).
You can check this list of methods/tools to persist data in Electron apps
app.getPath('userData')
path for each OS has a Local Storage, Session Storage and IndexedDB path (at least on Mac, not sure why my Windows does not have IndexedDB directory out of the gate). That said, I'm 95+% certain that local storage persists between app upgrades! And it is an incredibly simple option to implement. IndexedDB may help for blobs/etc. Worth considering!