Skip to content

Commit cba1cee

Browse files
committed
se agregan modificaciones a la base de datos para soportar el nuevo requerimiento
1 parent dc11176 commit cba1cee

File tree

4 files changed

+165
-27
lines changed

4 files changed

+165
-27
lines changed

prisma/initial_data.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const categories = [
1010
alt: "Hombre luciendo polo azul",
1111
description:
1212
"Polos exclusivos con diseños que todo desarrollador querrá lucir. Ideales para llevar el código a donde vayas.",
13+
hasVariants: true, // Los polos tienen variantes de talla
1314
},
1415
{
1516
title: "Tazas",
@@ -18,6 +19,7 @@ export const categories = [
1819
alt: "Tazas con diseño de código",
1920
description:
2021
"Tazas que combinan perfectamente con tu café matutino y tu pasión por la programación. ¡Empieza el día con estilo!",
22+
hasVariants: false, // Las tazas NO tienen variantes
2123
},
2224
{
2325
title: "Stickers",
@@ -26,6 +28,44 @@ export const categories = [
2628
alt: "Stickers de desarrollo web",
2729
description:
2830
"Personaliza tu espacio de trabajo con nuestros stickers únicos y muestra tu amor por el desarrollo web.",
31+
hasVariants: true, // Los stickers tienen variantes de tamaño
32+
},
33+
];
34+
35+
// Variantes por categoría
36+
export const categoryVariants = [
37+
// Variantes para Polos (categoryId: 1) - sin modificador de precio
38+
{ categoryId: 1, value: "small", label: "S", priceModifier: 0, sortOrder: 1 },
39+
{
40+
categoryId: 1,
41+
value: "medium",
42+
label: "M",
43+
priceModifier: 0,
44+
sortOrder: 2,
45+
},
46+
{ categoryId: 1, value: "large", label: "L", priceModifier: 0, sortOrder: 3 },
47+
48+
// Variantes para Stickers (categoryId: 3) - con modificador de precio
49+
{
50+
categoryId: 3,
51+
value: "3x3",
52+
label: "3×3 cm",
53+
priceModifier: 0,
54+
sortOrder: 1,
55+
},
56+
{
57+
categoryId: 3,
58+
value: "5x5",
59+
label: "5×5 cm",
60+
priceModifier: 1.0,
61+
sortOrder: 2,
62+
},
63+
{
64+
categoryId: 3,
65+
value: "10x10",
66+
label: "10×10 cm",
67+
priceModifier: 3.0,
68+
sortOrder: 3,
2969
},
3070
];
3171

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Warnings:
3+
4+
- A unique constraint covering the columns `[cart_id,product_id,category_variant_id]` on the table `cart_items` will be added. If there are existing duplicate values, this will fail.
5+
6+
*/
7+
-- DropIndex
8+
DROP INDEX "cart_items_cart_id_product_id_key";
9+
10+
-- AlterTable
11+
ALTER TABLE "cart_items" ADD COLUMN "category_variant_id" INTEGER;
12+
13+
-- AlterTable
14+
ALTER TABLE "categories" ADD COLUMN "has_variants" BOOLEAN NOT NULL DEFAULT false;
15+
16+
-- AlterTable
17+
ALTER TABLE "order_items" ADD COLUMN "category_variant_id" INTEGER,
18+
ADD COLUMN "variant_info" TEXT;
19+
20+
-- CreateTable
21+
CREATE TABLE "category_variants" (
22+
"id" SERIAL NOT NULL,
23+
"category_id" INTEGER NOT NULL,
24+
"value" TEXT NOT NULL,
25+
"label" TEXT NOT NULL,
26+
"price_modifier" DECIMAL(10,2) NOT NULL DEFAULT 0,
27+
"sort_order" INTEGER NOT NULL DEFAULT 0,
28+
"created_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
29+
"updated_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
30+
31+
CONSTRAINT "category_variants_pkey" PRIMARY KEY ("id")
32+
);
33+
34+
-- CreateIndex
35+
CREATE UNIQUE INDEX "category_variants_category_id_value_key" ON "category_variants"("category_id", "value");
36+
37+
-- CreateIndex
38+
CREATE UNIQUE INDEX "cart_items_cart_id_product_id_category_variant_id_key" ON "cart_items"("cart_id", "product_id", "category_variant_id");
39+
40+
-- AddForeignKey
41+
ALTER TABLE "category_variants" ADD CONSTRAINT "category_variants_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;
42+
43+
-- AddForeignKey
44+
ALTER TABLE "cart_items" ADD CONSTRAINT "cart_items_category_variant_id_fkey" FOREIGN KEY ("category_variant_id") REFERENCES "category_variants"("id") ON DELETE SET NULL ON UPDATE CASCADE;
45+
46+
-- AddForeignKey
47+
ALTER TABLE "order_items" ADD CONSTRAINT "order_items_category_variant_id_fkey" FOREIGN KEY ("category_variant_id") REFERENCES "category_variants"("id") ON DELETE SET NULL ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,35 @@ model Category {
4242
imgSrc String? @map("img_src")
4343
alt String?
4444
description String?
45+
hasVariants Boolean @default(false) @map("has_variants") // Indica si esta categoría maneja variantes
4546
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
4647
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
4748
48-
products Product[]
49+
products Product[]
50+
categoryVariants CategoryVariant[]
4951
5052
@@map("categories")
5153
}
5254

55+
// Variantes disponibles para cada categoría
56+
model CategoryVariant {
57+
id Int @id @default(autoincrement())
58+
categoryId Int @map("category_id")
59+
value String // "small", "medium", "large", "3x3", "5x5", "10x10"
60+
label String // "S", "M", "L", "3×3 cm", "5×5 cm", "10×10 cm"
61+
priceModifier Decimal @default(0) @map("price_modifier") @db.Decimal(10, 2)
62+
sortOrder Int @default(0) @map("sort_order") // Para ordenar las opciones
63+
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
64+
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
65+
66+
category Category @relation(fields: [categoryId], references: [id], onDelete: Cascade)
67+
cartItems CartItem[]
68+
orderItems OrderItem[]
69+
70+
@@unique([categoryId, value])
71+
@@map("category_variants")
72+
}
73+
5374
model Product {
5475
id Int @id @default(autoincrement())
5576
title String
@@ -83,18 +104,21 @@ model Cart {
83104
@@map("carts")
84105
}
85106

107+
// Actualizar CartItem para variantes de categoría
86108
model CartItem {
87-
id Int @id @default(autoincrement())
88-
cartId Int @map("cart_id")
89-
productId Int @map("product_id")
90-
quantity Int
91-
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
92-
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
93-
94-
cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)
95-
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
96-
97-
@@unique([cartId, productId], name: "unique_cart_item")
109+
id Int @id @default(autoincrement())
110+
cartId Int @map("cart_id")
111+
productId Int @map("product_id")
112+
categoryVariantId Int? @map("category_variant_id") // Variante seleccionada
113+
quantity Int
114+
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
115+
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
116+
117+
cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)
118+
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
119+
categoryVariant CategoryVariant? @relation(fields: [categoryVariantId], references: [id], onDelete: SetNull)
120+
121+
@@unique([cartId, productId, categoryVariantId], name: "unique_cart_item_variant")
98122
@@map("cart_items")
99123
}
100124

