1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import { vendorTypes } from "@/db/schema";
import { SQL, eq, inArray, sql,asc, desc } from "drizzle-orm";
import { PgTransaction } from "drizzle-orm/pg-core";
/**
* 협력업체 타입 조회 (복잡한 where + order + limit + offset 지원)
*/
export async function selectVendorTypes(
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(vendorTypes)
.where(where)
.orderBy(...(orderBy ?? []))
.offset(offset)
.limit(limit);
}
/**
* 전체 협력업체 타입 조회
*/
export async function findAllVendorTypes(tx: any) {
return tx.select()
.from(vendorTypes)
.orderBy(vendorTypes.nameKo);
}
/**
* 협력업체 타입 개수 카운트
*/
export async function countVendorTypes(tx: any, where?: SQL<unknown> | undefined) {
const result = await tx
.select({ count: sql`count(*)` })
.from(vendorTypes)
.where(where || undefined);
return Number(result[0]?.count || 0);
}
/**
* 협력업체 타입 추가
*/
export async function insertVendorType(
tx: any,
data: {
code: string;
nameKo: string;
nameEn: string;
}
) {
const insertedRows = await tx
.insert(vendorTypes)
.values(data);
// 삽입된 데이터 가져오기
return tx.select()
.from(vendorTypes)
.where(eq(vendorTypes.code, data.code))
.limit(1);
}
/**
* 단일 협력업체 타입 업데이트
*/
export async function updateVendorType(
tx: any,
id: number,
data: Partial<{
code: string;
nameKo: string;
nameEn: string;
}>
) {
await tx
.update(vendorTypes)
.set({
...data,
updatedAt: new Date()
})
.where(eq(vendorTypes.id, id));
// 업데이트된 데이터 가져오기
return tx.select()
.from(vendorTypes)
.where(eq(vendorTypes.id, id))
.limit(1);
}
/**
* ID로 단일 협력업체 타입 삭제
*/
export async function deleteVendorTypeById(tx: any, id: number) {
// 삭제 전 데이터 가져오기 (필요한 경우)
const deletedRecord = await tx.select()
.from(vendorTypes)
.where(eq(vendorTypes.id, id))
.limit(1);
// 데이터 삭제
await tx
.delete(vendorTypes)
.where(eq(vendorTypes.id, id));
return deletedRecord;
}
/**
* 다수의 ID로 여러 협력업체 타입 삭제
*/
export async function deleteVendorTypesByIds(tx: any, ids: number[]) {
// 삭제 전 데이터 가져오기 (필요한 경우)
const deletedRecords = await tx.select()
.from(vendorTypes)
.where(inArray(vendorTypes.id, ids));
// 데이터 삭제
await tx
.delete(vendorTypes)
.where(inArray(vendorTypes.id, ids));
return deletedRecords;
}
|