如何在Electron应用程序中保留数据? [英] How to persist data in an Electron app?

查看:244
本文介绍了如何在Electron应用程序中保留数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索 Electron 文档,以尝试弄清楚如何在Electron应用程序中持久存储数据。例如,在iOS或OS X中,您可以使用NSUserDefaults来存储用户设置和首选项。我想做类似的事情。如何在电子应用程序中保留数据?

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 是目前唯一建议或推荐的工具,它是Electron by Electron的嵌入式持久数据库。 - http://electron.atom.io/community/

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.


针对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

插入文档:

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 all inhabited planets in the solar system
db.find({ system: 'solar', inhabited: true }, function (err, docs) {
  // docs is an array containing document Earth only
});

列表继续...

截至2019年,这不再是有效答案。请参见下面的 @jviotti @Tharanga 的答案。

As of 2019, this is no longer the valid answer. See the answers of @jviotti and @Tharanga below.

这篇关于如何在Electron应用程序中保留数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