1https://github.com/sindresorhus/electron-store
electron-store
1能夠用來保存Electron
應用程序或模塊的簡單數據持久性-保存和加載用戶首選項,應用程序狀態,緩存等。html
Electron
沒有內置的方法來保留用戶首選項和其餘數據。electron-store
模塊能夠爲您解決該問題,所以您能夠專一於構建應用程序。 數據保存在app.getPath('userData')
2中的JSON
文件中。您能夠在主進程和渲染器進程中直接使用此模塊。vue
2https://electronjs.org/docs/api/app#appgetpathnamenode
app.getPath(name)
-儲存你應用程序設置文件的文件夾,默認是appData
文件夾附加應用的名稱。git
appData
-當前用戶的應用數據文件夾,默認對應:github
%APPDATA%
Windows 中$XDG_CONFIG_HOME
or~/.config
Linux 中~/Library/Application Support
macOS 中算法
electron-store
模塊的API更好。 您能夠設置並獲取嵌套屬性。 您能夠設置默認的初始配置。注:不少同窗以爲用localstorage能夠代替vuex, 對於不變的數據確實能夠,可是當兩個組件共用一個數據源(對象或數組)時,若是其中一個組件改變了該數據源,但願另外一個組件響應該變化時,localstorage沒法作到,緣由就是區別1。vuex
$ npm install electron-store
注:須要Electron 5或更高版本。若是安裝失敗,能夠換成命令
cnpm install electron-store
(前提是安裝了cnpm)npm
const Store = require('electron-store'); const store = new Store(); store.set('unicorn', '🦄'); console.log(store.get('unicorn')); //=> '🦄' // 使用點表示法訪問嵌套屬性 store.set('foo.bar', true); console.log(store.get('foo')); //=> {bar: true} store.delete('unicorn'); console.log(store.get('unicorn')); //=> undefined
更改是原子寫入磁盤的,所以,若是進程在寫入過程當中崩潰,則不會破壞現有配置。json
返回:一個新實例。api
options
Type: object
defaults
Type: object
// Default values for the store items.
// 注意:默認值將覆蓋schema選項中的默認鍵。
schema
type: object
注:JSON Schema(https://json-schema.org/) 就是用來定義json數據約束的一個標準。根據這個約定模式,交換數據的雙方能夠理解json數據的要求和約束,也能夠據此對數據進行驗證,保證數據交換的正確性。目前最新的Json-schema版本是draft 7,發佈於2018-03-19。
在後臺,JSON模式(JSON Schema)驗證器ajv3用於驗證您的配置。 咱們使用JSON Schema draft-074並支持全部驗證關鍵字5和格式6。
您應該將模式定義爲一個對象,其中每一個鍵是數據屬性的名稱,每一個值是用於驗證該屬性的JSON模式。 在這裏查看更多7。
3https://github.com/epoberezkin/ajv
4http://json-schema.org/latest/json-schema-validation.html
5https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md
6https://github.com/epoberezkin/ajv#formats
7https://json-schema.org/understanding-json-schema/reference/object.html#properties
例如:
const Store = require('electron-store'); const schema = { foo: { type: 'number', maximum: 100, minimum: 1, default: 50 }, bar: { type: 'string', format: 'url' } }; const store = new Store({schema}); console.log(store.get('foo')); //=> 50 store.set('foo', '1'); // [Error: Config schema violation: `foo` should be number]
注意:若是設置了默認值,則默認值將被覆蓋。
migrations
Type: object
注:在解決此問題(https://github.com/sindresorhus/conf/issues/92)以前,請不要使用此功能。
每當升級版本時,您均可以使用遷移(migrations)對存儲執行操做。
遷移對象應包含「版本」:處理程序的鍵值對。 版本也能夠是semver range(https://github.com/npm/node-semver#ranges)。例如:
const Store = require('electron-store'); const store = new Store({ migrations: { '0.0.1': store => { store.set('debugPhase', true); }, '1.0.0': store => { store.delete('debugPhase'); store.set('phase', '1.0.0'); }, '1.0.2': store => { store.set('phase', '1.0.2'); }, '>=2.0.0': store => { store.set('phase', '>=2.0.0'); } } });
name
Type: string
Default: config
存儲文件的名稱(不帶擴展名)。
若是您想要爲應用程序提供多個存儲文件,這將頗有用。 或者,若是您要製做一個可持久使用的Electron模塊以保留一些數據,則在這種狀況下,您不該使用名稱config。
cwd
Type: string
Default: app.getPath('userData')
存儲文件位置。 除非絕對必要,不然請勿指定! 默認狀況下,它將經過遵循系統約定來選擇最佳位置。 您極可能會誤解並惹惱用戶。
若是是相對路徑,則相對於默認cwd。 例如在macOS 中,{cwd:'unicorn'}
將在〜/Librar/Application\ Support/App\ Name/unicorn
中生成一個存儲文件。
encryptionKey
Type: string | Buffer | TypedArray | DataView
Default: undefined
若是加密密鑰以安全方式(不是純文本)存儲在Node.js
應用程序中,則可用於保護敏感數據。 例如,經過使用node-keytar
(https://github.com/atom/node-keytar)安全地存儲加密密鑰,或向用戶詢問加密密鑰(密碼),而後將其存儲在變量中。
除了安全性,這還能夠用於混淆。 若是用戶瀏覽config
目錄並找到配置文件,由於它只是一個JSON
文件,所以他們可能會傾向於修改它。 經過提供加密密鑰,該文件將被混淆,這有望阻止任何用戶這樣作。
它還具備確保配置文件完整性的好處。 若是以任何方式更改文件,則解密將不起做用,在這種狀況下,存儲將僅重置爲默認狀態。
指定後,將使用aes-256-cbc
加密算法對存儲進行加密。
fileExtension
Type: string
Default: json
配置文件的擴展名。
一般,您不須要這樣作,可是若是您想與帶有可與您的應用程序關聯的自定義文件擴展名的文件進行交互,則可能會頗有用。 這些多是簡單的保存/導出/首選項文件,旨在在應用程序外部共享或保存。
clearInvalidConfig
Type: boolean
Default: true
若是讀取配置文件致使SyntaxError(語法錯誤),則清除該配置。 這是一個很好的默認設置,由於該配置文件不適合手動編輯,所以一般意味着該配置已損壞,用戶對此無能爲力。 可是,若是讓用戶直接編輯配置文件,則可能會發生錯誤,而且當配置無效而不是清除時引起錯誤可能更有用。 禁用此選項將使其在無效的配置上引起SyntaxError而不是清除。
serialize
Type: Function
Default: value => JSON.stringify(value, null, '\t')
寫入配置文件時將配置對象序列化爲UTF-8字符串的函數。您一般不須要此功能,可是若是您想使用JSON之外的格式,則可能會頗有用。
deserialize
Type: Function
Default: JSON.parse
讀取配置文件時從UTF-8字符串反序列化配置對象的功能。您一般不須要此功能,可是若是您想使用JSON之外的格式,則可能會頗有用。
accessPropertiesByDotNotation
Type: boolean
Default: true
經過點表示法訪問嵌套屬性。 例如:
const Store = require('electron-store'); const store = new Store(); store.set({ foo: { bar: { foobar: '🦄' } } }); console.log(store.get('foo.bar.foobar')); //=> '🦄'
另外,您能夠將此選項設置爲false,以便將整個字符串視爲一個鍵。
const store = new Store({accessPropertiesByDotNotation: false}); store.set({ `foo.bar.foobar`: '🦄' }); console.log(store.get('foo.bar.foobar')); //=> '🦄'
watch
Type: boolean
Default: false
監視配置文件中的全部更改,若是已設置,則調用onDidChange的回調。 若是有多個進程更改同一個配置文件,這將頗有用。當前,此選項在macOS上的Node.js 8上不起做用。
您能夠在鍵中使用點符號來訪問嵌套屬性。該實例(Instance)是可迭代的,所以您能夠在for…of循環中直接使用它。
.set(key, value)
設置一個項目。該值必須是JSON可序列化的。 嘗試將類型設置爲undefined,function或symbol會致使TypeError。
.set(object)
一次設置多個項目。
.get(key, [defaultValue])
獲取一個項目或defaultValue(若是該項目不存在)。
.reset(…keys)
將項目重置爲其默認值(由defaults或schema選項定義)。
.has(key)
檢查項目是否存在。
.delete(key)
刪除項目。
.clear()
刪除全部項目。
.onDidChange(key, callback)callback
: (newValue, oldValue) => {}
監視給定的鍵,對任何更改調用回調。 第一次設置鍵時,oldValue將是不肯定的,而刪除鍵時,newValue將是不肯定的。
事件僅在同一過程當中觸發。 所以,若是在渲染器進程中觸發事件,則不會在主進程中得到事件。 參見#39(https://github.com/sindresorhus/electron-store/issues/39)。
.onDidAnyChange(callback)callback
: (newValue, oldValue) => {}
監視整個配置對象,對任何更改調用回調。 oldValue和newValue將分別是更改先後的配置對象。 您必須將oldValue與newValue進行比較,以瞭解發生了什麼變化。
.size
獲取項目總個數。
.store
獲取全部數據做爲對象或將當前數據替換爲對象:
conf.store = { hello: 'world' };
.path
獲取存儲文件的路徑。
.openInEditor()
在用戶的編輯器中打開存儲文件。
一、我可使用YAML或其餘序列化格式嗎?
只要表示形式與utf8
編碼兼容,就可使用serialize
和反序列化選項來自定義配置文件的格式。使用YAML
的示例:
const Store = require('electron-store'); const yaml = require('js-yaml'); const store = new Store({ fileExtension: 'yaml', serialize: yaml.safeDump, deserialize: yaml.safeLoad });
7、相關
ahttps://github.com/sindresorhus/electron-util
bhttps://github.com/sindresorhus/electron-debug
chttps://github.com/sindresorhus/electron-context-menu
dhttps://github.com/sindresorhus/electron-dl
ehttps://github.com/sindresorhus/electron-unhandled
fhttps://github.com/sindresorhus/electron-reloader
ghttps://github.com/sindresorhus/electron-serve
hhttps://github.com/sindresorhus/conf