// src/lib/tasks/repository.ts import db from "@/db/db"; import { ocrRows, users } from "@/db/schema"; import { eq, inArray, not, asc, desc, and, ilike, gte, lte, count, gt, } from "drizzle-orm"; import { PgTransaction } from "drizzle-orm/pg-core"; /** * 단건/복수 조회 시 공통으로 사용 가능한 SELECT 함수 예시 * - 트랜잭션(tx)을 받아서 사용하도록 구현 */ export async function selectOcrRows( tx: PgTransaction, params: { where?: any; // drizzle-orm의 조건식 (and, eq...) 등 orderBy?: (ReturnType | ReturnType)[]; offset?: number; limit?: number; } ) { const { where, orderBy, offset = 0, limit = 10 } = params; return tx .select({ // ocrRows의 모든 필드 id: ocrRows.id, tableId: ocrRows.tableId, fileName: ocrRows.fileName, inspectionDate: ocrRows.inspectionDate, sessionId: ocrRows.sessionId, rowIndex: ocrRows.rowIndex, reportNo: ocrRows.reportNo, no: ocrRows.no, identificationNo: ocrRows.identificationNo, tagNo: ocrRows.tagNo, jointNo: ocrRows.jointNo, jointType: ocrRows.jointType, weldingDate: ocrRows.weldingDate, confidence: ocrRows.confidence, sourceTable: ocrRows.sourceTable, sourceRow: ocrRows.sourceRow, userId: ocrRows.userId, createdAt: ocrRows.createdAt, // users 테이블의 필드 userName: users.name, userEmail: users.email, }) .from(ocrRows) .leftJoin(users, eq(ocrRows.userId, users.id)) .where(where) .orderBy(...(orderBy ?? [])) .offset(offset) .limit(limit); } /** 총 개수 count */ export async function countOcrRows( tx: PgTransaction, where?: any ) { const res = await tx.select({ count: count() }).from(ocrRows).where(where); return res[0]?.count ?? 0; }