This commit is contained in:
Your Name committed 2026-06-27 23:59:16 -04:00
1 parent 1c0313495f
commit ac387185e0
15 files changed
+1855 -12

No files matched your search

@@ -0,0 +1,79 @@
-- CreateTable
CREATE TABLE "AttributeDefinition" (
"id" SERIAL NOT NULL,
"code" TEXT NOT NULL,
"label" TEXT NOT NULL,
"description" TEXT,
"type" TEXT NOT NULL,
"unit" TEXT,
"options" JSONB,
"isRequired" BOOLEAN NOT NULL DEFAULT false,
"isFilterable" BOOLEAN NOT NULL DEFAULT false,
"sortOrder" INTEGER NOT NULL DEFAULT 0,
"categoryId" INTEGER,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "AttributeDefinition_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "AttributeDefinitionTranslation" (
"id" SERIAL NOT NULL,
"attributeDefinitionId" INTEGER NOT NULL,
"langCode" TEXT NOT NULL,
"label" TEXT NOT NULL,
"description" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "AttributeDefinitionTranslation_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ProductAttribute" (
"id" SERIAL NOT NULL,
"productId" INTEGER NOT NULL,
"attributeDefinitionId" INTEGER NOT NULL,
"valueText" TEXT,
"valueNumber" DOUBLE PRECISION,
"valueBoolean" BOOLEAN,
"valueJson" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "ProductAttribute_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "AttributeDefinition_code_key" ON "AttributeDefinition"("code");
-- CreateIndex
CREATE INDEX "AttributeDefinition_categoryId_idx" ON "AttributeDefinition"("categoryId");
-- CreateIndex
CREATE INDEX "AttributeDefinition_sortOrder_idx" ON "AttributeDefinition"("sortOrder");
-- CreateIndex
CREATE UNIQUE INDEX "AttributeDefinitionTranslation_attributeDefinitionId_langCo_key" ON "AttributeDefinitionTranslation"("attributeDefinitionId", "langCode");
-- CreateIndex
CREATE INDEX "ProductAttribute_productId_idx" ON "ProductAttribute"("productId");
-- CreateIndex
CREATE INDEX "ProductAttribute_attributeDefinitionId_idx" ON "ProductAttribute"("attributeDefinitionId");
-- CreateIndex
CREATE UNIQUE INDEX "ProductAttribute_productId_attributeDefinitionId_key" ON "ProductAttribute"("productId", "attributeDefinitionId");
-- AddForeignKey
ALTER TABLE "AttributeDefinition" ADD CONSTRAINT "AttributeDefinition_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "AttributeDefinitionTranslation" ADD CONSTRAINT "AttributeDefinitionTranslation_attributeDefinitionId_fkey" FOREIGN KEY ("attributeDefinitionId") REFERENCES "AttributeDefinition"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ProductAttribute" ADD CONSTRAINT "ProductAttribute_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ProductAttribute" ADD CONSTRAINT "ProductAttribute_attributeDefinitionId_fkey" FOREIGN KEY ("attributeDefinitionId") REFERENCES "AttributeDefinition"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,20 @@
-- CreateTable
CREATE TABLE "ProductAttributeTranslation" (
"id" SERIAL NOT NULL,
"productAttributeId" INTEGER NOT NULL,
"langCode" TEXT NOT NULL,
"valueText" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "ProductAttributeTranslation_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "ProductAttributeTranslation_productAttributeId_idx" ON "ProductAttributeTranslation"("productAttributeId");
-- CreateIndex
CREATE UNIQUE INDEX "ProductAttributeTranslation_productAttributeId_langCode_key" ON "ProductAttributeTranslation"("productAttributeId", "langCode");
-- AddForeignKey
ALTER TABLE "ProductAttributeTranslation" ADD CONSTRAINT "ProductAttributeTranslation_productAttributeId_fkey" FOREIGN KEY ("productAttributeId") REFERENCES "ProductAttribute"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+69
View File
@@ -35,6 +35,7 @@ model Category {
name String @unique
imageUrl String?
products Product[]
attributes AttributeDefinition[]
translations CategoryTranslation[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -65,6 +66,7 @@ model Product {
options Json? @default("[]") // Expected shape: [{ name: "Color", values: ["Red", "Blue"] }, ...]
variants ProductVariant[]
images ProductImage[]
attributes ProductAttribute[]
reviews Review[]
translations ProductTranslation[]
createdAt DateTime @default(now())
@@ -113,6 +115,73 @@ model ProductVariant {
@@unique([productId, option1, option2, option3])
}
model AttributeDefinition {
id Int @id @default(autoincrement())
code String @unique
label String
description String?
type String // TEXT, NUMBER, BOOLEAN, SELECT, JSON
unit String?
options Json? // Expected shape: [{ value: "floral", label: { th: "ดอกไม้", en: "Floral" } }]
isRequired Boolean @default(false)
isFilterable Boolean @default(false)
sortOrder Int @default(0)
categoryId Int?
category Category? @relation(fields: [categoryId], references: [id], onDelete: Cascade)
products ProductAttribute[]
translations AttributeDefinitionTranslation[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([categoryId])
@@index([sortOrder])
}
model AttributeDefinitionTranslation {
id Int @id @default(autoincrement())
attributeDefinitionId Int
attributeDefinition AttributeDefinition @relation(fields: [attributeDefinitionId], references: [id], onDelete: Cascade)
langCode String
label String
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([attributeDefinitionId, langCode])
}
model ProductAttribute {
id Int @id @default(autoincrement())
productId Int
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
attributeDefinitionId Int
attributeDefinition AttributeDefinition @relation(fields: [attributeDefinitionId], references: [id], onDelete: Cascade)
valueText String?
valueNumber Float?
valueBoolean Boolean?
valueJson Json?
translations ProductAttributeTranslation[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([productId, attributeDefinitionId])
@@index([productId])
@@index([attributeDefinitionId])
}
model ProductAttributeTranslation {
id Int @id @default(autoincrement())
productAttributeId Int
productAttribute ProductAttribute @relation(fields: [productAttributeId], references: [id], onDelete: Cascade)
langCode String
valueText String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([productAttributeId, langCode])
@@index([productAttributeId])
}
model User {
id Int @id @default(autoincrement())
username String @unique