stash-it-plugin-prefixsuffix

1.1.0 • Public • Published

logo-stash-it-color-dark 2x

stash-it-plugin-prefixSuffix

build status Coverage Status

Prefix / suffix plugin for stash-it.

The problem

Imagine there are couple of teams working on a product that uses stash-it and the same storage. Each team would like to use any keys to store the items, and not to be worried that some names are already taken.

Solution

Using this plugin, you're able to create cache instances that will make sure that all of the keys you use will have (either or both) prefix / suffix of your choosing included automatically.

Installation

npm i stash-it-plugin-prefixsuffix --save

Usage

import { createCache } from 'stash-it';
import createPrefixSuffixPlugin from 'stash-it-plugin-prefixsuffix';

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

const team1Prefix = 'team1';
const team2Prefix = 'team2';
const cache = createCache(createMemoryAdapter());

const team1PrefixPlugin = createPrefixSuffixPlugin({ prefix: team1Prefix });
const team2PrefixPlugin = createPrefixSuffixPlugin({ prefix: team2Prefix });

const team1CacheInstance = cache.registerPlugins([ team1PrefixPlugin ]);
const team2CacheInstance = cache.registerPlugins([ team2PrefixPlugin ]);

// TEAM 1
team1CacheInstance.hasKey('key'); // false
team1CacheInstance.setItem('key', '11_TEAM_11');

const item = team1CacheInstance.getItem('key');

item.value; // 11_TEAM_11
item.key; // team1key

// TEAM 2
team2CacheInstance.hasKey('key'); // false
team2CacheInstance.setItem('key', '22_TEAM_22');

const item = team2CacheInstance.getItem('key');

item.value; // 22_TEAM_22
item.key; // team2key

Plugin extends cache instance with two methods: getPrefix() and getSuffix(). They return prefix / suffix passed upon plugin's creation.

import createPrefixSuffixPlugin from 'stash-it-plugin-prefixsuffix';

const prefix = 'somePrefix';
const suffix = 'someSuffix';

// assuming you already have cache instance created

const plugin = createPrefixSuffixPlugin({ prefix, suffix });
const cacheWithPrefixSuffix = cache.registerPlugins([ plugin ]);

cacheWithPrefixSuffix.getPrefix(); // somePrefix
cacheWithPrefixSuffix.getSuffix(); // someSuffix

API

Plugin takes an object, as an argument, upon creation. This object must contain at least one of the properties: prefix and / or suffix. Can have both.

const plugin1 = createPrefixSuffixPlugin({ prefix, suffix }); // OK
const plugin2 = createPrefixSuffixPlugin({ prefix }); // OK
const plugin3 = createPrefixSuffixPlugin({ suffix }); // OK
const plugin4 = createPrefixSuffixPlugin(); // will throw

prefix and suffix must be a string, containing any characters you want.

String(s) will be concatenated with key like so: ${prefix}${key}{$suffix}.

But as a user, you only need to pass the key when using stash-it's API.

Package Sidebar

Install

npm i stash-it-plugin-prefixsuffix

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

7.56 kB

Total Files

4

Last publish

Collaborators

  • jaceks