115 lines
4.3 KiB
TypeScript
115 lines
4.3 KiB
TypeScript
import {
|
|
bigint, // BIGINT (大整数, -2^63 到 2^63-1)
|
|
bigserial, // BIGSERIAL (自增大整数, 1 到 2^63-1)
|
|
boolean, // BOOLEAN (布尔值)
|
|
char, // CHAR (定长字符串)
|
|
check, // 定义检查约束
|
|
date, // DATE (日期)
|
|
decimal, // DECIMAL/NUMERIC (精确数字, 指定 precision 和 scale)
|
|
doublePrecision, // DOUBLE PRECISION (双精度浮点数, 8字节)
|
|
foreignKey, // 定义外键
|
|
index, // 创建索引
|
|
integer, // INTEGER (整数, -2^31 到 2^31-1)
|
|
interval, // INTERVAL (时间间隔)
|
|
json, // JSON (JSON 数据)
|
|
jsonb, // JSONB (二进制 JSON, 推荐使用)
|
|
numeric, // NUMERIC (同 decimal)
|
|
pgEnum, // 定义枚举类型 (PostgreSQL ENUM)
|
|
pgMaterializedView, // 创建物化视图
|
|
pgPolicy, // 定义行级安全策略
|
|
pgRole, // 定义角色
|
|
pgSequence, // 创建序列
|
|
pgTable, // 定义表
|
|
pgTableCreator, // 创建自定义表名生成器的表定义函数
|
|
pgView, // 创建视图
|
|
primaryKey, // 定义主键
|
|
serial, // SERIAL (自增整数, 1 到 2^31-1)
|
|
text, // TEXT (变长字符串, 无长度限制)
|
|
time, // TIME (时间)
|
|
timestamp, // TIMESTAMP (日期时间, 无时区)
|
|
unique, // 定义唯一约束
|
|
uniqueIndex, // 创建唯一索引
|
|
uuid, // UUID (通用唯一标识符)
|
|
varchar, // VARCHAR (变长字符串, 指定长度)
|
|
} from "drizzle-orm/pg-core";
|
|
|
|
import { sql } from "drizzle-orm";
|
|
|
|
import { id, createdAt, updatedAt } from "../common";
|
|
import { user } from "./auth.db";
|
|
import { attributeFile } from "./basic.db";
|
|
|
|
// Enums
|
|
export const catGenderEnum = pgEnum("cat_gender", ["male", "female"]);
|
|
export const catGradeEnum = pgEnum("cat_grade", ["A", "B", "C", "D"]);
|
|
export const catTypeEnum = pgEnum("cat_type", ["breeding_male", "breeding_female", "kitten"]);
|
|
export const reservationStatusEnum = pgEnum("reservation_status", ["queuing", "reserved", "completed", "cancelled"]);
|
|
export const orderStatusEnum = pgEnum("order_status", ["pending", "paid", "shipped", "delivered", "cancelled"]);
|
|
|
|
// Tables
|
|
export const shelter = pgTable("shelter", {
|
|
id,
|
|
name: varchar("name", { length: 255 }).notNull(),
|
|
description: text("description"),
|
|
address: varchar("address", { length: 255 }),
|
|
phone: varchar("phone", { length: 20 }),
|
|
imageIds: jsonb("image_ids").$type<number[]>().default([]),
|
|
createdAt,
|
|
updatedAt,
|
|
});
|
|
|
|
export const cat = pgTable("cat", {
|
|
id,
|
|
name: varchar("name", { length: 255 }).notNull(),
|
|
age: integer("age").notNull(), // in months
|
|
gender: catGenderEnum("gender").notNull(),
|
|
grade: catGradeEnum("grade").notNull(),
|
|
type: catTypeEnum("type").notNull(),
|
|
isAvailable: boolean("is_available").default(true).notNull(),
|
|
description: text("description"),
|
|
imageIds: jsonb("image_ids").$type<number[]>().default([]),
|
|
createdAt,
|
|
updatedAt,
|
|
});
|
|
|
|
export const reservation = pgTable("reservation", {
|
|
id,
|
|
userId: text("user_id").notNull().references(() => user.id, { onDelete: "cascade" }),
|
|
catId: integer("cat_id").references(() => cat.id, { onDelete: "set null" }),
|
|
deposit: numeric("deposit", { precision: 10, scale: 2 }).notNull(),
|
|
status: reservationStatusEnum("status").default("queuing").notNull(),
|
|
queueOrder: integer("queue_order"),
|
|
createdAt,
|
|
updatedAt,
|
|
});
|
|
|
|
export const product = pgTable("product", {
|
|
id,
|
|
name: varchar("name", { length: 255 }).notNull(),
|
|
description: text("description"),
|
|
price: numeric("price", { precision: 10, scale: 2 }).notNull(),
|
|
stock: integer("stock").default(0).notNull(),
|
|
imageIds: jsonb("image_ids").$type<number[]>().default([]),
|
|
createdAt,
|
|
updatedAt,
|
|
});
|
|
|
|
export const orderTable = pgTable("orders", {
|
|
id,
|
|
userId: text("user_id").notNull().references(() => user.id, { onDelete: "cascade" }),
|
|
totalPrice: numeric("total_price", { precision: 10, scale: 2 }).notNull(),
|
|
status: orderStatusEnum("status").default("pending").notNull(),
|
|
createdAt,
|
|
updatedAt,
|
|
});
|
|
|
|
export const orderItem = pgTable("order_item", {
|
|
id,
|
|
orderId: integer("order_id").notNull().references(() => orderTable.id, { onDelete: "cascade" }),
|
|
productId: integer("product_id").notNull().references(() => product.id, { onDelete: "cascade" }),
|
|
quantity: integer("quantity").notNull(),
|
|
unitPrice: numeric("unit_price", { precision: 10, scale: 2 }).notNull(),
|
|
createdAt,
|
|
updatedAt,
|
|
});
|