Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist data in an Electron app?

Tags:

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 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?

like image 442
Andrew Avatar asked Feb 26 '16 19:02

Andrew


People also ask

How do you save data Electron?

Electron doesn't have the strict limitation of data flow. You can store the data in localStorage, or store the data in main thread and expose it to global. Or, you can also use electron-store for simple key-value storage. When the application grows bigger and bigger, the data flow will become harder to maintain.

Where do Electron apps store data?

Typically data is stored in the user's “app data” folder. Where this directory is varies by operating system. Electron provides app. getPath which returns the right directory, depending on your platform.

How does Electron store data in local storage?

Even though Chromium supports Local Storage, Electron does not provide us with a built-in way to store and persist user settings and other data within the local storage. However, with the help of external npm packages, we can persist and access data within an Electron application simply and efficiently.

What database should I use for Electron app?

MongoDB. MongoDB is a fantastic NoSQL database with a lot of resources. In MongoDB, a record is a document, which is a data structure with field and value pairs. It has a straightforward API that is simple to incorporate into your Electron application.


2 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.

like image 57
mertyildiran Avatar answered Sep 28 '22 05:09

mertyildiran


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"     });  }); 
like image 31
jviotti Avatar answered Sep 28 '22 06:09

jviotti