This commit is contained in:
2025-06-22 11:34:32 +08:00
parent 142992a6c9
commit 7d139b22db
55 changed files with 11035 additions and 105 deletions
@@ -0,0 +1,93 @@
-- CreateEnum
CREATE TYPE "OrderStatus" AS ENUM ('PENDING', 'CONFIRMED', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED');
-- CreateTable
CREATE TABLE "users" (
"id" TEXT NOT NULL,
"email" TEXT NOT NULL,
"username" TEXT NOT NULL,
"password" TEXT NOT NULL,
"name" TEXT,
"phone" TEXT,
"address" TEXT,
"isAdmin" BOOLEAN NOT NULL DEFAULT false,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "component_types" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
CONSTRAINT "component_types_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "components" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"brand" TEXT NOT NULL,
"model" TEXT NOT NULL,
"price" DOUBLE PRECISION NOT NULL,
"description" TEXT,
"imageUrl" TEXT,
"stock" INTEGER NOT NULL DEFAULT 0,
"specifications" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"componentTypeId" TEXT NOT NULL,
CONSTRAINT "components_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orders" (
"id" TEXT NOT NULL,
"orderNumber" TEXT NOT NULL,
"totalAmount" DOUBLE PRECISION NOT NULL,
"status" "OrderStatus" NOT NULL DEFAULT 'PENDING',
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"userId" TEXT NOT NULL,
CONSTRAINT "orders_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "order_items" (
"id" TEXT NOT NULL,
"quantity" INTEGER NOT NULL DEFAULT 1,
"price" DOUBLE PRECISION NOT NULL,
"orderId" TEXT NOT NULL,
"componentId" TEXT NOT NULL,
CONSTRAINT "order_items_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
-- CreateIndex
CREATE UNIQUE INDEX "component_types_name_key" ON "component_types"("name");
-- CreateIndex
CREATE UNIQUE INDEX "orders_orderNumber_key" ON "orders"("orderNumber");
-- AddForeignKey
ALTER TABLE "components" ADD CONSTRAINT "components_componentTypeId_fkey" FOREIGN KEY ("componentTypeId") REFERENCES "component_types"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "order_items" ADD CONSTRAINT "order_items_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "order_items" ADD CONSTRAINT "order_items_componentId_fkey" FOREIGN KEY ("componentId") REFERENCES "components"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,20 @@
-- CreateTable
CREATE TABLE "cart_items" (
"id" TEXT NOT NULL,
"quantity" INTEGER NOT NULL DEFAULT 1,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"userId" TEXT NOT NULL,
"componentId" TEXT NOT NULL,
CONSTRAINT "cart_items_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "cart_items_userId_componentId_key" ON "cart_items"("userId", "componentId");
-- AddForeignKey
ALTER TABLE "cart_items" ADD CONSTRAINT "cart_items_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "cart_items" ADD CONSTRAINT "cart_items_componentId_fkey" FOREIGN KEY ("componentId") REFERENCES "components"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+3
View File
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"
+116
View File
@@ -0,0 +1,116 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(cuid())
email String @unique
username String @unique
password String
name String?
phone String?
address String?
isAdmin Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
orders Order[]
cartItems CartItem[]
@@map("users")
}
model ComponentType {
id String @id @default(cuid())
name String @unique // CPU、内存、硬盘、主板、显卡、机箱
description String?
components Component[]
@@map("component_types")
}
model Component {
id String @id @default(cuid())
name String
brand String
model String
price Float
description String?
imageUrl String?
stock Int @default(0)
specifications String? // JSON格式存储规格参数
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
componentTypeId String
componentType ComponentType @relation(fields: [componentTypeId], references: [id])
orderItems OrderItem[]
cartItems CartItem[]
@@map("components")
}
model Order {
id String @id @default(cuid())
orderNumber String @unique
totalAmount Float
status OrderStatus @default(PENDING)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
user User @relation(fields: [userId], references: [id])
orderItems OrderItem[]
@@map("orders")
}
model OrderItem {
id String @id @default(cuid())
quantity Int @default(1)
price Float
orderId String
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
componentId String
component Component @relation(fields: [componentId], references: [id])
@@map("order_items")
}
model CartItem {
id String @id @default(cuid())
quantity Int @default(1)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
componentId String
component Component @relation(fields: [componentId], references: [id], onDelete: Cascade)
@@unique([userId, componentId]) // 一个用户对同一个商品只能有一条购物车记录
@@map("cart_items")
}
enum OrderStatus {
PENDING
CONFIRMED
PROCESSING
SHIPPED
DELIVERED
CANCELLED
}