idb-builder
v0.1.1
Published
Type-safe IndexedDB migrations
Downloads
219
Maintainers
Readme
idb-builder
Type-safe IndexedDB migrations
Getting started
Install
npm install idb-builderSet up migrations
Define your database schema using TypeScript types and the schema() helper. The createMigrations() builder captures your database structure with full type inference.
import { createMigrations, schema } from 'idb-builder'
const migrations = createMigrations().version(1, v =>
v.createObjectStore({
name: 'users',
schema: schema<{
id: string
name: string
email: string
}>(),
primaryKey: 'id',
})
)Open the database
Pass your migrations to openDB() to open the database. The returned database handle is fully typed based on your migration definitions.
const db = await openDB('my-app', migrations)Read and write data
All operations are type-safe. The compiler knows which object stores exist, what shape the data has, and what type the primary key is.
// Insert a record
await db.put('users', {
id: '1',
name: 'Alice',
email: '[email protected]',
})
// Retrieve by primary key
const user = await db.get('users', '1')
// Get all records
const allUsers = await db.getAll('users')Future work
- Fully typesafe keyrange queries (will require a custom wrapper around
idbmethods)
