Files
psalmtafel-webservice/lib/db/managing.ts
T

148 lines
4.5 KiB
TypeScript

import Database from "better-sqlite3";
import path from "path";
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 (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS songbooks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
language TEXT
);
CREATE TABLE IF NOT EXISTS songbook_songs (
songbook_id INTEGER,
song_id INTEGER,
song_number TEXT,
PRIMARY KEY (songbook_id, song_id),
FOREIGN KEY (songbook_id) REFERENCES songbooks(id) ON DELETE CASCADE,
FOREIGN KEY (song_id) REFERENCES songs(id) ON DELETE CASCADE
);
`)
export async function getSongs() {
try {
const songs = db.prepare(`
SELECT
ss.songbook_id,
ss.song_id,
ss.song_number,
s.title
FROM songbook_songs ss
JOIN songs s ON ss.song_id = s.id
ORDER BY ss.songbook_id, ss.song_number ASC
`).all();
return songs;
} catch (error) {
return null
}
}
export async function getSong(songbookId: number, songId: string) {
try {
const song = db.prepare(`
SELECT
sb.name AS songbook_name,
ss.song_number AS number,
s.title AS song_title
FROM songbook_songs ss
JOIN songbooks sb ON ss.songbook_id = sb.id
JOIN songs s ON ss.song_id = s.id
WHERE ss.songbook_id = ? AND ss.song_id = ?
`).get(songbookId, songId) as { songbook_name: string; number: string; song_title: string } | undefined;
return song || null;
} catch (error) {
console.error("Fehler in getSong:", error);
return null;
}
}
export async function getSongInfos(songId: string) {
try {
const songRecords = db.prepare(`
SELECT
ss.songbook_id,
sb.name AS songbook_name,
ss.song_number,
s.title
FROM songbook_songs ss
JOIN songbooks sb ON ss.songbook_id = sb.id
JOIN songs s ON ss.song_id = s.id
WHERE s.id = ?
`).all(songId) as {
songbook_id: number;
songbook_name: string;
song_number: string;
title: string;
}[] | undefined
return songRecords || null;
} catch (error) {
console.error("Fehler in getSong:", error);
return null;
}
}
export async function addSongToSongbook(title:string, songbookId:number, number:string) {
try {
const runTransaction = db.transaction(() => {
let song = db.prepare("SELECT id FROM songs WHERE title = ?").get(title) as { id: number } | undefined
let songId: number | bigint;
if (!song) {
const insertSong = db.prepare("INSERT INTO songs (title) VALUES (?)").run(title);
songId = insertSong.lastInsertRowid;
} else {
songId = song.id;
}
db.prepare(`
INSERT OR REPLACE INTO songbook_songs (songbook_id, song_id, song_number)
VALUES (?, ?, ?)
`).run(songbookId, songId, number);
})
runTransaction();
return { success: true }
} catch (error) {
console.error("Error in addSongToSongbook:", error);
return { success: false };
}
}
export async function getSongbooks() {
try {
const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all();
return songbooks;
} catch (error) {
return null;
}
}
export async function addSongbook(name:String, language:String) {
try {
const info = db.prepare(`
INSERT INTO songbooks (name, language)
VALUES (?, ?)
`).run(name, language || null);
const newBook = {
id: info.lastInsertRowid,
name,
language
}
return newBook;
} catch (error) {
return null;
}
}