feat: Configuration de la base de données avec Prisma - Ajout du schéma de base de données et des migrations initiales

This commit is contained in:
Matthieu
2025-07-29 21:03:16 +02:00
parent f05d59ed95
commit dbc89327eb
5 changed files with 438 additions and 0 deletions

View File

@@ -0,0 +1,208 @@
-- CreateTable
CREATE TABLE "sites" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "sites_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "type_machines" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"category" TEXT,
"maintenanceFrequency" TEXT,
"components" JSONB,
"criticalParts" JSONB,
"specifications" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "type_machines_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "type_composants" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "type_composants_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "type_pieces" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "type_pieces_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "machines" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"reference" TEXT,
"prestataire" TEXT,
"prix" DECIMAL(10,2),
"emplacement" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"siteId" TEXT NOT NULL,
"typeMachineId" TEXT,
CONSTRAINT "machines_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "composants" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"reference" TEXT,
"prestataire" TEXT,
"prix" DECIMAL(10,2),
"emplacement" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"machineId" TEXT,
"parentComposantId" TEXT,
"typeComposantId" TEXT,
CONSTRAINT "composants_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "pieces" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"reference" TEXT,
"prestataire" TEXT,
"prix" DECIMAL(10,2),
"emplacement" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"machineId" TEXT,
"composantId" TEXT,
"typePieceId" TEXT,
CONSTRAINT "pieces_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "documents" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"filename" TEXT NOT NULL,
"path" TEXT NOT NULL,
"mimeType" TEXT NOT NULL,
"size" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"machineId" TEXT,
"composantId" TEXT,
"pieceId" TEXT,
CONSTRAINT "documents_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "custom_fields" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"type" TEXT NOT NULL,
"required" BOOLEAN NOT NULL DEFAULT false,
"defaultValue" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"typeMachineId" TEXT,
"typeComposantId" TEXT,
"typePieceId" TEXT,
CONSTRAINT "custom_fields_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "custom_field_values" (
"id" TEXT NOT NULL,
"value" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"customFieldId" TEXT NOT NULL,
"machineId" TEXT,
"composantId" TEXT,
"pieceId" TEXT,
CONSTRAINT "custom_field_values_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "type_machines_name_key" ON "type_machines"("name");
-- CreateIndex
CREATE UNIQUE INDEX "type_composants_name_key" ON "type_composants"("name");
-- CreateIndex
CREATE UNIQUE INDEX "type_pieces_name_key" ON "type_pieces"("name");
-- AddForeignKey
ALTER TABLE "machines" ADD CONSTRAINT "machines_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "sites"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "machines" ADD CONSTRAINT "machines_typeMachineId_fkey" FOREIGN KEY ("typeMachineId") REFERENCES "type_machines"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "composants" ADD CONSTRAINT "composants_machineId_fkey" FOREIGN KEY ("machineId") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "composants" ADD CONSTRAINT "composants_parentComposantId_fkey" FOREIGN KEY ("parentComposantId") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "composants" ADD CONSTRAINT "composants_typeComposantId_fkey" FOREIGN KEY ("typeComposantId") REFERENCES "type_composants"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pieces" ADD CONSTRAINT "pieces_machineId_fkey" FOREIGN KEY ("machineId") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pieces" ADD CONSTRAINT "pieces_composantId_fkey" FOREIGN KEY ("composantId") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "pieces" ADD CONSTRAINT "pieces_typePieceId_fkey" FOREIGN KEY ("typePieceId") REFERENCES "type_pieces"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "documents" ADD CONSTRAINT "documents_machineId_fkey" FOREIGN KEY ("machineId") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "documents" ADD CONSTRAINT "documents_composantId_fkey" FOREIGN KEY ("composantId") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "documents" ADD CONSTRAINT "documents_pieceId_fkey" FOREIGN KEY ("pieceId") REFERENCES "pieces"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_fields" ADD CONSTRAINT "custom_fields_typeMachineId_fkey" FOREIGN KEY ("typeMachineId") REFERENCES "type_machines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_fields" ADD CONSTRAINT "custom_fields_typeComposantId_fkey" FOREIGN KEY ("typeComposantId") REFERENCES "type_composants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_fields" ADD CONSTRAINT "custom_fields_typePieceId_fkey" FOREIGN KEY ("typePieceId") REFERENCES "type_pieces"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_field_values" ADD CONSTRAINT "custom_field_values_customFieldId_fkey" FOREIGN KEY ("customFieldId") REFERENCES "custom_fields"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_field_values" ADD CONSTRAINT "custom_field_values_machineId_fkey" FOREIGN KEY ("machineId") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_field_values" ADD CONSTRAINT "custom_field_values_composantId_fkey" FOREIGN KEY ("composantId") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_field_values" ADD CONSTRAINT "custom_field_values_pieceId_fkey" FOREIGN KEY ("pieceId") REFERENCES "pieces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "type_machines" ADD COLUMN "machinePieces" JSONB;

View File