@@ -122,19 +146,23 @@ model Order {
122146
@@map("orders")
123147
}
124148

149+
// Actualizar OrderItem
125150
model OrderItem {
126-
id Int @id @default(autoincrement())
127-
orderId Int @map("order_id")
128-
productId Int? @map("product_id")
129-
quantity Int
130-
title String
131-
price Decimal @db.Decimal(10, 2)
132-
imgSrc String? @map("img_src")
133-
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
134-
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
135-
136-
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
137-
product Product? @relation(fields: [productId], references: [id], onDelete: SetNull)
151+
id Int @id @default(autoincrement())
152+
orderId Int @map("order_id")
153+
productId Int? @map("product_id")
154+
categoryVariantId Int? @map("category_variant_id")
155+
quantity Int
156+
title String
157+
variantInfo String? @map("variant_info") // "Talla: M" o "Tamaño: 5×5 cm"
158+
price Decimal @db.Decimal(10, 2) // Precio final (base + modificador)
159+
imgSrc String? @map("img_src")
160+
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
161+
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
162+
163+
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
164+
product Product? @relation(fields: [productId], references: [id], onDelete: SetNull)
165+
categoryVariant CategoryVariant? @relation(fields: [categoryVariantId], references: [id], onDelete: SetNull)
138166
139167
@@map("order_items")
140-
}
168+
}

prisma/seed.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
import { categories, products } from "./initial_data";
1+
import { categories, categoryVariants, products } from "./initial_data";
22
import { PrismaClient } from "../generated/prisma/client";
33

44
const prisma = new PrismaClient();
55

66
async function seedDb() {
7+
await prisma.orderItem.deleteMany();
8+
console.log(" ✅ OrderItems eliminados");
9+
10+
await prisma.cart.deleteMany();
11+
console.log(" ✅ Carts eliminados");
12+
13+
await prisma.cartItem.deleteMany();
14+
console.log(" ✅ CartItems eliminados");
15+
16+
await prisma.categoryVariant.deleteMany();
17+
console.log(" ✅ CategoryVariants eliminados");
18+
19+
await prisma.product.deleteMany();
20+
console.log(" ✅ Products eliminados");
21+
22+
await prisma.category.deleteMany();
23+
console.log(" ✅ Categories eliminadas");
24+
725
await prisma.category.createMany({
826
data: categories,
927
});
1028
console.log("1. Categories successfully inserted");
1129

30+
await prisma.categoryVariant.createMany({
31+
data: categoryVariants,
32+
});
33+
console.log("2. Category variants successfully inserted");
34+
1235
await prisma.product.createMany({
1336
data: products,
1437
});
15-
console.log("2. Products successfully inserted");
38+
console.log("3. Products successfully inserted");
1639
}
1740

1841
seedDb()

0 commit comments

Comments
 (0)