Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Strongly typed IndexedDB stores with Zod. Store can present an arbitrary object schema.

Notifications You must be signed in to change notification settings

cermakjiri/idb-stores

Repository files navigation

idb-stores

Strongly typed IndexedDB stores with Zod. Store can present an arbitrary object schema.

Features

  • Type-safe IndexedDB.
  • Runtime validations against Zod schemas.
  • Create multiple IndexedDB databases, each with multiple stores.
  • Mocked store for non-browser env (SSR).

Getting started

yarn add idb-stores
import { initIDB } from 'idb-stores';
import { z } from 'zod';

(async () => {
    // Initialize IndexedDB database
    const getStore = initIDB({
        database: {
            name: 'my-database',
            version: 1,
        },

        storeSchemas: {
            auth: z.object({
                username: z.string().optional(),

                meta: z
                    .array(
                        z.shape({
                            foo: z.boolean(),
                        }),
                    )
                    .optional(),
            }),
        },
    });

    const store = getStore('auth'); // ✅
    // const store = getStore('non-existing-store-name') // ❌

    await store.set('username', 'alois'); // ✅
    // await store.set('username', 1234) // ❌

    const username = await store.get('username'); // `username` is type of `string | undefined`

    // ----

    await store.set('meta', [{ foo: true }, { foo: false }]);
    const meta = await store.get('meta'); // [{ foo: true }, { foo: false }]
})();

About

Strongly typed IndexedDB stores with Zod. Store can present an arbitrary object schema.

Topics

Resources

Stars

Watchers

Forks