import db from "@/db/db"; import { NewTag, tags } from "@/db/schema/vendorData"; import { eq, inArray, not, asc, desc, and, ilike, gte, lte, count, gt, } from "drizzle-orm"; import { PgTransaction } from "drizzle-orm/pg-core"; export async function selectTags( 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() .from(tags) .where(where) .orderBy(...(orderBy ?? [])) .offset(offset) .limit(limit); } /** 총 개수 count */ export async function countTags( tx: PgTransaction, where?: any ) { const res = await tx.select({ count: count() }).from(tags).where(where); return res[0]?.count ?? 0; } export async function insertTag( tx: PgTransaction, data: NewTag // DB와 동일한 insert 가능한 타입 ) { // returning() 사용 시 배열로 돌아오므로 [0]만 리턴 return tx .insert(tags) .values(data) .returning({ id: tags.id, createdAt: tags.createdAt }); } /** 단건 삭제 */ export async function deleteTagById( tx: PgTransaction, tagId: number ) { return tx.delete(tags).where(eq(tags.id, tagId)); } /** 복수 삭제 */ export async function deleteTagsByIds( tx: PgTransaction, ids: number[] ) { return tx.delete(tags).where(inArray(tags.id, ids)); }