Migrate config to PostgreSQL

This commit is contained in:
mstfyldz
2026-05-27 16:53:06 +03:00
parent 10f7ee95dd
commit a5951a97e9
6 changed files with 117 additions and 48 deletions

View File

@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import pool from './appDb'
const CONFIG_PATH = path.join(process.cwd(), 'config.json')
@@ -36,18 +37,75 @@ export interface Config {
services: Service[]
}
export function readConfig(): Config {
if (!fs.existsSync(CONFIG_PATH)) {
return { sites: [], databases: [], services: [] }
}
const raw = fs.readFileSync(CONFIG_PATH, 'utf-8')
return JSON.parse(raw)
}
export function writeConfig(config: Config): void {
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), 'utf-8')
}
export function generateId(): string {
return `${Date.now()}-${Math.random().toString(36).slice(2, 7)}`
}
// Migration ve okuma bir arada
export async function readConfig(): Promise<Config> {
// Migration kontrolü
if (fs.existsSync(CONFIG_PATH)) {
try {
const raw = fs.readFileSync(CONFIG_PATH, 'utf-8')
const oldConfig = JSON.parse(raw) as Config
// Veritabanına aktar
for (const site of oldConfig.sites || []) {
await pool.query(`INSERT INTO config_sites (id, name, url, interval_min) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`, [site.id, site.name, site.url, site.interval_min || 5])
}
for (const db of oldConfig.databases || []) {
await pool.query(`INSERT INTO config_databases (id, name, host, port, database, username, password, ssl, color) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT DO NOTHING`, [db.id, db.name, db.host, db.port, db.database, db.username, db.password, db.ssl || false, db.color])
}
for (const svc of oldConfig.services || []) {
await pool.query(`INSERT INTO config_services (id, name, url, icon, description) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING`, [svc.id, svc.name, svc.url, svc.icon, svc.description])
}
// Dosyanın uzantısını değiştirerek migration'ın bittiğini işaretle
fs.renameSync(CONFIG_PATH, CONFIG_PATH + '.bak')
console.log('[Migration] config.json verileri PostgreSQL tablolalarına aktarıldı ve dosya yedeklendi.')
} catch (e) {
console.error('[Migration] config.json veritabanına aktarılırken hata oluştu:', e)
}
}
// DB'den oku
const [sitesRes, dbsRes, svcsRes] = await Promise.all([
pool.query(`SELECT * FROM config_sites`),
pool.query(`SELECT * FROM config_databases`),
pool.query(`SELECT * FROM config_services`),
])
return {
sites: sitesRes.rows as Site[],
databases: dbsRes.rows as Database[],
services: svcsRes.rows as Service[]
}
}
export async function addConfigItem(type: 'site' | 'database' | 'service', item: any): Promise<any> {
const newItem = { ...item, id: generateId() }
if (type === 'site') {
await pool.query(`INSERT INTO config_sites (id, name, url, interval_min) VALUES ($1, $2, $3, $4)`, [newItem.id, newItem.name, newItem.url, newItem.interval_min || 5])
} else if (type === 'database') {
await pool.query(`INSERT INTO config_databases (id, name, host, port, database, username, password, ssl, color) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [newItem.id, newItem.name, newItem.host, newItem.port, newItem.database, newItem.username, newItem.password, newItem.ssl || false, newItem.color])
} else if (type === 'service') {
await pool.query(`INSERT INTO config_services (id, name, url, icon, description) VALUES ($1, $2, $3, $4, $5)`, [newItem.id, newItem.name, newItem.url, newItem.icon, newItem.description])
}
return newItem
}
export async function updateConfigItem(type: 'site' | 'database' | 'service', id: string, item: any): Promise<void> {
if (type === 'site') {
await pool.query(`UPDATE config_sites SET name=$1, url=$2, interval_min=$3 WHERE id=$4`, [item.name, item.url, item.interval_min || 5, id])
} else if (type === 'database') {
await pool.query(`UPDATE config_databases SET name=$1, host=$2, port=$3, database=$4, username=$5, password=$6, ssl=$7, color=$8 WHERE id=$9`, [item.name, item.host, item.port, item.database, item.username, item.password, item.ssl || false, item.color, id])
} else if (type === 'service') {
await pool.query(`UPDATE config_services SET name=$1, url=$2, icon=$3, description=$4 WHERE id=$5`, [item.name, item.url, item.icon, item.description, id])
}
}
export async function deleteConfigItem(type: 'site' | 'database' | 'service', id: string): Promise<void> {
if (type === 'site') await pool.query(`DELETE FROM config_sites WHERE id=$1`, [id])
else if (type === 'database') await pool.query(`DELETE FROM config_databases WHERE id=$1`, [id])
else if (type === 'service') await pool.query(`DELETE FROM config_services WHERE id=$1`, [id])
}