diff options
Diffstat (limited to 'lib/information/repository.ts')
| -rw-r--r-- | lib/information/repository.ts | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/lib/information/repository.ts b/lib/information/repository.ts new file mode 100644 index 00000000..2a3bc1c0 --- /dev/null +++ b/lib/information/repository.ts @@ -0,0 +1,190 @@ +import { asc, desc, eq, ilike, and, count, sql } from "drizzle-orm"
+import db from "@/db/db"
+import { pageInformation, type PageInformation, type NewPageInformation } from "@/db/schema/information"
+import type { GetInformationSchema } from "./validations"
+import { PgTransaction } from "drizzle-orm/pg-core"
+
+// 최신 패턴: 트랜잭션을 지원하는 인포메이션 조회
+export async function selectInformationLists(
+ tx: PgTransaction<any, any, any>,
+ params: {
+ where?: ReturnType<typeof and>
+ orderBy?: (ReturnType<typeof asc> | ReturnType<typeof desc>)[]
+ offset?: number
+ limit?: number
+ }
+) {
+ const { where, orderBy, offset = 0, limit = 10 } = params
+
+ return tx
+ .select()
+ .from(pageInformation)
+ .where(where)
+ .orderBy(...(orderBy ?? [desc(pageInformation.createdAt)]))
+ .offset(offset)
+ .limit(limit)
+}
+
+// 최신 패턴: 트랜잭션을 지원하는 카운트 조회
+export async function countInformationLists(
+ tx: PgTransaction<any, any, any>,
+ where?: ReturnType<typeof and>
+) {
+ const res = await tx
+ .select({ count: count() })
+ .from(pageInformation)
+ .where(where)
+
+ return res[0]?.count ?? 0
+}
+
+// 기존 패턴 (하위 호환성을 위해 유지)
+export async function selectInformation(input: GetInformationSchema) {
+ const { page, per_page = 50, sort, pageCode, pageName, isActive, from, to } = input
+
+ const conditions = []
+
+ if (pageCode) {
+ conditions.push(ilike(pageInformation.pageCode, `%${pageCode}%`))
+ }
+
+ if (pageName) {
+ conditions.push(ilike(pageInformation.pageName, `%${pageName}%`))
+ }
+
+ if (isActive !== null) {
+ conditions.push(eq(pageInformation.isActive, isActive))
+ }
+
+ if (from) {
+ conditions.push(sql`${pageInformation.createdAt} >= ${from}`)
+ }
+
+ if (to) {
+ conditions.push(sql`${pageInformation.createdAt} <= ${to}`)
+ }
+
+ const offset = (page - 1) * per_page
+
+ // 정렬 설정
+ let orderBy = desc(pageInformation.createdAt);
+
+ if (sort && Array.isArray(sort) && sort.length > 0) {
+ const sortItem = sort[0];
+ if (sortItem.id === "createdAt") {
+ orderBy = sortItem.desc ? desc(pageInformation.createdAt) : asc(pageInformation.createdAt);
+ }
+ }
+
+ const whereClause = conditions.length > 0 ? and(...conditions) : undefined
+
+ const data = await db
+ .select()
+ .from(pageInformation)
+ .where(whereClause)
+ .orderBy(orderBy)
+ .limit(per_page)
+ .offset(offset)
+
+ return data
+}
+
+// 기존 패턴: 인포메이션 총 개수 조회
+export async function countInformation(input: GetInformationSchema) {
+ const { pageCode, pageName, isActive, from, to } = input
+
+ const conditions = []
+
+ if (pageCode) {
+ conditions.push(ilike(pageInformation.pageCode, `%${pageCode}%`))
+ }
+
+ if (pageName) {
+ conditions.push(ilike(pageInformation.pageName, `%${pageName}%`))
+ }
+
+ if (isActive !== null) {
+ conditions.push(eq(pageInformation.isActive, isActive))
+ }
+
+ if (from) {
+ conditions.push(sql`${pageInformation.createdAt} >= ${from}`)
+ }
+
+ if (to) {
+ conditions.push(sql`${pageInformation.createdAt} <= ${to}`)
+ }
+
+ const whereClause = conditions.length > 0 ? and(...conditions) : undefined
+
+ const result = await db
+ .select({ count: count() })
+ .from(pageInformation)
+ .where(whereClause)
+
+ return result[0]?.count ?? 0
+}
+
+// 페이지 코드별 인포메이션 조회 (활성화된 것만)
+export async function getInformationByPageCode(pageCode: string): Promise<PageInformation | null> {
+ const result = await db
+ .select()
+ .from(pageInformation)
+ .where(and(
+ eq(pageInformation.pageCode, pageCode),
+ eq(pageInformation.isActive, true)
+ ))
+ .limit(1)
+
+ return result[0] || null
+}
+
+// 인포메이션 생성
+export async function insertInformation(data: NewPageInformation): Promise<PageInformation> {
+ const result = await db
+ .insert(pageInformation)
+ .values(data)
+ .returning()
+
+ return result[0]
+}
+
+// 인포메이션 수정
+export async function updateInformation(id: number, data: Partial<NewPageInformation>): Promise<PageInformation | null> {
+ const result = await db
+ .update(pageInformation)
+ .set({ ...data, updatedAt: new Date() })
+ .where(eq(pageInformation.id, id))
+ .returning()
+
+ return result[0] || null
+}
+
+// 인포메이션 삭제
+export async function deleteInformationById(id: number): Promise<boolean> {
+ const result = await db
+ .delete(pageInformation)
+ .where(eq(pageInformation.id, id))
+
+ return (result.rowCount ?? 0) > 0
+}
+
+// 인포메이션 다중 삭제
+export async function deleteInformationByIds(ids: number[]): Promise<number> {
+ const result = await db
+ .delete(pageInformation)
+ .where(sql`${pageInformation.id} = ANY(${ids})`)
+
+ return result.rowCount ?? 0
+}
+
+// ID로 인포메이션 조회
+export async function getInformationById(id: number): Promise<PageInformation | null> {
+ const result = await db
+ .select()
+ .from(pageInformation)
+ .where(eq(pageInformation.id, id))
+ .limit(1)
+
+ return result[0] || null
+}
\ No newline at end of file |
