getsetdel
v1.0.2
Published
**Key-value store implemented on top of IndexedDB with a small metadata layer to support store management.**
Downloads
407
Maintainers
Readme
getsetdel
Key-value store implemented on top of IndexedDB with a small metadata layer to support store management.
Features:
- Multiple named key-value stores
- Built on top of IndexedDB (using idb-keyval)
- Keep track of created stores including a schema version and tags
- Auto-clear data on version or tag changes
- Support for custom store metadata
- Query inventory of stores by name or tags
Design
GetSetDel is a key-value store with an small inventory layer on top of it.
It choses clearing the store and hydrating it from scratch over dealing with complex data migrations. This is enabled by the inventory layer which keeps track of details that would invalidate the data (data/schema version or tags) as well as custom metadata that you may need (e.g. last sync timestamp) to keep the data up to date.
Storage Inventory Information
When creating a store (via createStore
), you can provide an optional version
(which can be used to keep track of schema changes) and an optional array tags
(e.g. 'public', 'private' data). At the same time we keep track of creation
which contains the time when the current instance of the store was created.
Data Invalidation
Instead of dealing with data migrations, we just get rid of all the data and start over. This happens in two ways:
- During store creation (i.e.
createStore
): At this time, if the data is invalidated, it is simply cleared (all data removed and inventory entry removed including all metadata). - During calls to all data access/modification methods (e.g.
get
,set
,del
,entries
, etc.): At this time, if the data is invalidated, it will throw aGetSetDelResetError
exception which you can handle by clearing your state and starting over fromcreateStore
.
Alternatives
If you would like to manage all your stores yourself or if you only need a single store, I suggest you use idb-keyval instead of this. If you need more complex IndexedDB functionality, idb-keyval suggests IDB.
Usage
Creating (initializing) a store
import { createStore } from 'getsetdel'
// Option 1. Minimum required options
// This will result in an IndexedDB databased named
// 'getsetdel-store-name' with a store called 'store'
const storeToken = await createStore({
name: 'store-name',
})
// Option 2. Store with all the options (name is the only
// required prop)
// This will result in an IndexedDB databased named
// 'getsetdel-all-options-store--0001' with a store called 'store'
const allOptionsStoreToken = await createStore({
name: 'all-options-store',
// In case you want to store data about a specific entity (this
// is behind the scenes just added to the IndexedDB name)
key: '0001',
// Data is cleared whenever createStore is called with different
// values on the following:
tags: ['private', 'other'],
version: 1,
})
Accessing/deleting data
import { createStore, del, set } from 'getsetdel'
const storeToken = await createStore({
name: 'store-name',
})
// To add data you can use set or setMany
await set(storeToken, 'key1', { some: 'data', here: true })
// To get data you can use get, getMany (retrieve many keys
// at once), or entries (returns key-value pairs for all
// the entries in the store)
const key1Value = await get(storeToken, 'key1')
console.log(key1Value)
// To remove the data you can use del, delMany, or clear
// to fully remove the data and the store entry in the
// inventory.
await del(storeToken, 'key1')
Metadata
// Get the metadata
const metadata = await getMetadata(storeToken)
// Set the metadata (This is a full overwrite and not
// a merge)
await setMetadata(storeToken, {
...metadata,
lastSync: Date.now(),
})
Reset handling
GetSetDel provides a helper method to manage GetSetDelResetError
.
const onStoreError = async () => {
// Do what you need to reset the state of your code and
// re-initialize the store by calling `createStore`
}
// When the `version` prop, `creation` timestamp, or `tags` change,
// `onStoreError` is called. In the case of any other exception is thrown, the
// exception is just re-thrown.
await set(storeToken, 'key', { data: 'hello' }).handleResetError(onStoreError)
Query Inventory
You can query the inventory by name
or tags
.
Example 1. Clear all the data tagged as private
when a user logs out.
const privateStoreTokens = await queryInventory({
includesAnyTag: ['private'],
})
await Promise.all(privateStoreTokens.map((token) => clear(token)))
Example 2. For clearing multiple stores called todo-items
with key
that matches project ids (e.g. { name: 'todo-items', key: 'projectid0001'}
and { name: 'todo-items', key: 'projectid0002'}
).
const privateStoreTokens = await queryInventory({
name: 'todo-items',
})
await Promise.all(privateStoreTokens.map((token) => clear(token)))
API Reference
See docs