Persist user carts in database
This commit is contained in:
34
prisma/migrations/0006_cart/migration.sql
Normal file
34
prisma/migrations/0006_cart/migration.sql
Normal file
@@ -0,0 +1,34 @@
|
||||
CREATE TABLE "Cart" (
|
||||
"id" TEXT NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
"deliveryAddressId" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "Cart_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "CartItem" (
|
||||
"id" TEXT NOT NULL,
|
||||
"cartId" TEXT NOT NULL,
|
||||
"productId" TEXT NOT NULL,
|
||||
"productName" TEXT NOT NULL,
|
||||
"sku" TEXT NOT NULL,
|
||||
"isCustomizable" BOOLEAN NOT NULL DEFAULT false,
|
||||
"quantity" DECIMAL(14,3) NOT NULL,
|
||||
"parameters" JSONB NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "CartItem_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "Cart_userId_key" ON "Cart"("userId");
|
||||
CREATE INDEX "Cart_deliveryAddressId_idx" ON "Cart"("deliveryAddressId");
|
||||
CREATE UNIQUE INDEX "CartItem_cartId_productId_key" ON "CartItem"("cartId", "productId");
|
||||
CREATE INDEX "CartItem_productId_idx" ON "CartItem"("productId");
|
||||
|
||||
ALTER TABLE "Cart" ADD CONSTRAINT "Cart_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
ALTER TABLE "Cart" ADD CONSTRAINT "Cart_deliveryAddressId_fkey" FOREIGN KEY ("deliveryAddressId") REFERENCES "DeliveryAddress"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
ALTER TABLE "CartItem" ADD CONSTRAINT "CartItem_cartId_fkey" FOREIGN KEY ("cartId") REFERENCES "Cart"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
ALTER TABLE "CartItem" ADD CONSTRAINT "CartItem_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -70,6 +70,7 @@ model User {
|
||||
sentInvitations Invitation[] @relation("InvitationManager")
|
||||
acceptedInvitations Invitation[] @relation("InvitationAcceptor")
|
||||
messengerConnections MessengerConnection[]
|
||||
cart Cart?
|
||||
clientOrders Order[] @relation("OrderClient")
|
||||
managerOrders Order[] @relation("OrderManager")
|
||||
orderStatusEvents OrderStatusEvent[]
|
||||
@@ -91,6 +92,7 @@ model DeliveryAddress {
|
||||
unrestrictedValue String?
|
||||
fiasId String?
|
||||
defaultForUsers User[] @relation("UserDefaultDeliveryAddress")
|
||||
carts Cart[]
|
||||
orders Order[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
@@ -179,11 +181,43 @@ model Product {
|
||||
isCustomizable Boolean @default(false)
|
||||
isActive Boolean @default(true)
|
||||
inventory ProductStock[]
|
||||
cartItems CartItem[]
|
||||
orderItems OrderItem[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model Cart {
|
||||
id String @id @default(cuid())
|
||||
userId String @unique
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
deliveryAddressId String?
|
||||
deliveryAddress DeliveryAddress? @relation(fields: [deliveryAddressId], references: [id], onDelete: SetNull)
|
||||
items CartItem[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([deliveryAddressId])
|
||||
}
|
||||
|
||||
model CartItem {
|
||||
id String @id @default(cuid())
|
||||
cartId String
|
||||
cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)
|
||||
productId String
|
||||
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
|
||||
productName String
|
||||
sku String
|
||||
isCustomizable Boolean @default(false)
|
||||
quantity Decimal @db.Decimal(14, 3)
|
||||
parameters Json
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@unique([cartId, productId])
|
||||
@@index([productId])
|
||||
}
|
||||
|
||||
model Warehouse {
|
||||
id String @id @default(cuid())
|
||||
code String @unique
|
||||
|
||||
Reference in New Issue
Block a user