account.ts 772 B

1234567891011121314151617181920212223242526272829303132
  1. import { z } from "zod"
  2. import { eq } from "drizzle-orm"
  3. import { fn } from "./util/fn"
  4. import { Database } from "./drizzle"
  5. import { Identifier } from "./identifier"
  6. import { AccountTable } from "./schema/account.sql"
  7. export namespace Account {
  8. export const create = fn(
  9. z.object({
  10. id: z.string().optional(),
  11. }),
  12. async (input) =>
  13. Database.use(async (tx) => {
  14. const id = input.id ?? Identifier.create("account")
  15. await tx.insert(AccountTable).values({
  16. id,
  17. })
  18. return id
  19. }),
  20. )
  21. export const fromID = fn(z.string(), async (id) =>
  22. Database.use((tx) =>
  23. tx
  24. .select()
  25. .from(AccountTable)
  26. .where(eq(AccountTable.id, id))
  27. .then((rows) => rows[0]),
  28. ),
  29. )
  30. }