@stash-it/prefix-suffix-plugin

0.0.6 • Public • Published

@stash-it/prefix-suffix-plugin

logo-stash-it-color-dark 2x

@stash-it/prefix-suffix-plugin is a plugin for @stash-it/stash-it that allows you to add a prefix and/or suffix to the key before persisting the value.

When to use it?

Let's say you're using a storage that doesn't support namespaces, but you want to separate the keys for different parts of your application. You can use this plugin to add a prefix and/or suffix to the key. Now your team can save an item for key user1 and so can the other team(s).

Installation

npm

npm install @stash-it/prefix-suffix-plugin

deno

deno add @stash-it/prefix-suffix-plugin

yarn

yarn dlx jsr add @stash-it/prefix-suffix-plugin

pnpm

pnpm dlx jsr add @stash-it/prefix-suffix-plugin

bun

bunx jsr add @stash-it/prefix-suffix-plugin

Usage

// Import stash-it main class.
import { StashIt } from '@stash-it/stash-it';

// Import the plugin.
import { createPrefixSuffixPlugin } from '@stash-it/prefix-suffix-plugin';

// For it to work you will also need an adapter.
// You can use any of the @stash-it adapters or create your own.
import { MemoryAdapter } from '@stash-it/memory-adapter';

// Create an instance of the adapter.
const adapter = new MemoryAdapter();

// Create an instance of stash-it.
const stash = new StashIt(adapter);

// Create an instance of the plugin.
const plugin = createPrefixSuffixPlugin({
  prefix: 'prefix-',
  suffix: '-suffix',
});

// Register the plugin to the stash-it instance.
stash.registerPlugins([plugin]);

// Use it.
await stash.set('key', 'value');
const item = await stash.get('key');

console.log(item.key); // 'key', same key! but the data is persisted with prefix and suffix

Package Sidebar

Install

npm i @stash-it/prefix-suffix-plugin

Weekly Downloads

2

Version

0.0.6

License

MIT

Unpacked Size

5.97 kB

Total Files

5

Last publish

Collaborators

  • jaceks