summaryrefslogtreecommitdiff
path: root/db/schema/gtc.ts
blob: a4052d61b7bbe8af60b9e6a7f2aa1c23ccf4019a (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
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
import { 
    pgTable, 
    serial, 
    varchar, 
    text, 
    integer, 
    timestamp, 
    pgEnum,
    boolean,
    index,
    uniqueIndex
  } from "drizzle-orm/pg-core"
  import { relations } from "drizzle-orm"
import { projects } from "./projects"
import { users } from "./users"
  
  // GTC 구분 enum
  export const gtcTypeEnum = pgEnum("gtc_type", ["standard", "project"])
  
  // GTC 문서 테이블
  export const gtcDocuments = pgTable("gtc_documents", {
    id: serial("id").primaryKey(),
    
    // 구분 (표준/프로젝트)
    type: gtcTypeEnum("type").notNull(),
    
    // 프로젝트 참조 (프로젝트 타입인 경우만)
    projectId: integer("project_id").references(() => projects.id, { 
      onDelete: "cascade" 
    }),
    
    // 리비전 번호
    revision: integer("revision").notNull().default(0),
    
    // 파일 정보
    fileName: varchar("file_name", { length: 255 }),
    filePath: varchar("file_path", { length: 500 }),
    fileSize: integer("file_size"), // bytes
    
    // 최초 등록 정보
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    createdById: integer("created_by_id")
      .references(() => users.id, { onDelete: "set null" })
      .notNull(),
    
    // 최종 수정 정보  
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedById: integer("updated_by_id")
      .references(() => users.id, { onDelete: "set null" }),
    
    // 편집 사유
    editReason: text("edit_reason"),
    
    // 활성 상태
    isActive: boolean("is_active").default(true).notNull(),
    
  }, (table) => {
    return {
      // 프로젝트별 리비전 유니크 (표준의 경우 projectId가 null)
      projectRevisionIdx: uniqueIndex("gtc_project_revision_idx")
        .on(table.projectId, table.revision, table.type),
      
      // 조회 성능을 위한 인덱스들
      typeIdx: index("gtc_type_idx").on(table.type),
      projectIdx: index("gtc_project_idx").on(table.projectId),
      createdAtIdx: index("gtc_created_at_idx").on(table.createdAt),
      updatedAtIdx: index("gtc_updated_at_idx").on(table.updatedAt),
    }
  })
  
  // 관계 정의 (필요한 경우)
  export const gtcDocumentsRelations = relations(gtcDocuments, ({ one }) => ({
    project: one(projects, {
      fields: [gtcDocuments.projectId],
      references: [projects.id],
    }),
    createdBy: one(users, {
      fields: [gtcDocuments.createdById],
      references: [users.id],
    }),
    updatedBy: one(users, {
      fields: [gtcDocuments.updatedById],
      references: [users.id],
    }),
  }))
  
  // 타입 정의
  export type GtcDocument = typeof gtcDocuments.$inferSelect
  export type NewGtcDocument = typeof gtcDocuments.$inferInsert
  
  // 조인된 결과를 위한 타입
  export type GtcDocumentWithRelations = GtcDocument & {
    project?: {
      id: number
      code: string
      name: string
    }
    createdBy?: {
      id: number
      name: string
    }
    updatedBy?: {
      id: number
      name: string
    }
  }