mirror of
https://github.com/R0m1k3/Menu.git
synced 2026-10-11 17:28:28 +02:00
feat: implement Prisma client and standalone database initialization script
This commit is contained in:
1 parent
c3d141402f
commit
eff3d0402d
5 files changed
+70
-74
No files matched your search
@@ -1,26 +1,17 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
|
||||
import { initializeDatabase } from './initDb';
|
||||
|
||||
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
||||
|
||||
/**
|
||||
* Creates the Prisma client.
|
||||
* Schema initialization is handled by scripts/init-db.mjs (run at startup).
|
||||
*/
|
||||
const createPrismaClient = () => {
|
||||
try {
|
||||
const dbUrl = process.env.DATABASE_URL || 'file:/app/prisma/dev.db';
|
||||
console.log(`[DB] Connecting to: ${dbUrl}`);
|
||||
|
||||
// Ensure schema exists before creating the Prisma client
|
||||
initializeDatabase();
|
||||
|
||||
const adapter = new PrismaBetterSqlite3({ url: dbUrl });
|
||||
const client = new PrismaClient({ adapter });
|
||||
|
||||
console.log('[DB] Prisma client ready.');
|
||||
return client;
|
||||
} catch (error) {
|
||||
console.error('[DB] Critical initialization failure:', error);
|
||||
throw error;
|
||||
}
|
||||
const dbUrl = process.env.DATABASE_URL || 'file:/app/prisma/dev.db';
|
||||
console.log(`[DB] Connecting to: ${dbUrl}`);
|
||||
const adapter = new PrismaBetterSqlite3({ url: dbUrl });
|
||||
return new PrismaClient({ adapter });
|
||||
};
|
||||
|
||||
export const prisma = globalForPrisma.prisma || createPrismaClient();
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* Initializes the SQLite database schema using raw SQL.
|
||||
* Safe to run multiple times — uses IF NOT EXISTS and handles migrations.
|
||||
*/
|
||||
export function initializeDatabase(): void {
|
||||
const dbUrl = process.env.DATABASE_URL || 'file:/app/prisma/dev.db';
|
||||
const filePath = dbUrl.replace(/^file:/, '');
|
||||
const resolvedPath = path.isAbsolute(filePath)
|
||||
? filePath
|
||||
: path.join(process.cwd(), filePath);
|
||||
|
||||
console.log(`[DB Init] Ensuring schema at: ${resolvedPath}`);
|
||||
|
||||
try {
|
||||
const db = new Database(resolvedPath);
|
||||
|
||||
// Config table
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS "Config" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"value" TEXT NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
// Button table (base)
|
||||
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: add icon column if missing (added in v2)
|
||||
try {
|
||||
db.exec(`ALTER TABLE "Button" ADD COLUMN "icon" TEXT;`);
|
||||
console.log('[DB Init] Migration applied: Button.icon column added.');
|
||||
} catch {
|
||||
// Column already exists — no action needed
|
||||
}
|
||||
|
||||
db.close();
|
||||
console.log('[DB Init] Schema ready.');
|
||||
} catch (err) {
|
||||
console.error('[DB Init Error] Failed to initialize schema:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user