npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

getsetdel

v1.0.2

Published

**Key-value store implemented on top of IndexedDB with a small metadata layer to support store management.**

Downloads

22

Readme

getsetdel

Key-value store implemented on top of IndexedDB with a small metadata layer to support store management.

github license npm version

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:

  1. 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).
  2. 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 a GetSetDelResetError exception which you can handle by clearing your state and starting over from createStore.

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