diff options
Diffstat (limited to 'lib/items-tech/repository.ts')
| -rw-r--r-- | lib/items-tech/repository.ts | 248 |
1 files changed, 124 insertions, 124 deletions
diff --git a/lib/items-tech/repository.ts b/lib/items-tech/repository.ts index 1f4f7933..10ae2dab 100644 --- a/lib/items-tech/repository.ts +++ b/lib/items-tech/repository.ts @@ -1,124 +1,124 @@ -// src/lib/items/repository.ts -import db from "@/db/db"; -import { Item, ItemOffshoreTop, ItemOffshoreHull, itemOffshoreHull, itemOffshoreTop, items } from "@/db/schema/items"; -import { - eq, - inArray, - asc, - desc, - count, -} from "drizzle-orm"; -import { PgTransaction } from "drizzle-orm/pg-core"; -export type NewItem = typeof items.$inferInsert - -/** - * 단건/복수 조회 시 공통으로 사용 가능한 SELECT 함수 예시 - * - 트랜잭션(tx)을 받아서 사용하도록 구현 - */ -export async function selectItems( - tx: PgTransaction<any, any, any>, - params: { - where?: any; // drizzle-orm의 조건식 (and, eq...) 등 - orderBy?: (ReturnType<typeof asc> | ReturnType<typeof desc>)[]; - offset?: number; - limit?: number; - } -) { - const { where, orderBy, offset = 0, limit = 10 } = params; - - return tx - .select() - .from(items) - .where(where) - .orderBy(...(orderBy ?? [])) - .offset(offset) - .limit(limit); -} -/** 총 개수 count */ -export async function countItems( - tx: PgTransaction<any, any, any>, - where?: any -) { - const res = await tx.select({ count: count() }).from(items).where(where); - return res[0]?.count ?? 0; -} - -/** 단건 Insert 예시 */ -export async function insertItem( - tx: PgTransaction<any, any, any>, - data: NewItem // DB와 동일한 insert 가능한 타입 -) { - // returning() 사용 시 배열로 돌아오므로 [0]만 리턴 - return tx - .insert(items) - .values(data) - .returning({ id: items.id, createdAt: items.createdAt }); -} - -/** 복수 Insert 예시 */ -export async function insertItems( - tx: PgTransaction<any, any, any>, - data: Item[] -) { - return tx.insert(items).values(data).onConflictDoNothing(); -} - - - -/** 단건 삭제 */ -export async function deleteItemById( - tx: PgTransaction<any, any, any>, - itemId: number -) { - return tx.delete(items).where(eq(items.id, itemId)); -} - -/** 복수 삭제 */ -export async function deleteItemsByIds( - tx: PgTransaction<any, any, any>, - ids: number[] -) { - return tx.delete(items).where(inArray(items.id, ids)); -} - -/** 전체 삭제 */ -export async function deleteAllItems( - tx: PgTransaction<any, any, any>, -) { - return tx.delete(items); -} - -/** 단건 업데이트 */ -export async function updateItem( - tx: PgTransaction<any, any, any>, - itemId: number, - data: Partial<Item> -) { - return tx - .update(items) - .set(data) - .where(eq(items.id, itemId)) - .returning({ id: items.id, createdAt: items.createdAt }); -} - -/** 복수 업데이트 */ -export async function updateItems( - tx: PgTransaction<any, any, any>, - ids: number[], - data: Partial<Item> -) { - return tx - .update(items) - .set(data) - .where(inArray(items.id, ids)) - .returning({ id: items.id, createdAt: items.createdAt }); -} - -export async function findAllItems(): Promise<Item[]> { - return db.select().from(items).orderBy(asc(items.itemCode)); -} -export async function findAllOffshoreItems(): Promise<(ItemOffshoreHull | ItemOffshoreTop)[]> { - const hullItems = await db.select().from(itemOffshoreHull); - const topItems = await db.select().from(itemOffshoreTop); - return [...hullItems, ...topItems]; -} +// src/lib/items/repository.ts
+import db from "@/db/db";
+import { Item, ItemOffshoreTop, ItemOffshoreHull, itemOffshoreHull, itemOffshoreTop, items } from "@/db/schema/items";
+import {
+ eq,
+ inArray,
+ asc,
+ desc,
+ count,
+} from "drizzle-orm";
+import { PgTransaction } from "drizzle-orm/pg-core";
+export type NewItem = typeof items.$inferInsert
+
+/**
+ * 단건/복수 조회 시 공통으로 사용 가능한 SELECT 함수 예시
+ * - 트랜잭션(tx)을 받아서 사용하도록 구현
+ */
+export async function selectItems(
+ tx: PgTransaction<any, any, any>,
+ params: {
+ where?: any; // drizzle-orm의 조건식 (and, eq...) 등
+ orderBy?: (ReturnType<typeof asc> | ReturnType<typeof desc>)[];
+ offset?: number;
+ limit?: number;
+ }
+) {
+ const { where, orderBy, offset = 0, limit = 10 } = params;
+
+ return tx
+ .select()
+ .from(items)
+ .where(where)
+ .orderBy(...(orderBy ?? []))
+ .offset(offset)
+ .limit(limit);
+}
+/** 총 개수 count */
+export async function countItems(
+ tx: PgTransaction<any, any, any>,
+ where?: any
+) {
+ const res = await tx.select({ count: count() }).from(items).where(where);
+ return res[0]?.count ?? 0;
+}
+
+/** 단건 Insert 예시 */
+export async function insertItem(
+ tx: PgTransaction<any, any, any>,
+ data: NewItem // DB와 동일한 insert 가능한 타입
+) {
+ // returning() 사용 시 배열로 돌아오므로 [0]만 리턴
+ return tx
+ .insert(items)
+ .values(data)
+ .returning({ id: items.id, createdAt: items.createdAt });
+}
+
+/** 복수 Insert 예시 */
+export async function insertItems(
+ tx: PgTransaction<any, any, any>,
+ data: Item[]
+) {
+ return tx.insert(items).values(data).onConflictDoNothing();
+}
+
+
+
+/** 단건 삭제 */
+export async function deleteItemById(
+ tx: PgTransaction<any, any, any>,
+ itemId: number
+) {
+ return tx.delete(items).where(eq(items.id, itemId));
+}
+
+/** 복수 삭제 */
+export async function deleteItemsByIds(
+ tx: PgTransaction<any, any, any>,
+ ids: number[]
+) {
+ return tx.delete(items).where(inArray(items.id, ids));
+}
+
+/** 전체 삭제 */
+export async function deleteAllItems(
+ tx: PgTransaction<any, any, any>,
+) {
+ return tx.delete(items);
+}
+
+/** 단건 업데이트 */
+export async function updateItem(
+ tx: PgTransaction<any, any, any>,
+ itemId: number,
+ data: Partial<Item>
+) {
+ return tx
+ .update(items)
+ .set(data)
+ .where(eq(items.id, itemId))
+ .returning({ id: items.id, createdAt: items.createdAt });
+}
+
+/** 복수 업데이트 */
+export async function updateItems(
+ tx: PgTransaction<any, any, any>,
+ ids: number[],
+ data: Partial<Item>
+) {
+ return tx
+ .update(items)
+ .set(data)
+ .where(inArray(items.id, ids))
+ .returning({ id: items.id, createdAt: items.createdAt });
+}
+
+export async function findAllItems(): Promise<Item[]> {
+ return db.select().from(items).orderBy(asc(items.itemCode));
+}
+export async function findAllOffshoreItems(): Promise<(ItemOffshoreHull | ItemOffshoreTop)[]> {
+ const hullItems = await db.select().from(itemOffshoreHull);
+ const topItems = await db.select().from(itemOffshoreTop);
+ return [...hullItems, ...topItems];
+}
|
