Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cermakjiri/idb-stores
Strongly typed IndexedDB stores with Zod. Store can present arbitrary an object schema.
https://github.com/cermakjiri/idb-stores
Last synced: about 1 month ago
JSON representation
Strongly typed IndexedDB stores with Zod. Store can present arbitrary an object schema.
- Host: GitHub
- URL: https://github.com/cermakjiri/idb-stores
- Owner: cermakjiri
- Created: 2024-02-15T16:20:45.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-05T10:03:54.000Z (4 months ago)
- Last Synced: 2024-08-12T05:03:56.684Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 1.02 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# idb-stores
Strongly typed IndexedDB stores with [Zod](https://zod.dev). Store can present arbitrary an 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
```sh
yarn add idb-stores
``````ts
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 }]
})();
```