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
|
"use server"; // Next.js 서버 액션에서 직접 import하려면 (선택)
import { revalidateTag, unstable_noStore } from "next/cache";
import db from "@/db/db";
import { unstable_cache } from "@/lib/unstable-cache";
import { GetFormListsSchema } from "./validation";
import { filterColumns } from "@/lib/filter-columns";
import { formListsView } from "@/db/schema/vendorData";
import { asc, desc, ilike, inArray, and, gte, lte, not, or } from "drizzle-orm";
import { countFormLists, selectFormLists } from "./repository";
import { projects } from "@/db/schema";
export async function getFormLists(input: GetFormListsSchema) {
// return unstable_cache(
// async () => {
try {
const offset = (input.page - 1) * input.perPage;
const advancedTable = true;
// advancedTable 모드면 filterColumns()로 where 절 구성
const advancedWhere = filterColumns({
table: formListsView, // 뷰 테이블 사용
filters: input.filters,
joinOperator: input.joinOperator,
});
let globalWhere;
if (input.search) {
const s = `%${input.search}%`;
globalWhere = or(
ilike(formListsView.formCode, s),
ilike(formListsView.formName, s),
ilike(formListsView.tagTypeLabel, s),
ilike(formListsView.classLabel, s),
ilike(formListsView.remark, s),
ilike(formListsView.projectName, s), // 뷰 테이블의 projectName 사용
ilike(formListsView.projectCode, s), // 뷰 테이블의 projectCode 사용
);
}
const finalWhere = and(
advancedWhere,
globalWhere
);
const where = finalWhere;
const orderBy =
input.sort.length > 0
? input.sort.map((item) => {
// 뷰 테이블은 모든 필드가 포함되어 있으므로 간단하게 처리
return item.desc
? desc(formListsView[item.id])
: asc(formListsView[item.id]);
})
: [asc(formListsView.createdAt)];
// 트랜잭션 내부에서 Repository 호출
const { data, total } = await db.transaction(async (tx) => {
// 뷰 테이블을 사용하는 새로운 select 함수
const data = await selectFormLists(tx, {
where,
orderBy,
offset,
limit: input.perPage,
});
// 뷰 테이블을 사용하는 새로운 count 함수
const total = await countFormLists(tx, where);
return { data, total };
});
const pageCount = Math.ceil(total / input.perPage);
return { data, pageCount };
} catch (err) {
console.log(err)
// 에러 발생 시 디폴트
return { data: [], pageCount: 0 };
}
// },
// [JSON.stringify(input)], // 캐싱 키
// {
// revalidate: 3600,
// tags: ["form-lists"],
// }
// )();
}
|