import { createStore, type StoreApi } from "zustand/vanilla" import { hoist, type HoistedStoreApi } from "zustand-hoist" import { databaseSchema, type DatabaseSchema, type Thing } from "./schema.ts" import { combine } from "zustand/middleware" export const createDatabase = () => { return hoist(createStore(initializer)) } export type DbClient = ReturnType const initializer = combine(databaseSchema.parse({}), (set) => ({ addThing: (thing: Omit) => { set((state) => ({ things: [ ...state.things, { ...thing, thing_id: state.idCounter.toString() }, ], idCounter: state.idCounter + 1, })) }, }))