account.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. email: z.string().email(),
  11. id: z.string().optional(),
  12. }),
  13. async (input) =>
  14. Database.transaction(async (tx) => {
  15. const id = input.id ?? Identifier.create("account")
  16. await tx.insert(AccountTable).values({
  17. id,
  18. email: input.email,
  19. })
  20. return id
  21. }),
  22. )
  23. export const fromID = fn(z.string(), async (id) =>
  24. Database.transaction(async (tx) => {
  25. return tx
  26. .select()
  27. .from(AccountTable)
  28. .where(eq(AccountTable.id, id))
  29. .execute()
  30. .then((rows) => rows[0])
  31. }),
  32. )
  33. export const fromEmail = fn(z.string().email(), async (email) =>
  34. Database.transaction(async (tx) => {
  35. return tx
  36. .select()
  37. .from(AccountTable)
  38. .where(eq(AccountTable.email, email))
  39. .execute()
  40. .then((rows) => rows[0])
  41. }),
  42. )
  43. }