We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
electron应用在开发中,需要存储数据到本地,经历了两个版本,其方案都不太一样。
electron
一开始考虑使用cookie,在开发过程中没有任何问题,但是编译之后去使用,发现无法操作cookie。原来在开发中直接js操作的的浏览器的cookie,而在electron中需要交由底层的nodejs去操作本地的cookie,官方说法是通过Session的cookies属性来访问Cookies的实例。但是我在实践过程中确实没有成功,然后随着需求变化,数据量变大,就直接放弃了这个方案。
Session
cookies
Cookies
当时用的electron版本是9.0.0,之后才用的方案是直接文件存储,即直接fs读与写,毫无问题。就是注意配置文件的存放位置。
9.0.0
fs
现在再去使用electron,版本已经到了11.0.0。当我去使用fs读写时直接给我报错fs.writeFile is not a function,经过一天多的排错和查找,最终放弃该方案,当然我并没有找到原因和解决方案。最后决定使用lowdb去实现存储。
11.0.0
fs.writeFile is not a function
lowdb
https://github.com/typicode/lowdb
Small JSON database for Node, Electron and the browser. Powered by Lodash. ⚡
npm install lowdb
const low = require('lowdb') const FileSync = require('lowdb/adapters/FileSync') const adapter = new FileSync('db.json') const db = low(adapter) // 默认初始化配置文件中 db.defaults({ posts: [], user: {}, count: 0 }) .write() // 增 db.get('posts') .push({ id: 1, title: 'lowdb is awesome'}) .write() // 删 db.get('posts') .remove({ title: 'low!' }) .write() // 改 db.set('user.name', 'typicode') .write() // 查 db.get('posts[0].title') .value()
比较重要的是,lowdb本身支持对于配置文件的加密,但是需要自己去实现写加解密的函数。
const adapter = new FileSync('db.json', { serialize: (data) => encrypt(JSON.stringify(data)), deserialize: (data) => JSON.parse(decrypt(data)) })
如下加解密方式可以参考下:
const algorithm = "aes-256-ctr"; const ENCRYPTION_KEY = "<ENCRYPTION_KEY>" const IV_LENGTH = 16; // 加密 function encrypt(text) { let iv = crypto.randomBytes(IV_LENGTH); let cipher = crypto.createCipheriv( algorithm, Buffer.from(ENCRYPTION_KEY, "hex"), iv ); cipher.setAutoPadding(true); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return iv.toString("hex") + ":" + encrypted.toString("hex"); } // 解密 function decrypt(text) { let textParts = text.split(":"); let iv = Buffer.from(textParts.shift(), "hex"); let encryptedText = Buffer.from(textParts.join(":"), "hex"); let decipher = crypto.createDecipheriv( algorithm, Buffer.from(ENCRYPTION_KEY, "hex"), iv ); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
简述
electron
应用在开发中,需要存储数据到本地,经历了两个版本,其方案都不太一样。一开始考虑使用cookie,在开发过程中没有任何问题,但是编译之后去使用,发现无法操作cookie。原来在开发中直接js操作的的浏览器的cookie,而在electron中需要交由底层的nodejs去操作本地的cookie,官方说法是通过
Session
的cookies
属性来访问Cookies
的实例。但是我在实践过程中确实没有成功,然后随着需求变化,数据量变大,就直接放弃了这个方案。当时用的
electron
版本是9.0.0
,之后才用的方案是直接文件存储,即直接fs
读与写,毫无问题。就是注意配置文件的存放位置。现在再去使用
electron
,版本已经到了11.0.0
。当我去使用fs
读写时直接给我报错fs.writeFile is not a function
,经过一天多的排错和查找,最终放弃该方案,当然我并没有找到原因和解决方案。最后决定使用lowdb
去实现存储。官方文档
Github仓库
https://github.com/typicode/lowdb
官方介绍
Small JSON database for Node, Electron and the browser. Powered by Lodash. ⚡
实现
安装
增删改查实例
加密
比较重要的是,
lowdb
本身支持对于配置文件的加密,但是需要自己去实现写加解密的函数。如下加解密方式可以参考下:
The text was updated successfully, but these errors were encountered: