From 4ccd8608140fd0cc156502c905eba4fabdd2c1b9 Mon Sep 17 00:00:00 2001 From: Mayo Date: Thu, 30 Jul 2026 11:48:00 +0200 Subject: [PATCH] Database-Layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Das Datanbankschema wurde an das alte angepasst. !!Hinzugefügt wurde 'User'!! --- lib/db/managing.ts | 13 +---- .../migration.sql | 54 +++++++++++++++++++ prisma/schema.prisma | 29 ++++++++-- 3 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 prisma/migrations/20260730094544_schema_update/migration.sql diff --git a/lib/db/managing.ts b/lib/db/managing.ts index f3c69b1..abd7db4 100644 --- a/lib/db/managing.ts +++ b/lib/db/managing.ts @@ -1,14 +1,5 @@ -import Database from "better-sqlite3"; -import path from "path"; +import { prisma } from "@/lib/db/prisma"; -const dbPath = path.join(process.cwd(), "songs.db"); - -// Verhindert, dass Next.js bei jedem Reload die DB neu öffnet -const globalForDb = globalThis as unknown as { db: Database.Database | undefined }; - -export const db = globalForDb.db ?? new Database(dbPath); - -if (process.env.NODE_ENV !== "production") globalForDb.db = db; db.exec(` CREATE TABLE IF NOT EXISTS songs ( @@ -185,4 +176,4 @@ export async function addSongbook(name: string, language: string) { } catch (error) { return null; } -} \ No newline at end of file +} diff --git a/prisma/migrations/20260730094544_schema_update/migration.sql b/prisma/migrations/20260730094544_schema_update/migration.sql new file mode 100644 index 0000000..8657730 --- /dev/null +++ b/prisma/migrations/20260730094544_schema_update/migration.sql @@ -0,0 +1,54 @@ +/* + Warnings: + + - You are about to drop the column `email` on the `User` table. All the data in the column will be lost. + - You are about to drop the column `firstname` on the `User` table. All the data in the column will be lost. + - You are about to drop the column `lastname` on the `User` table. All the data in the column will be lost. + +*/ +-- CreateTable +CREATE TABLE "Song" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "title" TEXT NOT NULL +); + +-- CreateTable +CREATE TABLE "SongBook" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "title" TEXT NOT NULL, + "language" TEXT NOT NULL +); + +-- CreateTable +CREATE TABLE "SongBookSongs" ( + "SongBookId" INTEGER NOT NULL, + "SongId" INTEGER NOT NULL, + "songNumber" INTEGER NOT NULL, + + PRIMARY KEY ("SongBookId", "SongId"), + CONSTRAINT "SongBookSongs_SongId_fkey" FOREIGN KEY ("SongId") REFERENCES "Song" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "SongBookSongs_SongBookId_fkey" FOREIGN KEY ("SongBookId") REFERENCES "SongBook" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_User" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "username" TEXT NOT NULL, + "password" TEXT NOT NULL, + "role" TEXT NOT NULL DEFAULT 'user', + "name" TEXT +); +INSERT INTO "new_User" ("id", "password", "role", "username") SELECT "id", "password", "role", "username" FROM "User"; +DROP TABLE "User"; +ALTER TABLE "new_User" RENAME TO "User"; +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; + +-- CreateIndex +CREATE UNIQUE INDEX "Song_title_key" ON "Song"("title"); + +-- CreateIndex +CREATE UNIQUE INDEX "SongBook_title_key" ON "SongBook"("title"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3d2f095..18da547 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,11 +14,34 @@ datasource db { model User { id Int @id @default(autoincrement()) - email String @unique username String @unique password String role String @default("user") - firstname String - lastname String? + name String? } +model Song { + id Int @id @default(autoincrement()) + title String @unique + + songBookSongs SongBookSongs[] +} + +model SongBook { + id Int @id @default(autoincrement()) + title String @unique + language String + + songBookSongs SongBookSongs[] +} + +model SongBookSongs { + SongBookId Int + SongId Int + songNumber Int + + song Song @relation(fields: [SongId], references: [id], onDelete: Cascade) + book SongBook @relation(fields: [SongBookId], references: [id], onDelete: Cascade) + + @@id([SongBookId, SongId]) +}