How to persist data in an Electron app?

Solution 1:

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.

Solution 2:

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"
    });

});

Solution 3:

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

Solution 4:

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');