From 0dee8b4678112fc212d1a7463ec0e0d9a1447244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Andr=C3=A9s=20Trujillo?= Date: Wed, 22 Nov 2023 18:00:16 +0100 Subject: [PATCH] feat: add borrow prisma schema --- .../20231122165948_borrow/migration.sql | 18 +++++++ prisma/schema.prisma | 50 ++++++++++++------- 2 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 prisma/migrations/20231122165948_borrow/migration.sql diff --git a/prisma/migrations/20231122165948_borrow/migration.sql b/prisma/migrations/20231122165948_borrow/migration.sql new file mode 100644 index 0000000..bfe7ee3 --- /dev/null +++ b/prisma/migrations/20231122165948_borrow/migration.sql @@ -0,0 +1,18 @@ +-- CreateTable +CREATE TABLE "Borrow" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "bookId" TEXT NOT NULL, + "borrowedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "Borrow_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "Borrow_bookId_key" ON "Borrow"("bookId"); + +-- AddForeignKey +ALTER TABLE "Borrow" ADD CONSTRAINT "Borrow_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Borrow" ADD CONSTRAINT "Borrow_bookId_fkey" FOREIGN KEY ("bookId") REFERENCES "Book"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1096274..28f30d5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -9,18 +9,18 @@ generator client { } model Account { - id String @id @default(cuid()) - userId String - type String - provider String - providerAccountId String - refresh_token String? @db.Text - access_token String? @db.Text - expires_at Int? - token_type String? - scope String? - id_token String? @db.Text - session_state String? + id String @id @default(cuid()) + userId String + type String + provider String + providerAccountId String + refresh_token String? @db.Text + access_token String? @db.Text + expires_at Int? + token_type String? + scope String? + id_token String? @db.Text + session_state String? user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@ -41,9 +41,10 @@ model User { email String? @unique emailVerified DateTime? image String? - roles String[] @default(["ROLE_USER"]) + roles String[] @default(["ROLE_USER"]) accounts Account[] sessions Session[] + Borrow Borrow[] } model VerificationToken { @@ -55,8 +56,21 @@ model VerificationToken { } model Book { - id String @id @default(cuid()) - authors String[] - image String - title String -} \ No newline at end of file + id String @id @default(cuid()) + authors String[] + image String + title String + Borrow Borrow? +} + +model Borrow { + id String @id @default(cuid()) + userId String + bookId String + borrowedAt DateTime + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + book Book @relation(fields: [bookId], references: [id], onDelete: Cascade) + + @@unique([bookId]) +}