Make database connection fully lazy to avoid build-time initialization

This commit is contained in:
bitc0de committed 2025-12-17 15:42:04 +01:00
1 parent 3169fa815e
commit e86d4d7d60
8 files changed
+18 -10

No files matched your search

+3 -1
View File
@@ -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) {
@@ -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')
+2 -1
View File
@@ -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) {
+3 -1
View File
@@ -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)
+2 -1
View File
@@ -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)
+3 -1
View File
@@ -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) {
+2 -1
View File
@@ -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) {
+1 -3
View File
@@ -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()