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?
相关问题
- How can I get a window object from new BrowserWind
- Failed at the electron@1.8.2 postinstall script
- No member named ForceSet
- Export HTMLtable to CSV in electron
- Using electron-usb with electron
相关文章
- Configuring electron-webpack renderer to work with
- Error messages and console logs in Electron?
- Adding Apple in-app purchase to Electron HTML/JS a
- Minimal Example: Opening a window in electron from
- Electron dying without any information, what now?
- Electron without GUI
- Testing Electron application with org.openqa.selen
- How to build the Electron application for the Wind
You can go for Indexeddb, which is most likely suitable for client-side app needs due to:
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.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?
Creating or loading a database:
Inserting a document:
Finding documents:
The list goes on...
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
usage
API
storage.get(filePath, cb)
storage.get(filePath)
storage.set(filePath, data, cb)
storage.set(filePath, data)
storage.isPathExists(path, cb)
storage.isPathExists(path)
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:
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
There is a nice module for storing user data in elecron. It's called electron-store.
Installation
Sample usage (copied from github page)