summaryrefslogtreecommitdiff
path: root/lib/tags-plant/queries.ts
blob: c7ad43e06a89c0c93072f719635aac72e9aa666d (plain)
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
// lib/vendor-data-plant/queries.ts
"use server"

import db from "@/db/db"

import { tagsPlant } from "@/db/schema/vendorData"
import { eq, and } from "drizzle-orm"
import { revalidateTag, unstable_noStore } from "next/cache";

/**
 * 모든 태그 가져오기 (클라이언트 렌더링용)
 */
export async function getAllTagsPlant(
  projectCode: string,
  packageCode: string
) {
    unstable_noStore();
  try {
    const tags = await db
      .select()
      .from(tagsPlant)
      .where(
        and(
          eq(tagsPlant.projectCode, projectCode),
          eq(tagsPlant.packageCode, packageCode)
        )
      )
      .orderBy(tagsPlant.createdAt)

    return tags
  } catch (error) {
    console.error("Error fetching all tags:", error)
    return []
  }
}

/**
 * 고유 속성 키 추출
 */
export async function getUniqueAttributeKeys(
  projectCode: string,
  packageCode: string
): Promise<string[]> {
  try {
    const result = await db
      .select({
        attributes: tagsPlant.attributes
      })
      .from(tagsPlant)
      .where(
        and(
          eq(tagsPlant.projectCode, projectCode),
          eq(tagsPlant.packageCode, packageCode)
        )
      )

    const allKeys = new Set<string>()
    
    for (const row of result) {
      if (row.attributes && typeof row.attributes === 'object') {
        Object.keys(row.attributes).forEach(key => allKeys.add(key))
      }
    }

    return Array.from(allKeys).sort()
  } catch (error) {
    console.error("Error getting unique attribute keys:", error)
    return []
  }
}