Skip to content

smolak/stash-it-plugin-readOnly

Repository files navigation

logo-stash-it-color-dark 2x

stash-it-plugin-readOnly

build status Coverage Status

Read-only plugin for stash-it.

The problem

Let's say you need to have an instance of stash-it that is only capable of reading from the storage, but not writing, changing or deleting anything.

Solution

This plugin will allow you to do just that. And if anyone will try to write / delete something, an error will be thrown.

Installation

npm i stash-it-plugin-readonly --save

Usage

import { createCache } from 'stash-it';
import createReadOnlyPlugin from 'stash-it-plugin-readonly';

// You can use any adapter
import createMemoryAdapter from 'stash-it-adapter-memory';

const cacheInstance = createCache(createMemoryAdapter());

const readOnlyPlugin = createReadOnlyPlugin();
const readOnlyCacheInstance = cacheInstance.registerPlugins([ readOnlyPlugin ]);

// This will work
readOnlyCacheInstance.hasItem('key');

// But this will throw
readOnlyCacheInstance.removeItem('key');

// or this
readOnlyCacheInstance.setItem('key', 'value');

Methods that will throw are: setItem, addExtra, setExtra and removeItem. All other will work just fine.

Demo

Edit x5p5op0j4

About

Read only plugin for stash-it

Resources

License

Stars

Watchers

Forks

Packages

No packages published