How to persist data in an Electron app?

2019-03-08 19:15发布

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?

标签: electron
7条回答
劳资没心,怎么记你
2楼-- · 2019-03-08 19:23

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-03-08 19:27

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

查看更多
戒情不戒烟
4楼-- · 2019-03-08 19:27

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 !')
  }
});
查看更多
Summer. ? 凉城
5楼-- · 2019-03-08 19:31

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');
查看更多
混吃等死
6楼-- · 2019-03-08 19:32

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

查看更多
萌系小妹纸
7楼-- · 2019-03-08 19:39

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', '                                                                    
查看更多
登录 后发表回答