我一直在搜索Electron文档,试图找出如何在Electron应用程序中保存数据.例如,在iOS或OS X中,您可以使用NSUserDefaults来存储用户设置和首选项.我想做类似的事情.如何在Electron应用程序中保留数据?
mer*_*ran 30
目前,NeDB是Electron by Electron的唯一建议或特色工具,作为嵌入式持久数据库.- http://electron.atom.io/community/
如果设置很复杂,那么存储用户设置也很有用.
嵌入式持久性或内存数据库,用于Node.js,nw.js,Electron和浏览器,100%JavaScript,无二进制依赖.API是MongoDB的一个子集,速度很快.- NeDB
创建或加载数据库:
var Datastore = require('nedb')
, db = new Datastore({ filename: 'path/to/datafile', autoload: true });
// You can issue commands right away
Run Code Online (Sandbox Code Playgroud)
插入文件:
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
});
Run Code Online (Sandbox Code Playgroud)
查找文件:
// 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
});
Run Code Online (Sandbox Code Playgroud)
名单继续......
jvi*_*tti 17
我编写了一个名为electron-json-storage的NPM模块,旨在将其抽象出来并为开发人员提供一个简单易用的界面.
该模块内部读取/写入JSON 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"
});
});
Run Code Online (Sandbox Code Playgroud)
小智 10
由于 NeDB 的最新版本是 4 年前的,并且有很多未解决的问题,我不推荐它。但是您现在可以使用许多其他替代方法。
https://github.com/pubkey/rxdb(很多功能,可观察的查询)
https://github.com/pouchdb/pouchdb(简单但许多未解决的问题)
https://github.com/techfort/LokiJS(仅内存存储)
https://github.com/typicode/lowdb(适用于简单的小型数据集)
电子视图使用Webkit构建,使您可以访问基于Web的localstorage api.适合简单易用的设置存储.
如果您需要更强大的功能或需要从主脚本进行存储访问,则可以使用众多基于节点的存储模块之一.我个人喜欢lowdb.
对于大多数节点存储模块,您需要提供文件位置.尝试:
var app = require('app');
app.getPath('userData');
Run Code Online (Sandbox Code Playgroud)
有一个很好的模块用于在elecron中存储用户数据.它被称为电子商店.
安装
$ npm install electron-store
Run Code Online (Sandbox Code Playgroud)
示例用法(从github页面复制)
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
Run Code Online (Sandbox Code Playgroud)
该模块具有许多功能,与window.localStorage相比有许多优点
有一个模块提供了简单的方法来获取和设置json文件到这个目录,如果需要创建子目录并支持回调和承诺:
https://github.com/ran-y/electron-storage
自述:
安装
$ npm install --save electron-storage
Run Code Online (Sandbox Code Playgroud)
用法
const storage = require('electron-storage');
Run Code Online (Sandbox Code Playgroud)
API
storage.get(filePath,cb)
storage.get(filePath, (err, data) => {
if (err) {
console.error(err)
} else {
console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)
storage.get(文件路径)
storage.get(filePath)
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
Run Code Online (Sandbox Code Playgroud)
storage.set(filePath,data,cb)
storage.set(filePath, data, (err) => {
if (err) {
console.error(err)
}
});
Run Code Online (Sandbox Code Playgroud)
storage.set(filePath,data)
storage.set(filePath, data)
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
Run Code Online (Sandbox Code Playgroud)
storage.isPathExists(path,cb)
storage.isPathExists(path, (itDoes) => {
if (itDoes) {
console.log('pathDoesExists !')
}
});
Run Code Online (Sandbox Code Playgroud)
storage.isPathExists(路径)
storage.isPathExists(path)
.then(itDoes => {
if (itDoes) {
console.log('pathDoesExists !')
}
});
Run Code Online (Sandbox Code Playgroud)
除了其他答案中提到的之外,您还有多种选择。
如果要将数据存储在 SQL 数据库中,则可以https://github.com/mapbox/node-sqlite3
或者,如果您正在存储配置,您可以直接存储在操作系统的userData存储中。
const electron = require('electron');
const fs = require('fs');
const path = require('path');
const dataPath = electron.app.getPath('userData');
const filePath = path.join(dataPath, 'config.json');
function writeData(key, value){
let contents = parseData()
contents[key] = value;
fs.writeFileSync(filePath, JSON.stringify(contents));
}
function readData(key, value) {
let contents = parseData()
return contents[key]
}
function parseData(){
const defaultData = {}
try {
return JSON.parse(fs.readFileSync(filePath));
} catch(error) {
return defaultData;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41104 次 |
| 最近记录: |