@wranggle/storage-key-name-layer

WranggleStorage feature layer adding key prefixes to create separate namespaces to any data store

Usage no npm install needed!

<script type="module">
  import wranggleStorageKeyNameLayer from 'https://cdn.skypack.dev/@wranggle/storage-key-name-layer';
</script>

README

KeyNameLayer

KeyNameLayer is a WranggleStorage feature layer that adds a prefix to each key, effectively imposing an isolated namespace on the store.

For example, we construct two stores in different namespaces:

const eastStore = new WranggleStorage({ bucket: 'Region:east' })
const westStore = new WranggleStorage({ bucket: 'Region:west' })

Each store can get/set items in isolation, even with the same key:

await eastStore.set({ lunch: 'philly' })
await westStore.set({ lunch: 'burrito' })

The underlying store will now have two items set without conflict, with keys: Region:east/lunch and Region:west/lunch.

Applying the feature

When constructing your WranggleStorage instance, use the bucket keyword to add the feature:

const store = new WranggleStorage({ bucket: 'SomeNamespace' }); 

You can also supply an options object, should you want to supply it with secondary/advanced options.

Options

Primary option:

  • bucket string prefix to prepend to keys. The bucketDelimeter option will be added.

Secondary/advanced options:

  • bucketDelimeter string added to bucket value when provided. Default is "/".

  • prefix string prepended to keys. Similar to bucket but bucketDelimeter is not added.

  • suffix string appended to keys. Use is discouraged! (Because the ExpirationLayer uses a suffix and if this layer does so too, layer order can no longer be ignored.)

Installing From Individual Packages

The KeyNameLayer is included in the @wranggle/storage-web and @wranggle/storage-node bundles.

Or you can add/install the individual package, alongside @wranggle/storage-core and a persistence adapter:

  1. Add the @wranggle/storage-key-name-layer package to your project:

    # From the command line using yarn:
    yarn add @wranggle/storage-key-name-layer
    
    # or with npm:
    npm install @wranggle/storage-key-name-layer
    
  2. In your JavaScript, import or require the packages:

    import KeyNameLayer from '@wranggle/storage-key-name-layer'
    // or:
    const KeyNameLayer = require('@wranggle/storage-key-name-layer')
    
  3. Instantiate your store:

    const store = new WranggleStorage({ expire: myDuration });
    

    Or do so without the construction shortcut:

    const store = new WranggleStorage({ persist: new KeyNameLayer({ duration: myDuration }));