@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "custom_fields" ADD COLUMN "options" TEXT[];

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"

223
prisma/schema.prisma Normal file
View File

@@ -0,0 +1,223 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// Entités principales
model Site {
id String @id @default(cuid())
name String
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
machines Machine[]
@@map("sites")
}
model TypeMachine {
id String @id @default(cuid())
name String @unique
description String?
category String?
maintenanceFrequency String?
components Json? // Stockage de la structure hiérarchique des composants
criticalParts Json? // Stockage des pièces critiques
machinePieces Json? // Stockage des pièces de machine
specifications Json? // Stockage des spécifications techniques
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
machines Machine[]
customFields CustomField[] @relation("TypeMachineCustomFields")
@@map("type_machines")
}
model TypeComposant {
id String @id @default(cuid())
name String @unique
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
composants Composant[]
customFields CustomField[] @relation("TypeComposantCustomFields")
@@map("type_composants")
}
model TypePiece {
id String @id @default(cuid())
name String @unique
description String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
pieces Piece[]
customFields CustomField[] @relation("TypePieceCustomFields")
@@map("type_pieces")
}
model Machine {
id String @id @default(cuid())
name String
reference String?
prestataire String?
prix Decimal? @db.Decimal(10, 2)
emplacement String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
siteId String
site Site @relation(fields: [siteId], references: [id], onDelete: Cascade)
typeMachineId String?
typeMachine TypeMachine? @relation(fields: [typeMachineId], references: [id])
composants Composant[]
pieces Piece[]
documents Document[] @relation("MachineDocuments")
customFieldValues CustomFieldValue[] @relation("MachineCustomFieldValues")
@@map("machines")
}
model Composant {
id String @id @default(cuid())
name String
reference String?
prestataire String?
prix Decimal? @db.Decimal(10, 2)
emplacement String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations hiérarchiques
machineId String?
machine Machine? @relation(fields: [machineId], references: [id], onDelete: Cascade)
parentComposantId String?
parentComposant Composant? @relation("ComposantHierarchy", fields: [parentComposantId], references: [id], onDelete: Cascade)
sousComposants Composant[] @relation("ComposantHierarchy")
typeComposantId String?
typeComposant TypeComposant? @relation(fields: [typeComposantId], references: [id])
pieces Piece[]
documents Document[] @relation("ComposantDocuments")
customFieldValues CustomFieldValue[] @relation("ComposantCustomFieldValues")
@@map("composants")
}
model Piece {
id String @id @default(cuid())
name String
reference String?
prestataire String?
prix Decimal? @db.Decimal(10, 2)
emplacement String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
machineId String?
machine Machine? @relation(fields: [machineId], references: [id], onDelete: Cascade)
composantId String?
composant Composant? @relation(fields: [composantId], references: [id], onDelete: Cascade)
typePieceId String?
typePiece TypePiece? @relation(fields: [typePieceId], references: [id])
documents Document[] @relation("PieceDocuments")
customFieldValues CustomFieldValue[] @relation("PieceCustomFieldValues")
@@map("pieces")
}
model Document {
id String @id @default(cuid())
name String
filename String
path String
mimeType String
size Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations polymorphiques
machineId String?
machine Machine? @relation("MachineDocuments", fields: [machineId], references: [id], onDelete: Cascade)
composantId String?
composant Composant? @relation("ComposantDocuments", fields: [composantId], references: [id], onDelete: Cascade)
pieceId String?
piece Piece? @relation("PieceDocuments", fields: [pieceId], references: [id], onDelete: Cascade)
@@map("documents")
}
model CustomField {
id String @id @default(cuid())
name String
type String // 'string', 'number', 'boolean', 'date'
required Boolean @default(false)
defaultValue String?
options String[] // Pour les champs de type SELECT
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations polymorphiques pour les types
typeMachineId String?
typeMachine TypeMachine? @relation("TypeMachineCustomFields", fields: [typeMachineId], references: [id], onDelete: Cascade)
typeComposantId String?
typeComposant TypeComposant? @relation("TypeComposantCustomFields", fields: [typeComposantId], references: [id], onDelete: Cascade)
typePieceId String?
typePiece TypePiece? @relation("TypePieceCustomFields", fields: [typePieceId], references: [id], onDelete: Cascade)
// Relations avec les valeurs
customFieldValues CustomFieldValue[]
@@map("custom_fields")
}
model CustomFieldValue {
id String @id @default(cuid())
value String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
customFieldId String
customField CustomField @relation(fields: [customFieldId], references: [id], onDelete: Cascade)
machineId String?
machine Machine? @relation("MachineCustomFieldValues", fields: [machineId], references: [id], onDelete: Cascade)
composantId String?
composant Composant? @relation("ComposantCustomFieldValues", fields: [composantId], references: [id], onDelete: Cascade)
pieceId String?
piece Piece? @relation("PieceCustomFieldValues", fields: [pieceId], references: [id], onDelete: Cascade)
@@map("custom_field_values")
}