0.1.0 • Published 4 years ago

singleton-store v0.1.0

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
4 years ago

singleton-store

Version David (path)

A simple key-value store singleton.

Sometimes you have to do horrible things, like use the global object to share a singleton or some values.

Instead of attaching values to the global object, which can lead to security concerns, this singleton-storage package provides a key-value store which is guaranteed to be a singleton, so you can use it instead of the global object. Only code that are part of the same application bundle can access this store, making it more secured.

Example

package A

import { getStore } from 'singleton-storage';
const store = getStore();
store.set('share.config.something', 123);

package B

import { getStore } from 'singleton-storage';
const store = getStore();
store.get('share.config.something'); // 123;

Installation

For this to work correctly, there must be only a single copy of singleton-storage in node_modules at all times. (Similar to how there must be only a single copy of react.)

  • For application developers, just do a regular installation. If you want to use singleton-storage directly, or have some libraries that depends on it.
npm install singleton-storage
  • For library authors, you must always list this as peerDependencies in package.json. Listing it as dependencies may cause the library consumers to have duplicates in the final application.

In library

{
  "peerDependencies": {
    "singleton-storage": "*"
  },
  "devDependencies": {
    "singleton-storage": "x.x"
  }
}

vs. in application

{
  "dependencies": {
    "singleton-storage": "x.x"
  }
}

Available functions

import { getStore } from 'singleton-storage';
const store = getStore();
store.has(key);
store.get<string>(key); // must specify value type via generic
store.getOrCreate(key, factory);
store.remove(key);
store.set(key);

Credits

Inspired by global-cache

License

Apache-2.0