@wranggle/storage-core

Core package for WranggleStorage class

Usage no npm install needed!

<script type="module">
  import wranggleStorageCore from 'https://cdn.skypack.dev/@wranggle/storage-core';
</script>

README

WranggleStorage Misc

This @wranggle/storage-core package is responsible for the main WranggleStorage class, used to create an RPC endpoint.

Its main documentation is in the topmost README of this monorepo. This rpc-storage README holds odds and ends that don't belong there.

Subset Store

The createSubsetStore method lets you splinter off a new WranggleStorage instance from an existing one, inheriting all of the parent feature layers. You can then add new feature layers to the child that are not applied to the parent.

For example, you might set up a parent sessionStore to hold transient data, then apply different namespaces to child stores:

const sessionStore = myStore.createSubsetStore({ expire: oneDayInMs });
const thisProjectStore = sessionStore.createSubsetStore({ bucket: 'Project:01' }); 
const thatProjectStore = sessionStore.createSubsetStore({ bucket: 'Project:02' }); 

Both thisProjectStore and thatProjectStore share the expiring-keys behavior of its parent. The "bucket" option adds a KeyNameLayer, creating separate namespaces for the child stores.

Manipulate Feature Layers

Normally you'll specify the feature layers needed when you construct your WranggleStorage instance. You can, however, add or remove features after construction.

The store instance contains an array of FeatureLayer instances that you can manipulate. myStore.layers is publicly accessible and can be manipulated directly. Your WranggleStorage instance includes a couple helper methods:

  • useFeatureLayer constructs the feature layer instance if not already created, pushing the result to the end of the layers array.

    myStore.useFeatureLayer({ bucket: 'Projects' }); // inserts at ndx 1, the second feature layer in the array
    
  • insertFeatureLayerAt constructs the feature layer if necessary and inserts it at the specified position. Eg:

    myStore.insertFeatureLayerAt(new MyCustomLayer(), 1); // inserts at ndx 1, the second feature layer in the array
    

As with any middleware, order can make a difference. Be careful sorting or repositioning your feature layers.

When a data method is called, each layer has an opportunity to modify the request, from leftmost layer to the rightmost (oldest layer to newest) and then can modify the response / result in the opposite order, from right to left (newest to oldest layer.)

Custom Feature Layers

TODO:

  • how it works
  • example and links to src of existing ones
  • can extend NoopFeatureLayer
  • tip: attention to ctx.result (vs return value. easy to forget)
  • registering construction shortcut

Custom Persistence Adapter

TODO:

  • how it works
  • example and links
  • registering construction shortcut

Static Methods

  • WranggleStorage.knownPersistenceAdapterTypes(): string[] static method returns array of known persistence adapter types. These can be used as shortcuts during construction. Eg, if "memory" is listed, you can use new WranggleStorage({ memory: true })

  • WranggleStorage.knownFeatureLayerTypes(): string[] static method returns array of known feature layer types. These can be used as shortcuts during construction. Eg, if "bucket" is listed, you can use new WranggleStorage({ bucket: 'myBucket' })

  • WranggleStorage.registerPersistenceAdapter(adapterType: string, factory: (opts: any) => PersistenceAdapter). Register a factory function for building your custom persistence adapter. Once registered, the adapterType can be used as a shortcut when instantiating WranggleStorage.

  • WranggleStorage.registerFeatureLayer(layerType: string, factory: (opts: any) => FeatureLayer). Register a factory function for building your custom feature layer. Once registered, the layerType can be used as a shortcut when instantiating WranggleStorage or calling useFeatureLayer.