npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-json-storage-alt

v18.2.1

Published

Easily write and read user settings in Electron apps

Downloads

15

Readme

electron-json-storage

Easily write and read user settings in Electron apps

npm version dependencies Build Status Build status

Electron lacks an easy way to persist and read user settings for your application. electron-json-storage implements an API somehow 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:

$ npm install --save electron-json-storage

You can require this module from only the main process (remote module is deprecated after Electron 12).

Use in renderer process

set preload.js as preload file of BrowserWindow/webview

'use strict';

const storage = require('electron-json-storage-alt');
const { promisify } = require('util');
const { ipcRenderer, contextBridge } = require('electron');

contextBridge.exposeInMainWorld(
  '_electron_bridge',
  {
    storage: {
      get: promisify(storage.get),
      set: promisify(storage.set),
    },
  }
);

get storage from contextBridge:

const saveToStorage = async () => {
  await window._electron_bridge.storage.set('test_storage_current_time', new Date());
  const time = await window._electron_bridge.storage.get('test_storage_current_time');
  console.log('test_storage_current_time', time);
}

Documentation

storage.getDefaultDataPath() ⇒ String

Kind: static method of storage
Summary: Get the default data path
Returns: String - default data path
Access: public
Example

const 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

const os = require('os');
const storage = require('electron-json-storage');

storage.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

const storage = require('electron-json-storage');

const dataPath = storage.getDataPath();
console.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

const storage = require('electron-json-storage');

storage.get('foobar', function(error, data) {
  if (error) throw error;

  console.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

const storage = require('electron-json-storage');

storage.getMany([ 'foobar', 'barbaz' ], function(error, data) {
  if (error) throw error;

  console.log(data.foobar);
  console.log(data.barbaz);
});

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

const storage = require('electron-json-storage');

storage.getAll(function(error, data) {
  if (error) throw error;

  console.log(data);
});

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 | | callback | function | callback (error) |

Example

const storage = require('electron-json-storage');

storage.set('foobar', { foo: 'bar' }, function(error) {
  if (error) throw error;
});

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

const storage = require('electron-json-storage');

storage.has('foobar', function(error, hasKey) {
  if (error) throw error;

  if (hasKey) {
    console.log('There is data stored as `foobar`');
  }
});

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

const storage = require('electron-json-storage');

storage.keys(function(error, keys) {
  if (error) throw error;

  for (var key of keys) {
    console.log('There is a key called: ' + key);
  }
});

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

const storage = require('electron-json-storage');

storage.remove('foobar', function(error) {
  if (error) throw error;
});

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

const storage = require('electron-json-storage');

storage.clear(function(error) {
  if (error) throw error;
});

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:

$ npm test

Contribute

Before submitting a PR, please make sure that you include tests, and that jshint runs without any warning:

$ npm run-script lint

License

The project is licensed under the MIT license.