File
Blob: src/worker/db/d1/repositories/users.ts
| 1 | import { eq } from "drizzle-orm"; |
| 2 | |
| 3 | import { type D1DbExecutor, users } from "@/worker/db/d1"; |
| 4 | |
| 5 | export type UserRow = typeof users.$inferSelect; |
| 6 | export type NewUserRow = typeof users.$inferInsert; |
| 7 | |
| 8 | export const findUserById = async (db: D1DbExecutor, userId: string): Promise<UserRow | undefined> => { |
| 9 | const rows = await db.select().from(users).where(eq(users.id, userId)).limit(1); |
| 10 | return rows[0]; |
| 11 | }; |
| 12 | |
| 13 | export const findUserByEmail = async (db: D1DbExecutor, email: string): Promise<UserRow | undefined> => { |
| 14 | const rows = await db.select().from(users).where(eq(users.email, email)).limit(1); |
| 15 | return rows[0]; |
| 16 | }; |
| 17 | |
| 18 | export const findUserBySlug = async (db: D1DbExecutor, slug: string): Promise<UserRow | undefined> => { |
| 19 | const rows = await db.select().from(users).where(eq(users.slug, slug)).limit(1); |
| 20 | return rows[0]; |
| 21 | }; |
| 22 | |
| 23 | export const insertUser = async (db: D1DbExecutor, row: NewUserRow): Promise<void> => { |
| 24 | await db.insert(users).values(row); |
| 25 | }; |
| 26 | |
| 27 | export const deleteUserById = async (db: D1DbExecutor, userId: string): Promise<void> => { |
| 28 | await db.delete(users).where(eq(users.id, userId)); |
| 29 | }; |