How to persist data in an Electron app?

electron-store npm
electron-store angular
electron-json-storage
electron database
electron sqlite
nedb electron
electron app example
build electron app

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?


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.

How to persist data in an Electron app?, NeDB is the only suggested or featured tool as an embedded persistent database for Electron by Electron, currently. Thinking about how to persist data is a key step when developing applications, desktop apps make no exception. The way you use to handle data in your application affects your app performance and as a result the final user experience. There is a plethora of available mechanisms, for persisting data,


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

});

sindresorhus/electron-store: Simple data persistence for , Electron doesn't have a built-in way to persist user preferences and other data. This module handles that for you, so you can focus on building your app. The data is saved in a JSON file in app. getPath('userData') . Here is some advice if you plan to grow an Electron application that persists data on disk: 🤔 Early on, define an API between the persisted data and the redux’s state. It will ease following refactoring. You can rely on redux-persist as it can be extended. 🎓 If your persisted data model gets


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

How to Save Data Locally in Electron, If your application is storing any secret data like passwords and API keys, you may not want to store those using any of the methods explained  As a better alternative to using bare sqlite3 with Electron, I built a module called trilogy.It uses Promises and has support for 3 backends: sqlite3 if you want to rebuild as this article points out, sql.js for a pure JS alternative, and in-memory for fast non-persistent data.


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

Electron Tutorial: Data Persistence, Persisting Data in Electron Apps. First of all, why would you want to do that i.e. persist data? In most cases an application needs to persist data, for  The 3 most common ways to persist user data with an example. Most Electron apps need some sort of way to save user data. This could be user preferences (e.g. show/don’t show notifications) or some kind of application data (e.g. last window size and position).


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

How does one persist data in electron - electron, In my electron app I want to persist some data locally e.g. user data, list of file paths, configuration data. How can I do that, any minimalist approach for the same. Electron doesn't have a built-in way to persist user preferences and other data. This module handles that for you, so you can focus on building your app. The data is saved in a JSON file in app.getPath('userData'). You can use this module directly in both the main and renderer process.


electron-store, Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc. Questions tagged [electron] 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


electron-json-storage, Easily write and read user settings in Electron apps. Electron lacks an easy way to persist and read user settings for your and read JSON objects to/from the operating system application data directory, as defined by app. Linux: ~/.config/<App Name> Mac OS: ~/Library/Application Support/<App Name> Windows: C:\Users\<user>\AppData\Local\<App Name> In Electron we can use app.getPath('userData') to get the correct folder. Then we make a function to write our data to the disk or we use a library that does it for us. Using a Library to Store Data. Storing data in


How to store user data in Electron, So how do we save user settings in an Electron app? And where do we save them to? Because we have access to both browser and node.js APIs  If partition starts with persist:, the page will use a persistent session available to all pages in the app with the same partition. if there is no persist: prefix, the page will use an in-memory session. If the partition is empty then default session of the app will be returned.