Gathering detailed insights and metrics for electron-json-storage
Gathering detailed insights and metrics for electron-json-storage
Gathering detailed insights and metrics for electron-json-storage
Gathering detailed insights and metrics for electron-json-storage
npm install electron-json-storage
Typescript
Module System
Node Version
NPM Version
79.9
Supply Chain
98.7
Quality
74.3
Maintenance
100
Vulnerability
98.2
License
JavaScript (99.8%)
Shell (0.2%)
Total Downloads
5,771,790
Last Day
1,154
Last Week
11,864
Last Month
103,193
Last Year
1,183,512
1,440 Stars
208 Commits
80 Forks
21 Watching
6 Branches
20 Contributors
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
Cumulative downloads
Total Downloads
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
6
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:
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
).
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
.
String
| Null
String
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()
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());
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);
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});
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);
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});
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});
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});
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});
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});
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});
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});
If you're having any problem, please raise an issue on GitHub and we'll be happy to help.
Run the test suite by doing:
1$ npm test
Before submitting a PR, please make sure that you include tests, and that jshint runs without any warning:
1$ npm run-script lint
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
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
30 existing vulnerabilities detected
Details
Score
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