|
| 1 | +/** |
| 2 | + * User Queries |
| 3 | + * |
| 4 | + * Reusable Effect programs for user database operations. |
| 5 | + * |
| 6 | + * @module |
| 7 | + */ |
| 8 | +import { DateTime, Effect } from "effect" |
| 9 | +import { PgDrizzle } from "@effect/sql-drizzle/Pg" |
| 10 | +import { eq } from "drizzle-orm" |
| 11 | + |
| 12 | +import { users } from "../schema" |
| 13 | +import type { UserId, Email, User, CreateUser } from "@backpine/domain" |
| 14 | +import { UserNotFoundError, UserCreationError } from "@backpine/domain" |
| 15 | + |
| 16 | +// ============================================================================ |
| 17 | +// Internal Helpers |
| 18 | +// ============================================================================ |
| 19 | + |
| 20 | +/** Convert database ID to branded UserId */ |
| 21 | +const toUserId = (id: number): UserId => `usr_${id}` as UserId |
| 22 | + |
| 23 | +/** Parse UserId to database ID, returns null if invalid format */ |
| 24 | +const parseUserId = (id: UserId): number | null => { |
| 25 | + const match = id.match(/^usr_(\d+)$/) |
| 26 | + return match ? parseInt(match[1]!, 10) : null |
| 27 | +} |
| 28 | + |
| 29 | +/** Map database row to domain User */ |
| 30 | +const toDomainUser = (row: typeof users.$inferSelect): User => ({ |
| 31 | + id: toUserId(row.id), |
| 32 | + email: row.email as Email, |
| 33 | + name: row.name, |
| 34 | + createdAt: DateTime.unsafeFromDate(row.createdAt) |
| 35 | +}) |
| 36 | + |
| 37 | +// ============================================================================ |
| 38 | +// Query Programs |
| 39 | +// ============================================================================ |
| 40 | + |
| 41 | +/** |
| 42 | + * Find all users. |
| 43 | + * |
| 44 | + * @returns Effect that yields array of domain Users |
| 45 | + */ |
| 46 | +export const findAllUsers: Effect.Effect< |
| 47 | + User[], |
| 48 | + never, |
| 49 | + PgDrizzle |
| 50 | +> = Effect.gen(function* () { |
| 51 | + const drizzle = yield* PgDrizzle |
| 52 | + const rows = yield* drizzle |
| 53 | + .select() |
| 54 | + .from(users) |
| 55 | + .pipe(Effect.orElseSucceed(() => [])) |
| 56 | + |
| 57 | + return rows.map(toDomainUser) |
| 58 | +}) |
| 59 | + |
| 60 | +/** |
| 61 | + * Find user by ID. |
| 62 | + * |
| 63 | + * @param id - Branded UserId |
| 64 | + * @returns Effect that yields User or fails with UserNotFoundError |
| 65 | + */ |
| 66 | +export const findUserById = ( |
| 67 | + id: UserId |
| 68 | +): Effect.Effect<User, UserNotFoundError, PgDrizzle> => |
| 69 | + Effect.gen(function* () { |
| 70 | + const dbId = parseUserId(id) |
| 71 | + |
| 72 | + if (dbId === null) { |
| 73 | + return yield* Effect.fail( |
| 74 | + new UserNotFoundError({ id, message: `Invalid user ID format: ${id}` }) |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + const drizzle = yield* PgDrizzle |
| 79 | + const rows = yield* drizzle |
| 80 | + .select() |
| 81 | + .from(users) |
| 82 | + .where(eq(users.id, dbId)) |
| 83 | + .pipe(Effect.orElseSucceed(() => [])) |
| 84 | + |
| 85 | + const row = rows[0] |
| 86 | + |
| 87 | + if (!row) { |
| 88 | + return yield* Effect.fail( |
| 89 | + new UserNotFoundError({ id, message: `User not found: ${id}` }) |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + return toDomainUser(row) |
| 94 | + }) |
| 95 | + |
| 96 | +/** |
| 97 | + * Create a new user. |
| 98 | + * |
| 99 | + * @param data - CreateUser payload (email, name) |
| 100 | + * @returns Effect that yields created User or fails with UserCreationError |
| 101 | + */ |
| 102 | +export const createUser = ( |
| 103 | + data: CreateUser |
| 104 | +): Effect.Effect<User, UserCreationError, PgDrizzle> => |
| 105 | + Effect.gen(function* () { |
| 106 | + const drizzle = yield* PgDrizzle |
| 107 | + const rows = yield* drizzle |
| 108 | + .insert(users) |
| 109 | + .values({ email: data.email, name: data.name }) |
| 110 | + .returning() |
| 111 | + .pipe(Effect.mapError(() => new UserCreationError(data))) |
| 112 | + |
| 113 | + const row = rows[0] |
| 114 | + |
| 115 | + if (!row) { |
| 116 | + return yield* Effect.fail(new UserCreationError(data)) |
| 117 | + } |
| 118 | + |
| 119 | + return toDomainUser(row) |
| 120 | + }) |
0 commit comments