Skip to content

Commit

Permalink
feat: 🎸 add cart and product model
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Apr 26, 2022
1 parent 8e3dabb commit 4a6830b
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-- CreateTable
CREATE TABLE "cart" (
"id" UUID NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"users_id" UUID,

CONSTRAINT "cart_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "product" (
"id" UUID NOT NULL,
"title" TEXT,
"description" TEXT,
"price" INTEGER,
"discount_percentage" DECIMAL(65,30),
"rating" DECIMAL(65,30),
"stock" DECIMAL(65,30),
"brand" TEXT,
"category" TEXT,
"thumbnail" TEXT,
"images" TEXT[],
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"cart_id" UUID,

CONSTRAINT "product_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "cart_users_id_key" ON "cart"("users_id");

-- CreateIndex
CREATE INDEX "index_cart_on_created_at" ON "cart"("created_at");

-- CreateIndex
CREATE INDEX "index_cart_on_updated_at" ON "cart"("updated_at");

-- CreateIndex
CREATE INDEX "index_cart_on_users_id" ON "cart"("users_id");

-- CreateIndex
CREATE INDEX "index_product_on_created_at" ON "product"("created_at");

-- CreateIndex
CREATE INDEX "index_product_on_title" ON "product"("title");

-- CreateIndex
CREATE INDEX "index_product_on_description" ON "product"("description");

-- CreateIndex
CREATE INDEX "index_product_on_price" ON "product"("price");

-- CreateIndex
CREATE INDEX "index_product_on_discount_percentage" ON "product"("discount_percentage");

-- CreateIndex
CREATE INDEX "index_product_on_rating" ON "product"("rating");

-- CreateIndex
CREATE INDEX "index_product_on_stock" ON "product"("stock");

-- CreateIndex
CREATE INDEX "index_product_on_brand" ON "product"("brand");

-- CreateIndex
CREATE INDEX "index_product_on_category" ON "product"("category");

-- CreateIndex
CREATE INDEX "index_product_on_thumbnail" ON "product"("thumbnail");

-- CreateIndex
CREATE INDEX "index_product_on_images" ON "product"("images");

-- CreateIndex
CREATE INDEX "index_product_on_updated_at" ON "product"("updated_at");

-- CreateIndex
CREATE INDEX "index_product_on_cart_id" ON "product"("cart_id");

-- AddForeignKey
ALTER TABLE "cart" ADD CONSTRAINT "cart_users_id_fkey" FOREIGN KEY ("users_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product" ADD CONSTRAINT "product_cart_id_fkey" FOREIGN KEY ("cart_id") REFERENCES "cart"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to alter the column `discount_percentage` on the `product` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Decimal(10,2)`.
- You are about to alter the column `rating` on the `product` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Decimal(10,2)`.
- You are about to alter the column `stock` on the `product` table. The data in that column could be lost. The data in that column will be cast from `Decimal(65,30)` to `Decimal(10,2)`.
*/
-- AlterTable
ALTER TABLE "product" ALTER COLUMN "discount_percentage" SET DATA TYPE DECIMAL(10,2),
ALTER COLUMN "rating" SET DATA TYPE DECIMAL(10,2),
ALTER COLUMN "stock" SET DATA TYPE DECIMAL(10,2);
46 changes: 46 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ model users {
created_at DateTime @default(now())
updated_at DateTime @default(now())
password String?
cart cart?
comments comment[]
locations location[]
posts post[]
Expand Down Expand Up @@ -131,6 +132,51 @@ model todo {
@@index([users_id], map: "index_todo_on_users_id")
}

model cart {
id String @id @default(uuid()) @db.Uuid
created_at DateTime @default(now())
updated_at DateTime @default(now())
users_id String? @unique @db.Uuid
users users? @relation(fields: [users_id], references: [id])
products product[]
@@index([created_at], map: "index_cart_on_created_at")
@@index([updated_at], map: "index_cart_on_updated_at")
@@index([users_id], map: "index_cart_on_users_id")
}

model product {
id String @id @default(uuid()) @db.Uuid
title String?
description String?
price Int?
discount_percentage Decimal? @db.Decimal(10, 2)
rating Decimal? @db.Decimal(10, 2)
stock Decimal? @db.Decimal(10, 2)
brand String?
category String?
thumbnail String?
images String[]
created_at DateTime @default(now())
updated_at DateTime @default(now())
cart_id String? @db.Uuid
cart cart? @relation(fields: [cart_id], references: [id])
@@index([brand], map: "index_product_on_brand")
@@index([cart_id], map: "index_product_on_cart_id")
@@index([category], map: "index_product_on_category")
@@index([created_at], map: "index_product_on_created_at")
@@index([description], map: "index_product_on_description")
@@index([discount_percentage], map: "index_product_on_discount_percentage")
@@index([images], map: "index_product_on_images")
@@index([price], map: "index_product_on_price")
@@index([rating], map: "index_product_on_rating")
@@index([stock], map: "index_product_on_stock")
@@index([thumbnail], map: "index_product_on_thumbnail")
@@index([title], map: "index_product_on_title")
@@index([updated_at], map: "index_product_on_updated_at")
}

model quote {
id String @id @default(uuid()) @db.Uuid
content String?
Expand Down
60 changes: 60 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const prisma = new PrismaClient();
await createTags();
await createComments();
await createTodos();
await createCarts();
await createProducts();
await createQuotes();
})();

Expand Down Expand Up @@ -187,6 +189,64 @@ async function createTodos() {
}
}

async function createCarts() {
const cartsDataList = [];

const users = await prisma.users.findMany({
take: 5,
orderBy: {
created_at: 'desc',
},
});

for (let index = 0; index < 5; index++) {
const cartData = {
users_id: users[index].id,
};
cartsDataList.push(cartData);
}

if (cartsDataList) {
await prisma.cart.createMany({
data: cartsDataList,
});
}
}

async function createProducts() {
const productsDataList = [];

const carts = await prisma.cart.findMany({
take: 5,
orderBy: {
created_at: 'desc',
},
});

for (let index = 0; index < 5; index++) {
const productsData = {
title: faker.lorem.word(),
description: faker.lorem.words(),
price: faker.datatype.number(),
discount_percentage: faker.datatype.float(),
rating: faker.datatype.float(),
stock: faker.datatype.float(),
brand: faker.lorem.word(),
category: faker.lorem.word(),
thumbnail: faker.image.image(),
images: [faker.image.image(), faker.image.image(), faker.image.image()],
cart_id: carts[index].id,
};
productsDataList.push(productsData);
}

if (productsDataList) {
await prisma.product.createMany({
data: productsDataList,
});
}
}

async function createQuotes() {
const rootUrl = `https://api.quotable.io`;
const response = await axios.get(`${rootUrl}/quotes`, {
Expand Down

0 comments on commit 4a6830b

Please sign in to comment.