import { relations } from "drizzle-orm"; import { pgTable, varchar, text, timestamp ,serial, integer, pgEnum} from "drizzle-orm/pg-core" export const items = pgTable("items", { id: serial("id").primaryKey(), itemCode: varchar("item_code", { length: 100 }).unique(), itemName: varchar("item_name", { length: 255 }).notNull(), description: text("description"), createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), }); export type Item = typeof items.$inferSelect export const itemsRelations = relations(items, ({ many }) => ({ shipbuilding: many(itemShipbuilding), })); // 조선 기능(공종) 유형 enum 정의 export const workTypeEnum = pgEnum('work_type', ['기장', '전장', '선실', '배관', '철의']); // 해양 TOP 기능(공종) 유형 enum 정의 export const offshoreTopWorkTypeEnum = pgEnum('offshore_top_work_type', ['TM', 'TS', 'TE', 'TP']); // 해양 HULL 기능(공종) 유형 enum 정의 export const offshoreHullWorkTypeEnum = pgEnum('offshore_hull_work_type', ['HA', 'HE', 'HH', 'HM', 'NC']); //조선 아이템 테이블 //아이템코드(:자재그룹코드), 아이템이름(:아이템리스트), 공종(:workType), 선종, createdAt(:생성일), updatedAt(:수정일) export const itemShipbuilding = pgTable("item_shipbuilding", { id: serial("id").primaryKey(), itemId: integer("item_id").notNull().references(() => items.id, { onDelete: 'cascade' }), workType: workTypeEnum("work_type").notNull(), shipTypes: text("ship_types").notNull().default('A-MAX'), createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), }); //조선 아이템 관계 정의 export const itemShipbuildingRelations = relations(itemShipbuilding, ({ one }) => ({ item: one(items, { fields: [itemShipbuilding.itemId], references: [items.id], }), })); export type ItemShipbuilding = typeof itemShipbuilding.$inferSelect; export type ItemWithShipbuilding = Item & ItemShipbuilding; //해양 TOP 아이템 테이블 export const itemOffshoreTop = pgTable("item_offshore_top", { id: serial("id").primaryKey(), itemId: integer("item_id").notNull().references(() => items.id, { onDelete: 'cascade' }), workType: offshoreTopWorkTypeEnum("work_type").notNull(), itemList1: text("item_list1"), itemList2: text("item_list2"), itemList3: text("item_list3"), itemList4: text("item_list4"), createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), }); //해양 HULL 아이템 테이블 export const itemOffshoreHull = pgTable("item_offshore_hull", { id: serial("id").primaryKey(), itemId: integer("item_id").notNull().references(() => items.id, { onDelete: 'cascade' }), workType: offshoreHullWorkTypeEnum("work_type").notNull(), itemList1: text("item_list1"), itemList2: text("item_list2"), itemList3: text("item_list3"), itemList4: text("item_list4"), createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), }); //해양 TOP 아이템 관계 정의 export const itemOffshoreTopRelations = relations(itemOffshoreTop, ({ one }) => ({ item: one(items, { fields: [itemOffshoreTop.itemId], references: [items.id], }), })); //해양 HULL 아이템 관계 정의 export const itemOffshoreHullRelations = relations(itemOffshoreHull, ({ one }) => ({ item: one(items, { fields: [itemOffshoreHull.itemId], references: [items.id], }), })); export type ItemOffshoreTop = typeof itemOffshoreTop.$inferSelect; export type ItemOffshoreHull = typeof itemOffshoreHull.$inferSelect; export type ItemWithOffshoreTop = Item & ItemOffshoreTop; export type ItemWithOffshoreHull = Item & ItemOffshoreHull;