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