summaryrefslogtreecommitdiff
path: root/db/schema/test-table-v2.ts
blob: 37ccccbd62aee35951da329ec9ec122de448a28c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Test Table Schema for client-table-v2 테스트용 스키마
 * 
 * 3가지 패턴 테스트를 위한 테이블:
 * 1. testProducts - 기본 상품 테이블 (Client-Side, Factory Service 패턴용)
 * 2. testOrders - 주문 테이블 (Custom Service 패턴용 - 조인 테스트)
 * 3. testCustomers - 고객 테이블 (Custom Service 패턴용 - 조인 테스트)
 */

import {
  integer,
  serial,
  pgTable,
  varchar,
  timestamp,
  pgEnum,
  text,
  numeric,
  boolean,
  index,
} from "drizzle-orm/pg-core";

// === Enums ===

export const testProductStatusEnum = pgEnum("test_product_status", [
  "active",
  "inactive",
  "discontinued",
]);

export const testOrderStatusEnum = pgEnum("test_order_status", [
  "pending",
  "processing",
  "shipped",
  "delivered",
  "cancelled",
]);

// === Tables ===

/**
 * 상품 테이블 - Client-Side 및 Factory Service 패턴 테스트용
 */
export const testProducts = pgTable(
  "test_products",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    name: varchar("name", { length: 255 }).notNull(),
    sku: varchar("sku", { length: 50 }).notNull().unique(),
    description: text("description"),
    category: varchar("category", { length: 100 }).notNull(),
    price: numeric("price", { precision: 10, scale: 2 }).notNull(),
    stock: integer("stock").notNull().default(0),
    status: testProductStatusEnum("status").notNull().default("active"),
    isNew: boolean("is_new").default(false),
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  },
  (table) => {
    return {
      categoryIdx: index("test_products_category_idx").on(table.category),
      statusIdx: index("test_products_status_idx").on(table.status),
    };
  }
);

/**
 * 고객 테이블 - Custom Service 패턴의 조인 테스트용
 */
export const testCustomers = pgTable(
  "test_customers",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    name: varchar("name", { length: 255 }).notNull(),
    email: varchar("email", { length: 255 }).notNull().unique(),
    phone: varchar("phone", { length: 40 }),
    country: varchar("country", { length: 100 }),
    tier: varchar("tier", { length: 40 }).notNull().default("standard"), // standard, premium, vip
    totalOrders: integer("total_orders").default(0),
    createdAt: timestamp("created_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
  },
  (table) => {
    return {
      tierIdx: index("test_customers_tier_idx").on(table.tier),
    };
  }
);

/**
 * 주문 테이블 - Custom Service 패턴용 (고객/상품 조인)
 */
export const testOrders = pgTable(
  "test_orders",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    orderNumber: varchar("order_number", { length: 50 }).notNull().unique(),
    customerId: integer("customer_id")
      .references(() => testCustomers.id, { onDelete: "cascade" })
      .notNull(),
    productId: integer("product_id")
      .references(() => testProducts.id, { onDelete: "set null" }),
    quantity: integer("quantity").notNull().default(1),
    unitPrice: numeric("unit_price", { precision: 10, scale: 2 }).notNull(),
    totalAmount: numeric("total_amount", { precision: 10, scale: 2 }).notNull(),
    status: testOrderStatusEnum("status").notNull().default("pending"),
    notes: text("notes"),
    orderedAt: timestamp("ordered_at", { withTimezone: true })
      .defaultNow()
      .notNull(),
    shippedAt: timestamp("shipped_at", { withTimezone: true }),
    deliveredAt: timestamp("delivered_at", { withTimezone: true }),
  },
  (table) => {
    return {
      customerIdx: index("test_orders_customer_idx").on(table.customerId),
      productIdx: index("test_orders_product_idx").on(table.productId),
      statusIdx: index("test_orders_status_idx").on(table.status),
      orderedAtIdx: index("test_orders_ordered_at_idx").on(table.orderedAt),
    };
  }
);

// === Types ===

export type TestProduct = typeof testProducts.$inferSelect;
export type NewTestProduct = typeof testProducts.$inferInsert;

export type TestCustomer = typeof testCustomers.$inferSelect;
export type NewTestCustomer = typeof testCustomers.$inferInsert;

export type TestOrder = typeof testOrders.$inferSelect;
export type NewTestOrder = typeof testOrders.$inferInsert;