Coder Social home page Coder Social logo

jotai-minidb's Introduction

jotai-minidb - Jotai atoms for IndexedDB key-value storage persistency

Simple but fully functional way to persist key-value data in IndexedDb for Jotai. Analogues to atomWithStorage but when localStorage is not enough.

Demo App | Repo

Features

  • IndexedDB persistency
  • TypeScript support
  • Cross-tab sync (changes in one browser tab are automatically synced to other tabs)
  • Data migrations (if you have some local data you will have to migrate it sooner or later)

Installation

yarn add jotai-minidb jotai

Usage

First, you need to create instance of a MiniDb class.

import { MiniDb } from "jotai-minidb";
const myDb = new MiniDb();

After MiniDb instance is created it provides set of atoms to Interact with IndexedDB storage:

function MyComponent() {
  const [user, setUser] = useAtom(myDb.item("user-123"));

  return (
    <input
      value={user.name}
      onChange={(e) => setUser({ ...user, name: e.target.value })}
    />
  );
}

API

Atoms for reading all stored items

  • myDb.keys - read-only atom with an array of stored keys Atom<string[]>
  • myDb.values - read-only atom with an array of stored values Atom<T[]>
  • myDb.items - - read-only atom with an key/value cache Atom<Record<string, T>>
  • myDb.entries - read-only atom with [key, value] entries Atom<[string, T][]>

Atom to read/write item

const [item, setItem] = useAtom(myDb.item(key));

Other write atoms

Set value of the item by key

const set = useSetAtom(myDb.set);
set(key, value);

Set many items with an array of entries

const setMany = useSetAtom(myDb.setMany)
setMany([
  ['key-1', 1],
  ['key-2', 33],
  ...
])

Delete by key

const delete = useSetAtom(myDb.delete)
delete(key)

Clear all

const clear = useSetAtom(myDb.clear);
clear();

Examples

// Jotai V2 API!
import { useAtom, useAtomValue, useSetAtom } from "jotai/react";
import { MiniDb } from "jotai-minidb";

// Type of an item in key-value store
type Item = {
  name: string;
};

// 1. Create a minidb instance
const myDb = new MiniDb<Item>();

// 2. Get all keys, values or entries
export function Entries() {
  const keys = useAtomValue(myDb.keys);
  const values = useAtomValue(myDb.values);
  const entries = useAtomValue(myDb.entries);

  return (
    <div>
      Keys:
      <ul>
        {keys.map((key) => (
          <li>{key}</li>
        ))}
      </ul>

      Values:
      <ul>
        {values.map((value) => (
          <li>{value.name}</li>
        ))}
      </ul>

      Entries:
      <ul>
        {entries.map(([key, value]) => (
          <li>{value.name}</li>
        ))}
      </ul>
    </div>
  );
}

// 3. Get, or set item in a store
export function Entry() {
  const [item, setItem] = useAtom(myDb.item("some-item-key"));

  if (!item) {
    return null;
  }

  return (
    <input
      value={item.name}
      onChange={(e) => setItem({ ...item, name: e.target.value })}
    />
  );
}

// 4. Create new item or delete item
export function CreateUpdateOrDelete() {
  const set = useSetAtom(simpleStore.set);
  const del = useSetAtom(simpleStore.delete);

  return (
    <>
      <button onClick={() => set("some-key", { name: "new value" })}>
        Create
      </button>
      <button onClick={() => del("some-key")}>Delete</button>
    </>
  );
}

Configuration

MiniDb constructor takes an optional configuration object with the following parameters:

name

default: `jotai-minidb`

Database name. If you need multiple collections you can simply define multiple storages with different names:

const books = new MiniDb({ name: 'books' })
const authors = new MiniDb({ name: 'authors' })

version

default: 0

Schema version is used to introduce breaking change to a shape of the data stored in a database. If data in IndexedDb has a version lower than version then it is migrated with set of migrations. If version is lower than version of the data in IndexedDb then exception is thrown and onVersionMissmatch handler is called

initialData

type: Record<string, Item>
default: {}

Populate database with some initial data when it is created

migrations

default: {}

If version is greater than 0 you should provide a migration function for each version in migrations object where a key is version and value is a migration function

const myDb = new MiniDb<Item>({
    version: 2,
    migrations: {
        1: (item) => {
            item.age = 18
            return item
        },
        2: (item) => {
            // migrate from 1 => 2
        }
    },
});

onMigrationCompleted

default: () => {
    alert("Data has been migrated. Page will be reloaded");
    window.location.reload();
}

Callback that is called when migration is completed in other browser tab or window. For instance when user opens a new tab with the new version of the app. In simple cases the easiest way is to refresh the page because the old code likely can not work with migrated data anyway

onVersionMissmatch

deafult: () => {}

Callback that is called when version of the data in IndexedDb is higher than the version. Should not happen in normal situations

jotai-minidb's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

scarf005

jotai-minidb's Issues

InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.

Hey there! Such an awesome package. I'm going to be honest, I'm using it in production ๐Ÿ˜
Getting this error quite a lot, especially if a tab was left open for a while and a user returns to it. Mobile devices seem to be more susceptible to this issue.

Was wondering if you've ever had this issue and whether there is a slight hope it's going to be maintained, though, I might be the one doing something wrong as well.

Looking to use jotai-minidb with nextjs

Hey!
I'm trying to use jotai-minidb with nextjs and keep running into "IndexedDb is not defined" error.

I used typeof window !== "undefined" to conditionally load MiniDb and its .item but I'm not sure what it's default value should be since if its undefined then I can't use it in useAtom.

If I store an atomWithStorage then the setter that I get from useAtom ends up having a "never" type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.