-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
138 lines (126 loc) · 4.05 KB
/
schema.ts
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
import {
pgTable,
varchar,
date,
text,
foreignKey,
integer,
doublePrecision,
index,
serial,
} from "drizzle-orm/pg-core";
export const customers = pgTable(
"customers",
{
id: serial("id").primaryKey(),
companyName: text("company_name").notNull(),
contactName: varchar("contact_name").notNull(),
contactTitle: varchar("contact_title").notNull(),
address: varchar("address").notNull(),
city: varchar("city").notNull(),
postalCode: varchar("postal_code"),
region: varchar("region"),
country: varchar("country").notNull(),
phone: varchar("phone").notNull(),
fax: varchar("fax"),
}
);
export const employees = pgTable(
"employees",
{
id: serial("id").primaryKey(),
lastName: varchar("last_name").notNull(),
firstName: varchar("first_name"),
title: varchar("title").notNull(),
titleOfCourtesy: varchar("title_of_courtesy").notNull(),
birthDate: date("birth_date", { mode: "date" }).notNull(),
hireDate: date("hire_date", { mode: "date" }).notNull(),
address: varchar("address").notNull(),
city: varchar("city").notNull(),
postalCode: varchar("postal_code").notNull(),
country: varchar("country").notNull(),
homePhone: varchar("home_phone").notNull(),
extension: integer("extension").notNull(),
notes: text("notes").notNull(),
recipientId: integer("recipient_id"),
},
(table) => ({
recipientFk: foreignKey({
columns: [table.recipientId],
foreignColumns: [table.id],
}),
recepientIdx: index("recepient_idx").on(table.recipientId),
})
);
export const orders = pgTable("orders", {
id: serial("id").primaryKey(),
orderDate: date("order_date", { mode: "date" }).notNull(),
requiredDate: date("required_date", { mode: "date" }).notNull(),
shippedDate: date("shipped_date", { mode: "date" }),
shipVia: integer("ship_via").notNull(),
freight: doublePrecision("freight").notNull(),
shipName: varchar("ship_name").notNull(),
shipCity: varchar("ship_city").notNull(),
shipRegion: varchar("ship_region"),
shipPostalCode: varchar("ship_postal_code"),
shipCountry: varchar("ship_country").notNull(),
customerId: integer("customer_id")
.notNull()
.references(() => customers.id, { onDelete: "cascade" }),
employeeId: integer("employee_id")
.notNull()
.references(() => employees.id, { onDelete: "cascade" }),
});
export const suppliers = pgTable("suppliers", {
id: serial("id").primaryKey(),
companyName: varchar("company_name").notNull(),
contactName: varchar("contact_name").notNull(),
contactTitle: varchar("contact_title").notNull(),
address: varchar("address").notNull(),
city: varchar("city").notNull(),
region: varchar("region"),
postalCode: varchar("postal_code").notNull(),
country: varchar("country").notNull(),
phone: varchar("phone").notNull(),
});
export const products = pgTable(
"products",
{
id: serial("id").primaryKey(),
name: text("name").notNull(),
quantityPerUnit: varchar("qt_per_unit").notNull(),
unitPrice: doublePrecision("unit_price").notNull(),
unitsInStock: integer("units_in_stock").notNull(),
unitsOnOrder: integer("units_on_order").notNull(),
reorderLevel: integer("reorder_level").notNull(),
discontinued: integer("discontinued").notNull(),
supplierId: serial("supplier_id")
.notNull()
.references(() => suppliers.id, { onDelete: "cascade" }),
},
(table) => {
return {
supplierIdx: index("supplier_idx").on(table.supplierId),
};
}
);
export const details = pgTable(
"order_details",
{
unitPrice: doublePrecision("unit_price").notNull(),
quantity: integer("quantity").notNull(),
discount: doublePrecision("discount").notNull(),
orderId: integer("order_id")
.notNull()
.references(() => orders.id, { onDelete: "cascade" }),
productId: integer("product_id")
.notNull()
.references(() => products.id, { onDelete: "cascade" }),
},
(t) => {
return {
orderIdIdx: index("order_id_idx").on(t.orderId),
productIdIdx: index("product_id_idx").on(t.productId),
};
}
);