Make database initialization lazy to avoid build errors

This commit is contained in:
bitc0de committed 2025-12-17 15:37:15 +01:00
1 parent ae2a6a3b9e
commit 3169fa815e
1 file changed
+53 -37
+53 -37
View File
@@ -1,46 +1,62 @@
import Database from 'better-sqlite3'
import { join } from 'path'
import { mkdirSync, existsSync } from 'fs'
const dbPath = join(process.cwd(), 'data', 'bitify.db')
const db = new Database(dbPath)
let db: Database.Database | null = null
// Enable foreign keys
db.pragma('foreign_keys = ON')
function getDb() {
if (db) return db
// Create tables
db.exec(`
CREATE TABLE IF NOT EXISTS songs (
id TEXT PRIMARY KEY,
youtubeId TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
channelName TEXT NOT NULL,
thumbnail TEXT NOT NULL,
duration INTEGER NOT NULL,
isInLibrary INTEGER DEFAULT 1,
createdAt INTEGER DEFAULT (unixepoch())
);
const dataDir = join(process.cwd(), 'data')
// Create data directory if it doesn't exist
if (!existsSync(dataDir)) {
mkdirSync(dataDir, { recursive: true })
}
CREATE TABLE IF NOT EXISTS playlists (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
createdAt INTEGER DEFAULT (unixepoch())
);
const dbPath = join(dataDir, 'bitify.db')
db = new Database(dbPath)
CREATE TABLE IF NOT EXISTS playlistSongs (
id TEXT PRIMARY KEY,
playlistId TEXT NOT NULL,
songId TEXT NOT NULL,
position INTEGER NOT NULL,
addedAt INTEGER DEFAULT (unixepoch()),
FOREIGN KEY (playlistId) REFERENCES playlists(id) ON DELETE CASCADE,
FOREIGN KEY (songId) REFERENCES songs(id) ON DELETE CASCADE,
UNIQUE(playlistId, songId)
);
// Enable foreign keys
db.pragma('foreign_keys = ON')
CREATE INDEX IF NOT EXISTS idx_songs_youtubeId ON songs(youtubeId);
CREATE INDEX IF NOT EXISTS idx_songs_isInLibrary ON songs(isInLibrary);
CREATE INDEX IF NOT EXISTS idx_playlistSongs_playlistId ON playlistSongs(playlistId);
CREATE INDEX IF NOT EXISTS idx_playlistSongs_songId ON playlistSongs(songId);
`)
// Create tables
db.exec(`
CREATE TABLE IF NOT EXISTS songs (
id TEXT PRIMARY KEY,
youtubeId TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
channelName TEXT NOT NULL,
thumbnail TEXT NOT NULL,
duration INTEGER NOT NULL,
isInLibrary INTEGER DEFAULT 1,
createdAt INTEGER DEFAULT (unixepoch())
);
export default db
CREATE TABLE IF NOT EXISTS playlists (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
createdAt INTEGER DEFAULT (unixepoch())
);
CREATE TABLE IF NOT EXISTS playlistSongs (
id TEXT PRIMARY KEY,
playlistId TEXT NOT NULL,
songId TEXT NOT NULL,
position INTEGER NOT NULL,
addedAt INTEGER DEFAULT (unixepoch()),
FOREIGN KEY (playlistId) REFERENCES playlists(id) ON DELETE CASCADE,
FOREIGN KEY (songId) REFERENCES songs(id) ON DELETE CASCADE,
UNIQUE(playlistId, songId)
);
CREATE INDEX IF NOT EXISTS idx_songs_youtubeId ON songs(youtubeId);
CREATE INDEX IF NOT EXISTS idx_songs_isInLibrary ON songs(isInLibrary);
CREATE INDEX IF NOT EXISTS idx_playlistSongs_playlistId ON playlistSongs(playlistId);
CREATE INDEX IF NOT EXISTS idx_playlistSongs_songId ON playlistSongs(songId);
`)
return db
}
export default getDb()