feat: implement Prisma client and standalone database initialization script

This commit is contained in:
Michael committed 2026-04-10 11:09:52 +02:00
1 parent c3d141402f
commit eff3d0402d
5 files changed
+70 -74

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env node
/**
* Database initialization script.
* Run BEFORE the Next.js server starts (via entrypoint.sh).
* Uses better-sqlite3 directly — no Prisma CLI dependency.
*/
import Database from 'better-sqlite3';
import { existsSync, mkdirSync } from 'fs';
import { dirname } from 'path';
const DB_URL = process.env.DATABASE_URL || 'file:/app/prisma/dev.db';
const DB_PATH = DB_URL.replace(/^file:/, '');
// Ensure the directory exists
const dir = dirname(DB_PATH);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
console.log(`[init-db] Created directory: ${dir}`);
}
console.log(`[init-db] Initializing database at: ${DB_PATH}`);
const db = new Database(DB_PATH);
// Config table
db.exec(`
CREATE TABLE IF NOT EXISTS "Config" (
"id" TEXT NOT NULL PRIMARY KEY,
"value" TEXT NOT NULL
);
`);
// Button table
db.exec(`
CREATE TABLE IF NOT EXISTS "Button" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"url" TEXT NOT NULL,
"order" INTEGER NOT NULL DEFAULT 0
);
`);
// Migration v2: icon column
try {
db.exec(`ALTER TABLE "Button" ADD COLUMN "icon" TEXT;`);
console.log('[init-db] Migration: Button.icon column added.');
} catch {
// Column already exists — safe to ignore
}
db.close();
console.log('[init-db] Database ready.');