account.ts 822 B

123456789101112131415161718192021222324252627282930313233
  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.transaction(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.transaction(async (tx) => {
  23. return tx
  24. .select()
  25. .from(AccountTable)
  26. .where(eq(AccountTable.id, id))
  27. .execute()
  28. .then((rows) => rows[0])
  29. }),
  30. )
  31. }