From d7870a8153eb735115608a3ab8864cbdc55344bf Mon Sep 17 00:00:00 2001 From: Fred Date: Wed, 16 Apr 2025 13:50:38 +0200 Subject: [PATCH] prisma --- .env | 2 +- .gitignore | 2 +- package-lock.json | 23 ++++ package.json | 1 + .../20250416114816_init/migration.sql | 130 ++++++++++++++++++ prisma/migrations/migration_lock.toml | 3 + prisma/schema.prisma | 99 ++++++++++++- src/lib/db.ts | 16 +++ 8 files changed, 273 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20250416114816_init/migration.sql create mode 100644 prisma/migrations/migration_lock.toml create mode 100644 src/lib/db.ts diff --git a/.env b/.env index a8aeb0d..4a239ff 100644 --- a/.env +++ b/.env @@ -4,4 +4,4 @@ # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. # See the documentation for all the connection string options: https://pris.ly/d/connection-strings -DATABASE_URL="mysql://:@:/?schema=public" \ No newline at end of file +DATABASE_URL="mysql://monelia:monelia@localhost:3306/monelia?schema=public" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 8c5b2ee..5ef6a52 100644 --- a/.gitignore +++ b/.gitignore @@ -31,7 +31,7 @@ yarn-error.log* .pnpm-debug.log* # env files (can opt-in for committing if needed) -#.env* +.env* # vercel .vercel diff --git a/package-lock.json b/package-lock.json index 8e97329..d564d96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "monelia-nails", "version": "0.1.0", "dependencies": { + "@prisma/client": "^6.6.0", "next": "15.3.0", "prisma": "^6.6.0", "react": "^19.0.0", @@ -1270,6 +1271,28 @@ "node": ">=12.4.0" } }, + "node_modules/@prisma/client": { + "version": "6.6.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-6.6.0.tgz", + "integrity": "sha512-vfp73YT/BHsWWOAuthKQ/1lBgESSqYqAWZEYyTdGXyFAHpmewwWL2Iz6ErIzkj4aHbuc6/cGSsE6ZY+pBO04Cg==", + "hasInstallScript": true, + "license": "Apache-2.0", + "engines": { + "node": ">=18.18" + }, + "peerDependencies": { + "prisma": "*", + "typescript": ">=5.1.0" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + }, + "typescript": { + "optional": true + } + } + }, "node_modules/@prisma/config": { "version": "6.6.0", "resolved": "https://registry.npmjs.org/@prisma/config/-/config-6.6.0.tgz", diff --git a/package.json b/package.json index ff283e4..a0db5e9 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "lint": "next lint" }, "dependencies": { + "@prisma/client": "^6.6.0", "next": "15.3.0", "prisma": "^6.6.0", "react": "^19.0.0", diff --git a/prisma/migrations/20250416114816_init/migration.sql b/prisma/migrations/20250416114816_init/migration.sql new file mode 100644 index 0000000..a5bb5b1 --- /dev/null +++ b/prisma/migrations/20250416114816_init/migration.sql @@ -0,0 +1,130 @@ +-- CreateTable +CREATE TABLE `Utilisateur` ( + `ut_id` INTEGER NOT NULL AUTO_INCREMENT, + `ut_nom` VARCHAR(191) NULL, + `ut_prenom` VARCHAR(191) NULL, + `ut_mail` VARCHAR(191) NULL, + `ut_type_cliente` INTEGER NULL, + `ut_age` DATETIME(3) NULL, + `ut_nb_rdv` INTEGER NULL, + `ut_nb_rdv_honores` INTEGER NULL, + `ut_prix_moyen` INTEGER NULL, + `ut_pdp` INTEGER NULL, + `ut_panier` INTEGER NULL, + `ut_mdp` INTEGER NULL, + `ut_telephone` INTEGER NULL, + `ut_date_inscription` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `ut_derniere_connexion` DATETIME(3) NULL, + `ut_slug` VARCHAR(191) NULL, + + PRIMARY KEY (`ut_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `Formation` ( + `fo_id` INTEGER NOT NULL AUTO_INCREMENT, + `fo_titre` VARCHAR(191) NULL, + `fo_description` VARCHAR(191) NULL, + `fo_prix` VARCHAR(191) NULL, + `fo_miniature` LONGBLOB NULL, + `fo_teaser` VARCHAR(191) NULL, + `fo_date_publication` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `fo_desactive` BOOLEAN NOT NULL DEFAULT false, + `fo_slug` VARCHAR(191) NULL, + + PRIMARY KEY (`fo_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `Video` ( + `vi_fo_id` INTEGER NOT NULL, + `vi_id` INTEGER NOT NULL AUTO_INCREMENT, + `vi_titre` VARCHAR(191) NULL, + `vi_description` VARCHAR(191) NULL, + `vi_etape` INTEGER NULL, + `vi_url` VARCHAR(191) NULL, + `vi_slug` VARCHAR(191) NULL, + + PRIMARY KEY (`vi_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `Commande` ( + `co_id` INTEGER NOT NULL AUTO_INCREMENT, + `co_utilisateur` INTEGER NOT NULL, + `co_date_creation` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `co_date_update` DATETIME(3) NULL, + `co_slug` VARCHAR(191) NULL, + `co_status` VARCHAR(191) NOT NULL DEFAULT 'IN PROGRESS', + + PRIMARY KEY (`co_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `CommandeFormation` ( + `cof_id` INTEGER NOT NULL AUTO_INCREMENT, + `cof_formation` INTEGER NOT NULL, + `cof_slug` VARCHAR(191) NULL, + + PRIMARY KEY (`cof_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `Achat` ( + `ac_id` INTEGER NOT NULL AUTO_INCREMENT, + `ac_utilisateur` INTEGER NOT NULL, + `ac_formation` INTEGER NOT NULL, + `ac_type` VARCHAR(191) NULL, + `ac_slug` VARCHAR(191) NULL, + `ac_date_achat` DATETIME(3) NULL, + `ac_date_fin` DATETIME(3) NULL, + + PRIMARY KEY (`ac_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `Tag` ( + `ta_id` INTEGER NOT NULL AUTO_INCREMENT, + `ta_titre` VARCHAR(191) NULL, + `ta_slug` VARCHAR(191) NULL, + + PRIMARY KEY (`ta_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `TagVideo` ( + `tv_tag_id` INTEGER NOT NULL, + `tv_video_id` INTEGER NOT NULL, + + PRIMARY KEY (`tv_tag_id`, `tv_video_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `Session` ( + `ut_id` INTEGER NOT NULL, + `createAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + `expireAt` DATETIME(3) NULL, + + PRIMARY KEY (`ut_id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `Video` ADD CONSTRAINT `Video_vi_fo_id_fkey` FOREIGN KEY (`vi_fo_id`) REFERENCES `Formation`(`fo_id`) ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `Commande` ADD CONSTRAINT `Commande_co_utilisateur_fkey` FOREIGN KEY (`co_utilisateur`) REFERENCES `Utilisateur`(`ut_id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `CommandeFormation` ADD CONSTRAINT `CommandeFormation_cof_formation_fkey` FOREIGN KEY (`cof_formation`) REFERENCES `Formation`(`fo_id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `Achat` ADD CONSTRAINT `Achat_ac_utilisateur_fkey` FOREIGN KEY (`ac_utilisateur`) REFERENCES `Utilisateur`(`ut_id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `Achat` ADD CONSTRAINT `Achat_ac_formation_fkey` FOREIGN KEY (`ac_formation`) REFERENCES `Formation`(`fo_id`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `TagVideo` ADD CONSTRAINT `TagVideo_tv_tag_id_fkey` FOREIGN KEY (`tv_tag_id`) REFERENCES `Tag`(`ta_id`) ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `TagVideo` ADD CONSTRAINT `TagVideo_tv_video_id_fkey` FOREIGN KEY (`tv_video_id`) REFERENCES `Video`(`vi_id`) ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..592fc0b --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (e.g., Git) +provider = "mysql" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7e34e53..b4fcf51 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -6,10 +6,107 @@ generator client { provider = "prisma-client-js" - output = "../src/generated/prisma" } datasource db { provider = "mysql" url = env("DATABASE_URL") } + +model Utilisateur { + ut_id Int @id @default(autoincrement()) + ut_nom String? + ut_prenom String? + ut_mail String? + ut_type_cliente Int? + ut_age DateTime? + ut_nb_rdv Int? + ut_nb_rdv_honores Int? + ut_prix_moyen Int? + ut_pdp Int? + ut_panier Int? + ut_mdp Int? + ut_telephone Int? + ut_date_inscription DateTime @default(now()) + ut_derniere_connexion DateTime? + ut_slug String? + commande Commande[] + achat Achat[] +} + +model Formation { + fo_id Int @id @default(autoincrement()) + fo_titre String? + fo_description String? + fo_prix String? + fo_miniature Bytes? + fo_teaser String? + fo_date_publication DateTime @default(now()) + fo_desactive Boolean @default(false) + fo_slug String? + fo_video Video[] + commandeFormation CommandeFormation[] + achat Achat[] +} + +model Video{ + formation Formation @relation(fields: [vi_fo_id],references: [fo_id], onDelete: Cascade) + vi_fo_id Int + vi_id Int @id @default(autoincrement()) + vi_titre String? + vi_description String? + vi_etape Int? + vi_url String? + vi_slug String? + vi_tv TagVideo[] +} + +model Commande{ + co_id Int @id @default(autoincrement()) + co_utilisateur Int + utilisateur Utilisateur @relation(fields: [co_utilisateur],references: [ut_id]) + co_date_creation DateTime @default(now()) + co_date_update DateTime? + co_slug String? + co_status String @default("IN PROGRESS") +} + +model CommandeFormation{ + cof_id Int @id @default(autoincrement()) + cof_formation Int + cof_slug String? + formation Formation @relation(fields: [cof_formation],references: [fo_id]) +} + +model Achat{ + ac_id Int @id @default(autoincrement()) + ac_utilisateur Int + utilisateur Utilisateur @relation(fields: [ac_utilisateur],references: [ut_id]) + ac_formation Int + formation Formation @relation(fields: [ac_formation],references: [fo_id]) + ac_type String? + ac_slug String ? + ac_date_achat DateTime? + ac_date_fin DateTime? +} + +model Tag { + ta_id Int @id @default(autoincrement()) + ta_titre String? + ta_slug String? + vi_tv TagVideo[] +} + +model TagVideo{ + @@id([tv_tag_id, tv_video_id]) + tv_tag_id Int + tag Tag @relation(fields: [tv_tag_id],references: [ta_id], onDelete: Cascade) + tv_video_id Int + video Video @relation(fields: [tv_video_id],references: [vi_id], onDelete: Cascade) +} + +model Session{ + ut_id Int @id + createAt DateTime @default(now()) + expireAt DateTime? +} diff --git a/src/lib/db.ts b/src/lib/db.ts new file mode 100644 index 0000000..cf6aa93 --- /dev/null +++ b/src/lib/db.ts @@ -0,0 +1,16 @@ +import { PrismaClient } from "@prisma/client" + +const prismaClientSingleton = () => { + return new PrismaClient() +} + +declare const globalThis:{ + prismaGlobal : ReturnType +} & typeof global + +const prisma = globalThis.prismaGlobal ?? prismaClientSingleton() + +export default prisma + + +if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma; \ No newline at end of file