From e86d4d7d6097af46aed828d14bd4f650a0250c87 Mon Sep 17 00:00:00 2001 From: bitc0de Date: Wed, 17 Dec 2025 15:42:04 +0100 Subject: [PATCH] Make database connection fully lazy to avoid build-time initialization --- app/api/playlists/[id]/route.ts | 4 +++- app/api/playlists/[id]/songs/[songId]/route.ts | 3 ++- app/api/playlists/[id]/songs/route.ts | 3 ++- app/api/playlists/route.ts | 4 +++- app/api/songs/[id]/route.ts | 3 ++- app/api/songs/route.ts | 4 +++- app/api/stream/[id]/route.ts | 3 ++- lib/db.ts | 4 +--- 8 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/api/playlists/[id]/route.ts b/app/api/playlists/[id]/route.ts index 8503688..553c597 100644 --- a/app/api/playlists/[id]/route.ts +++ b/app/api/playlists/[id]/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' export async function DELETE( request: NextRequest, @@ -8,6 +8,7 @@ export async function DELETE( try { const { id } = await params + const db = getDb() db.prepare('DELETE FROM playlists WHERE id = ?').run(id) console.log('[API] Playlist deleted:', id) @@ -28,6 +29,7 @@ export async function GET( try { const { id } = await params + const db = getDb() const playlist = db.prepare('SELECT * FROM playlists WHERE id = ?').get(id) as any if (!playlist) { diff --git a/app/api/playlists/[id]/songs/[songId]/route.ts b/app/api/playlists/[id]/songs/[songId]/route.ts index 6437ce9..70cfc72 100644 --- a/app/api/playlists/[id]/songs/[songId]/route.ts +++ b/app/api/playlists/[id]/songs/[songId]/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' export async function DELETE( request: NextRequest, @@ -8,6 +8,7 @@ export async function DELETE( try { const { id, songId } = await params + const db = getDb() db.prepare('DELETE FROM playlistSongs WHERE playlistId = ? AND songId = ?').run(id, songId) console.log('[API] Song removed from playlist') diff --git a/app/api/playlists/[id]/songs/route.ts b/app/api/playlists/[id]/songs/route.ts index 7dceb17..afe3d4b 100644 --- a/app/api/playlists/[id]/songs/route.ts +++ b/app/api/playlists/[id]/songs/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' import { randomBytes } from 'crypto' export async function POST( @@ -18,6 +18,7 @@ export async function POST( } // Check if already in playlist + const db = getDb() const existing = db.prepare('SELECT * FROM playlistSongs WHERE playlistId = ? AND songId = ?').get(id, songId) if (existing) { diff --git a/app/api/playlists/route.ts b/app/api/playlists/route.ts index f987b4f..1fcc559 100644 --- a/app/api/playlists/route.ts +++ b/app/api/playlists/route.ts @@ -1,9 +1,10 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' import { randomBytes } from 'crypto' export async function GET() { try { + const db = getDb() const playlists = db.prepare('SELECT * FROM playlists ORDER BY createdAt DESC').all() as any[] // Get songs for each playlist @@ -42,6 +43,7 @@ export async function POST(request: NextRequest) { ) } + const db = getDb() const playlistId = randomBytes(16).toString('hex') db.prepare('INSERT INTO playlists (id, name) VALUES (?, ?)').run(playlistId, name) diff --git a/app/api/songs/[id]/route.ts b/app/api/songs/[id]/route.ts index 7680bfe..adad4f2 100644 --- a/app/api/songs/[id]/route.ts +++ b/app/api/songs/[id]/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' export async function DELETE( request: NextRequest, @@ -10,6 +10,7 @@ export async function DELETE( // Instead of deleting, mark as not in library // This keeps the song in playlists but removes it from main library + const db = getDb() db.prepare('UPDATE songs SET isInLibrary = 0 WHERE id = ?').run(id) console.log('[API] Song removed from library:', id) diff --git a/app/api/songs/route.ts b/app/api/songs/route.ts index 5339830..c8d63ee 100644 --- a/app/api/songs/route.ts +++ b/app/api/songs/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' import { getYoutubeMetadata } from '@/lib/ytdlp' import { randomBytes } from 'crypto' @@ -21,6 +21,7 @@ export async function POST(request: NextRequest) { console.log('[API] Got metadata:', metadata) // Check if song already exists + const db = getDb() const existingSong = db.prepare('SELECT * FROM songs WHERE youtubeId = ?').get(metadata.id) as any if (existingSong) { @@ -59,6 +60,7 @@ export async function POST(request: NextRequest) { export async function GET() { try { + const db = getDb() const songs = db.prepare('SELECT * FROM songs WHERE isInLibrary = 1 ORDER BY createdAt DESC').all() return NextResponse.json(songs) } catch (error) { diff --git a/app/api/stream/[id]/route.ts b/app/api/stream/[id]/route.ts index c431e8e..ec83036 100644 --- a/app/api/stream/[id]/route.ts +++ b/app/api/stream/[id]/route.ts @@ -1,5 +1,5 @@ import { NextRequest, NextResponse } from 'next/server' -import db from '@/lib/db' +import { getDb } from '@/lib/db' import { getStreamUrl } from '@/lib/ytdlp' import { streamCache } from '@/lib/cache' @@ -11,6 +11,7 @@ export async function GET( const { id } = await params // Get song from database + const db = getDb() const song = db.prepare('SELECT * FROM songs WHERE id = ?').get(id) as any if (!song) { diff --git a/lib/db.ts b/lib/db.ts index a28b0bf..bf3230f 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -4,7 +4,7 @@ import { mkdirSync, existsSync } from 'fs' let db: Database.Database | null = null -function getDb() { +export function getDb() { if (db) return db const dataDir = join(process.cwd(), 'data') @@ -58,5 +58,3 @@ function getDb() { return db } - -export default getDb()