For Frontend practice
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
email String @unique
|
||||
passwordHash String
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
|
||||
model Category {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @unique
|
||||
slug String @unique
|
||||
createdAt DateTime @default(now())
|
||||
products Product[]
|
||||
}
|
||||
|
||||
model Product {
|
||||
id Int @id @default(autoincrement())
|
||||
categoryId Int
|
||||
name String @unique
|
||||
description String?
|
||||
price Decimal @db.Decimal(10, 2)
|
||||
imageUrl String?
|
||||
stock Int @default(0)
|
||||
createdAt DateTime @default(now())
|
||||
category Category @relation(fields: [categoryId], references: [id], onDelete: Restrict)
|
||||
orderItems OrderItem[]
|
||||
}
|
||||
|
||||
model Order {
|
||||
id Int @id @default(autoincrement())
|
||||
customerName String
|
||||
customerEmail String
|
||||
totalPrice Decimal @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
items OrderItem[]
|
||||
}
|
||||
|
||||
model OrderItem {
|
||||
id Int @id @default(autoincrement())
|
||||
orderId Int
|
||||
productId Int
|
||||
quantity Int
|
||||
unitPrice Decimal @db.Decimal(10, 2)
|
||||
order Order @relation(fields: [orderId], references: [id], onDelete: Cascade)
|
||||
product Product @relation(fields: [productId], references: [id], onDelete: Restrict)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
const { PrismaClient } = require("@prisma/client");
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
const categories = [
|
||||
{ name: "Shoes", slug: "shoes" },
|
||||
{ name: "Bags", slug: "bags" },
|
||||
{ name: "Accessories", slug: "accessories" }
|
||||
];
|
||||
|
||||
for (const category of categories) {
|
||||
await prisma.category.upsert({
|
||||
where: { slug: category.slug },
|
||||
update: {},
|
||||
create: category
|
||||
});
|
||||
}
|
||||
|
||||
const shoes = await prisma.category.findUnique({ where: { slug: "shoes" } });
|
||||
const bags = await prisma.category.findUnique({ where: { slug: "bags" } });
|
||||
const accessories = await prisma.category.findUnique({ where: { slug: "accessories" } });
|
||||
|
||||
const products = [
|
||||
{
|
||||
categoryId: shoes.id,
|
||||
name: "Street Runner",
|
||||
description: "Lightweight city sneaker.",
|
||||
price: "18990.00",
|
||||
imageUrl: "/images/street-runner.jpg",
|
||||
stock: 24
|
||||
},
|
||||
{
|
||||
categoryId: shoes.id,
|
||||
name: "Trail Edge",
|
||||
description: "Stable shoe for outdoor tracks.",
|
||||
price: "24990.00",
|
||||
imageUrl: "/images/trail-edge.jpg",
|
||||
stock: 13
|
||||
},
|
||||
{
|
||||
categoryId: bags.id,
|
||||
name: "Urban Tote",
|
||||
description: "Everyday tote with zipper top.",
|
||||
price: "14990.00",
|
||||
imageUrl: "/images/urban-tote.jpg",
|
||||
stock: 30
|
||||
},
|
||||
{
|
||||
categoryId: accessories.id,
|
||||
name: "Classic Cap",
|
||||
description: "Adjustable cotton cap.",
|
||||
price: "6990.00",
|
||||
imageUrl: "/images/classic-cap.jpg",
|
||||
stock: 42
|
||||
}
|
||||
];
|
||||
|
||||
for (const product of products) {
|
||||
await prisma.product.upsert({
|
||||
where: { name: product.name },
|
||||
update: {},
|
||||
create: product
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
.then(async () => {
|
||||
await prisma.$disconnect();
|
||||
console.log("Prisma seed completed.");
|
||||
})
|
||||
.catch(async (error) => {
|
||||
console.error(error);
|
||||
await prisma.$disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user