Define your database
Call defineIDB once at module scope. The schema interface is your single source of truth — store names, keys, value shapes, and index names are all inferred from it.
import { defineIDB } from "react-idb-hooks";
interface AppSchema {
todos: {
value: { id: string; title: string; done: boolean };
key: string;
indexes: { byDone: 0 | 1 };
};
}
export const db = defineIDB<AppSchema>({
name: "my-app",
version: 1,
upgrade({ db, oldVersion }) {
// runs once per version bump
if (oldVersion < 1) {
const todos = db.createObjectStore("todos", { keyPath: "id" });
todos.createIndex("byDone", "done");
}
},
});