zewish / redux-remember

Saves and loads your redux state from a key-value store of your choice
MIT License
82 stars 5 forks source link

Simple IndexedDB driver example (using idb-keyval) #9

Open psychedelicious opened 1 year ago

psychedelicious commented 1 year ago

This is a trivial example, but I figured I'd share anyways.

Here's a simple storage driver using idb-keyval, a minimal library that wraps IndexedDB with a simple key-value interface:

import { createStore, get, set } from 'idb-keyval';
import { Driver } from 'redux-remember';

// Create a custom idb-keyval store (just needed to customize the name, else it's awkwardly named "keyval-store/keyval")
const idbKeyValStore = createStore('my-database', 'my-store');

// Create redux-remember driver, wrapping idb-keyval
const idbKeyValDriver: Driver = {
  getItem: (key) => get(key, idbKeyValStore),
  setItem: (key, value) => set(key, value, idbKeyValStore),
};

// Usage
rememberEnhancer(idbKeyValDriver, rememberedKeys, {
  serialize,
  unserialize,
})