refactor(api): use knex for database operations
This commit is contained in:
53
backend/db/init.js
Normal file
53
backend/db/init.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user