Some changes in manage.ts --> 3 user.ts, song.ts, songbook.ts

Aufspaltung auf 3 Dateien, aber man muss noch weiter arbeiten

(Task: Von better-sqlite3 --> ORM)
This commit is contained in:
rudi committed 2026-07-30 14:13:02 +02:00
1 parent d395308c6d
commit d03ff9d26d
7 files changed
+242 -50

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
# This is an Example for the Environment-Variables
# Create the admin user
ADMIN_USERNAME="admin"
ADMIN_PASSWORD="admin"
# Database location
DATABASE_URL="file:./songs.sqlite"
# Sectret for JWT cookies
JWT_SECRET=3a1e8c4b7d0f2a5e9c6b3d1f4a8e2c7b0d5f3a3f9afd1b4d8f0a7e6c5b259f
+5 -5
View File
@@ -7,12 +7,12 @@ export async function initDB() {
if (count === 0) { if (count === 0) {
await prisma.user.create({ await prisma.user.create({
data: { data: {
username: process.env.ADMIN_USERNAME!, username: process.env.ADMIN_USERNAME!,
password: await argon2.hash(process.env.ADMIN_PASSWORD!), password: await argon2.hash(process.env.ADMIN_PASSWORD!),
role: 'admin', role: 'admin',
name: 'Admin' name: 'Admin'
} }
}) })
console.log('Admin-User angelegt.') console.log('Created admin user')
} }
} }
+2
View File
@@ -0,0 +1,2 @@
import { prisma } from "@/lib/db/prisma";
+109 -35
View File
@@ -1,41 +1,115 @@
import { prisma } from "@/lib/db/prisma"; import { prisma } from "@/lib/db/prisma";
export async function getSongs() {
try {
const songs = db.prepare(` /**
SELECT * getSongBooks
ss.songbook_id, * Gibt alle Songbücher aus, mit allen Liedern
ss.song_id, * [{"id":number, "title": string, "songs":[{"id":number, "title": string},...]},...]
ss.song_number, */
s.title export async function getSongBooks() {
FROM songbook_songs ss try{
JOIN songs s ON ss.song_id = s.id const songbooks = await prisma.songBook.findMany({
ORDER BY ss.songbook_id, ss.song_number ASC include: {
`).all(); songs: {
return songs; select: {
} catch (error) { song_number: true,
return null song: true,
},
orderBy: {
song_number: "asc",
},
},
},
});
return { success: true, songBooks: songbooks };
} catch(error){
return { success: false, error: error }
} }
} }
export async function getSong(songbookId: number, songId: string) { /**
try { * getSongBooks
const song = db.prepare(` * Gibt ein Songbuch aus, mit allen Liedern
SELECT * {"id":number, "title": string, "songs":[{"id":number, "title": string},...]}
sb.name AS songbook_name, */
ss.song_number AS number, export async function getSongBook(songbookid: number) {
s.title AS song_title try{
FROM songbook_songs ss const songbook = await prisma.songBook.findMany({
JOIN songbooks sb ON ss.songbook_id = sb.id where: {
JOIN songs s ON ss.song_id = s.id id: songbookid,
WHERE ss.songbook_id = ? AND ss.song_id = ? },
`).get(songbookId, songId) as { songbook_name: string; number: string; song_title: string } | undefined; include: {
songs: {
select: {
songNumber: true,
song: true,
},
orderBy: {
songNumber: "asc",
},
},
},
});
return { success: true, songBook: songbook };
} catch(error){
return { success: false, error: error }
}
}
return song || null;
/**
* GetSongs
* Gibt alle Songs zurück.
* [{"id": int, "title": string}]
*/
export async function getSongs() {
try {
const songs = prisma.song.findMany({
select: {
id: true,
title: true,
},
orderBy: [
{ id: "asc" },
],
});
return songs;
} catch (error) {
return { success: false, error: error };
}
}
export async function getSong(songBookId: number, songId: string) {
try {
const song = await prisma.songBookSongs.findFirst({
where: {
songBookId: songBookId,
songId: songId,
},
select: {
songNumber: true,
book: {
select: {
title: true,
},
},
song: {
select: {
title: true,
},
},
},
});
return song ? {songbook_name: song.book.name, number: song.songNumber, song_title: song.song.title,}:null;
} catch (error) { } catch (error) {
console.error("Fehler in getSong:", error); console.error("Fehler in getSong:", error);
return null; return { success: false, error: error };
} }
} }
@@ -61,7 +135,7 @@ export async function getSongInfos(songId: string) {
return songRecords || null; return songRecords || null;
} catch (error) { } catch (error) {
console.error("Fehler in getSong:", error); console.error("Fehler in getSong:", error);
return null; return { success: false, error: error };
} }
} }
@@ -87,7 +161,7 @@ export async function addSongToSongbook(title:string, songbookId:number, number:
return { success: true } return { success: true }
} catch (error) { } catch (error) {
console.error("Error in addSongToSongbook:", error); console.error("Error in addSongToSongbook:", error);
return { success: false }; return { success: false, error: error };
} }
} }
@@ -113,7 +187,7 @@ export async function attachSongToSongbook(songId: number, songbookId: number, n
return { success: true }; return { success: true };
} catch (error) { } catch (error) {
console.error("Error in attachSongToSongbook:", error); console.error("Error in attachSongToSongbook:", error);
return { success: false }; return { success: false, error: error };
} }
} }
@@ -127,7 +201,7 @@ export async function removeSongfromSongbook(songId: number, songbookId: number)
return { success: info.changes > 0 }; return { success: info.changes > 0 };
} catch (error) { } catch (error) {
console.error("Error in removeSongfromSongbook:", error); console.error("Error in removeSongfromSongbook:", error);
return { success: false }; return { success: false, error: error };
} }
} }
@@ -136,7 +210,7 @@ export async function getSongbooks() {
const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all(); const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all();
return songbooks; return songbooks;
} catch (error) { } catch (error) {
return null; return { success: false, error: error };
} }
} }
@@ -154,6 +228,6 @@ export async function addSongbook(name: string, language: string) {
} }
return newBook; return newBook;
} catch (error) { } catch (error) {
return null; return { success: false, error: error };
} }
} }
+80
View File
@@ -0,0 +1,80 @@
import { prisma } from "@/lib/db/prisma";
/*
* GetUsers
* Gibt alle user zurück
* [{"id":number, "username": string, "role": string},...]
*/
export async function getUsers() {
try{
const users = await prisma.user.findMany({
select: {
id: true,
username: true,
role: true,
}
});
if (!users) {
return { success: false, error: "Benutzer nicht gefunden." };
}
return { success: true, users };
} catch(error) {
console.error("Error in getUsers", error);
return { success: false, error: "Error while loading Users" };
}
}
/*
* GetUserById
* Gibt einen Benutzer anhand der ID zurück
* {"id":number, "username": string, "role": string}
*/
export async function getUserById(id: number) {
try{
const user = await prisma.user.findUnique({
where: { id },
select: {
id: true,
username: true,
role: true,
}
});
if (!user) {
return { success: false, error: "Benutzer nicht gefunden." };
}
return { success: true, user };
} catch(error) {
console.error("Error in getUserById", error);
return { success: false, error: "Error while loading User" };
}
}
/*
* GetUserByUsername
* Gibt einen Benutzer anhand des Usernames zurück
* {"id":number, "username": string, "role": string}
*/
export async function getUserByUsername(username: string) {
try{
const user = await prisma.user.findUnique({
where: { username },
select: {
id: true,
username: true,
role: true,
}
});
if (!user) {
return { success: false, error: "Benutzer nicht gefunden." };
}
return { success: true, user };
} catch(error) {
console.error("Error in getUserByUsername", error);
return { success: false, error: "Error while loading User" };
}
}
@@ -0,0 +1,27 @@
/*
Warnings:
- The primary key for the `SongBookSongs` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `SongBookId` on the `SongBookSongs` table. All the data in the column will be lost.
- You are about to drop the column `SongId` on the `SongBookSongs` table. All the data in the column will be lost.
- Added the required column `songBookId` to the `SongBookSongs` table without a default value. This is not possible if the table is not empty.
- Added the required column `songId` to the `SongBookSongs` table without a default value. This is not possible if the table is not empty.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_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
);
INSERT INTO "new_SongBookSongs" ("songNumber") SELECT "songNumber" FROM "SongBookSongs";
DROP TABLE "SongBookSongs";
ALTER TABLE "new_SongBookSongs" RENAME TO "SongBookSongs";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
+7 -10
View File
@@ -1,7 +1,4 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// Get a free hosted Postgres database in seconds: `npx create-db`
generator client { generator client {
provider = "prisma-client" provider = "prisma-client"
@@ -24,7 +21,7 @@ model Song {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
title String @unique title String @unique
songBookSongs SongBookSongs[] songBooks SongBookSongs[]
} }
model SongBook { model SongBook {
@@ -32,16 +29,16 @@ model SongBook {
title String @unique title String @unique
language String language String
songBookSongs SongBookSongs[] songs SongBookSongs[]
} }
model SongBookSongs { model SongBookSongs {
SongBookId Int songBookId Int
SongId Int songId Int
songNumber Int songNumber Int
song Song @relation(fields: [SongId], references: [id], onDelete: Cascade) song Song @relation(fields: [songId], references: [id], onDelete: Cascade)
book SongBook @relation(fields: [SongBookId], references: [id], onDelete: Cascade) book SongBook @relation(fields: [songBookId], references: [id], onDelete: Cascade)
@@id([SongBookId, SongId]) @@id([songBookId, songId])
} }