import { Identifiable, Task, Todo, User } from '@homeman/models.ts' import { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts' import { Created } from '@homeman/models.ts' export const kv = await Deno.openKv('homeman.db') export const schema = { // key is table name, value is zod schema user: User, todo: Todo, task: Task, } export type Schema = typeof schema export type Table = keyof Schema export type Model = z.infer export async function get( a: T, id: z.infer['id'], ): Promise>> { return await kv.get>([a, id]) } export function all( a: T, ): Deno.KvListIterator> { return kv.list>({ prefix: [a] }) } type Create = Omit | Partial export function create( a: T, payload: Create, ): Deno.KvListIterator> { const data: Create> | undefined = schema[a].parse(payload) if (Identifiable.safeParse(data).success) { delete data.id } if (Created.safeParse(data).success) { delete data.createdBy } return kv.set([a, ) }