From 2d82a857487c7b3bc09756d34a551330a7c3f4a9 Mon Sep 17 00:00:00 2001 From: Tuan-Dat Tran Date: Fri, 20 Feb 2026 17:13:31 +0100 Subject: [PATCH] refactor(api): use knex for database operations --- backend/db/init.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 backend/db/init.js diff --git a/backend/db/init.js b/backend/db/init.js new file mode 100644 index 0000000..5c355e7 --- /dev/null +++ b/backend/db/init.js @@ -0,0 +1,53 @@ +import knex from 'knex'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import config from '../knexfile.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +let db = null; + +export async function getDB() { + if (!db) { + db = knex(config); + await db.migrate.latest(); + } + return db; +} + +export async function initDB() { + const db = await getDB(); + + const existing = await db('cv_data').where({ id: 1 }).first(); + if (!existing) { + await db('cv_data').insert({ + id: 1, + data: JSON.stringify({ + personal: { + name: "Tuan-Dat Tran", + title: "Junior DevOps Engineer", + intro: "Passionierter DevOps Engineer mit Fokus auf Cloud-Infrastruktur.", + email: "tuan-dat.tran@example.com", + github: "https://github.com/tuan-dat-tran", + linkedin: "https://linkedin.com/in/tuan-dat-tran", + location: "Deutschland" + }, + experience: [], + skills: {}, + education: [], + projects: [] + }) + }); + console.log('Initialized database with default CV data'); + } + + console.log('Database initialized'); +} + +export async function closeDB() { + if (db) { + await db.destroy(); + db = null; + } +}