Installations
npm install electron-json-storage
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.10.0
NPM Version
8.19.2
Score
79.9
Supply Chain
98.7
Quality
74.3
Maintenance
100
Vulnerability
98.2
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.8%)
Shell (0.2%)
Developer
electron-userland
Download Statistics
Total Downloads
5,771,790
Last Day
1,154
Last Week
11,864
Last Month
103,193
Last Year
1,183,512
GitHub Statistics
1,440 Stars
208 Commits
80 Forks
21 Watching
6 Branches
20 Contributors
Package Meta Information
Latest Version
4.6.0
Package Id
electron-json-storage@4.6.0
Unpacked Size
113.63 kB
Size
19.27 kB
File Count
16
NPM Version
8.19.2
Node Version
18.10.0
Publised On
12 Oct 2022
Total Downloads
Cumulative downloads
Total Downloads
5,771,790
Last day
-72.7%
1,154
Compared to previous day
Last week
-53.5%
11,864
Compared to previous week
Last month
-10.5%
103,193
Compared to previous month
Last year
48.8%
1,183,512
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
6
electron-json-storage
Easily write and read user settings in Electron apps
Electron lacks an easy way to persist and read user settings for your application. electron-json-storage
implements an API somewhat similar to localStorage to write and read JSON objects to/from the operating system application data directory, as defined by app.getPath('userData')
.
Related modules:
Installation
Install electron-json-storage
by running:
1$ npm install --save electron-json-storage
You can require this module from either the main or renderer process (with and without remote
).
Running on Electron >10 renderer processes
When loaded in renderer processes, this module will try to make use of
electron.remote
in order to fetch the userData
path.
Electron 10 now defaults enableRemoteModule
to
false,
which means that electron-json-storage
will be able to calculate a data path by default.
The solution is to manually call storage.setDataPath()
before reading or
writing any values or setting enableRemoteModule
to true
.
Documentation
- storage
- .getDefaultDataPath() ⇒
String
|Null
- .setDataPath(directory)
- .getDataPath() ⇒
String
- .get(key, [options], callback)
- .getSync(key, [options])
- .getMany(keys, [options], callback)
- .getAll([options], callback)
- .set(key, json, [options], callback)
- .has(key, [options], callback)
- .keys([options], callback)
- .remove(key, [options], callback)
- .clear([options], callback)
- .getDefaultDataPath() ⇒
storage.getDefaultDataPath() ⇒ String
| Null
This function will return null
when running in the
renderer process without support for the remote
IPC
mechanism. You have to explicitly set a data path using
.setDataPath()
in these cases.
Kind: static method of storage
Summary: Get the default data path
Returns: String
| Null
- default data path
Access: public
Example
1const defaultDataPath = storage.getDefaultDataPath()
storage.setDataPath(directory)
The default value will be used if the directory is undefined.
Kind: static method of storage
Summary: Set current data path
Access: public
Param | Type | Description |
---|---|---|
directory | String | Undefined | directory |
Example
1const os = require('os'); 2const storage = require('electron-json-storage'); 3 4storage.setDataPath(os.tmpdir());
storage.getDataPath() ⇒ String
Returns the current data path. It defaults to a directory called
"storage" inside Electron's userData
path.
Kind: static method of storage
Summary: Get current user data path
Returns: String
- the user data path
Access: public
Example
1const storage = require('electron-json-storage'); 2 3const dataPath = storage.getDataPath(); 4console.log(dataPath);
storage.get(key, [options], callback)
If the key doesn't exist in the user data, an empty object is returned.
Also notice that the .json
extension is added automatically, but it's
ignored if you pass it yourself.
Passing an extension other than .json
will result in a file created
with both extensions. For example, the key foo.data
will result in a file
called foo.data.json
.
Kind: static method of storage
Summary: Read user data
Access: public
Param | Type | Description |
---|---|---|
key | String | key |
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error, data) |
Example
1const storage = require('electron-json-storage'); 2 3storage.get('foobar', function(error, data) { 4 if (error) throw error; 5 6 console.log(data); 7});
storage.getSync(key, [options])
See .get()
.
Kind: static method of storage
Summary: Read user data (sync)
Access: public
Param | Type | Description |
---|---|---|
key | String | key |
[options] | Object | options |
[options.dataPath] | String | data path |
Example
1const storage = require('electron-json-storage'); 2 3var data = storage.getSync('foobar'); 4console.log(data);
storage.getMany(keys, [options], callback)
This function returns an object with the data of all the passed keys. If one of the keys doesn't exist, an empty object is returned for it.
Kind: static method of storage
Summary: Read many user data keys
Access: public
Param | Type | Description |
---|---|---|
keys | Array.<String> | keys |
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error, data) |
Example
1const storage = require('electron-json-storage'); 2 3storage.getMany([ 'foobar', 'barbaz' ], function(error, data) { 4 if (error) throw error; 5 6 console.log(data.foobar); 7 console.log(data.barbaz); 8});
storage.getAll([options], callback)
This function returns an empty object if there is no data to be read.
Kind: static method of storage
Summary: Read all user data
Access: public
Param | Type | Description |
---|---|---|
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error, data) |
Example
1const storage = require('electron-json-storage'); 2 3storage.getAll(function(error, data) { 4 if (error) throw error; 5 6 console.log(data); 7});
storage.set(key, json, [options], callback)
Kind: static method of storage
Summary: Write user data
Access: public
Param | Type | Description |
---|---|---|
key | String | key |
json | Object | json object |
[options] | Object | options |
[options.dataPath] | String | data path |
[options.validate] | String | validate writes by reading the data back |
[options.prettyPrinting] | boolean | adds line breaks and spacing to the written data |
callback | function | callback (error) |
Example
1const storage = require('electron-json-storage'); 2 3storage.set('foobar', { foo: 'bar' }, function(error) { 4 if (error) throw error; 5});
storage.has(key, [options], callback)
Kind: static method of storage
Summary: Check if a key exists
Access: public
Param | Type | Description |
---|---|---|
key | String | key |
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error, hasKey) |
Example
1const storage = require('electron-json-storage'); 2 3storage.has('foobar', function(error, hasKey) { 4 if (error) throw error; 5 6 if (hasKey) { 7 console.log('There is data stored as `foobar`'); 8 } 9});
storage.keys([options], callback)
Kind: static method of storage
Summary: Get the list of saved keys
Access: public
Param | Type | Description |
---|---|---|
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error, keys) |
Example
1const storage = require('electron-json-storage'); 2 3storage.keys(function(error, keys) { 4 if (error) throw error; 5 6 for (var key of keys) { 7 console.log('There is a key called: ' + key); 8 } 9});
storage.remove(key, [options], callback)
Notice this function does nothing, nor throws any error if the key doesn't exist.
Kind: static method of storage
Summary: Remove a key
Access: public
Param | Type | Description |
---|---|---|
key | String | key |
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error) |
Example
1const storage = require('electron-json-storage'); 2 3storage.remove('foobar', function(error) { 4 if (error) throw error; 5});
storage.clear([options], callback)
Kind: static method of storage
Summary: Clear all stored data in the current user data path
Access: public
Param | Type | Description |
---|---|---|
[options] | Object | options |
[options.dataPath] | String | data path |
callback | function | callback (error) |
Example
1const storage = require('electron-json-storage'); 2 3storage.clear(function(error) { 4 if (error) throw error; 5});
Support
If you're having any problem, please raise an issue on GitHub and we'll be happy to help.
Tests
Run the test suite by doing:
1$ npm test
Contribute
- Issue Tracker: github.com/electron-userland/electron-json-storage/issues
- Source Code: github.com/electron-userland/electron-json-storage
Before submitting a PR, please make sure that you include tests, and that jshint runs without any warning:
1$ npm run-script lint
License
The project is licensed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 2/5 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Reason
30 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-mpjm-v997-c4h4
- Warn: Project is vulnerable to: GHSA-3p22-ghq8-v749
- Warn: Project is vulnerable to: GHSA-77xc-hjv8-ww97
- Warn: Project is vulnerable to: GHSA-mq8j-3h7h-p8g7
- Warn: Project is vulnerable to: GHSA-p2jh-44qj-pf2v
- Warn: Project is vulnerable to: GHSA-p7v2-p9m8-qqg7
- Warn: Project is vulnerable to: GHSA-7x97-j373-85x5
- Warn: Project is vulnerable to: GHSA-7m48-wc93-9g85
- Warn: Project is vulnerable to: GHSA-qqvq-6xgj-jw8g
- Warn: Project is vulnerable to: GHSA-2j2x-2gpw-g8fm
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-6vfc-qv3f-vr6c
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-px4h-xg32-q955
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-mxhp-79qh-mcx6
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
Score
2.1
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More