refactor: Modularize server into separate concerns
Split monolithic index.ts (~1400 lines) into modular structure: - config.ts: Server configuration and constants - db/: Database initialization, migrations, and seeds - routes/: API handlers by domain (themes, canvas, components, etc.) - services/: Terminal WebSocket server - utils/: CORS helpers Entry point now only coordinates initialization.
This commit is contained in:
107
server/db/migrations.ts
Normal file
107
server/db/migrations.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
|
||||
export function runMigrations(db: Database) {
|
||||
// History table
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
timestamp TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
tool_name TEXT NOT NULL,
|
||||
args TEXT,
|
||||
result TEXT
|
||||
)
|
||||
`)
|
||||
|
||||
// Config table
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS config (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT
|
||||
)
|
||||
`)
|
||||
|
||||
// Vue components table
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS vue_components (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
template TEXT NOT NULL,
|
||||
setup TEXT,
|
||||
style TEXT,
|
||||
props TEXT,
|
||||
imports TEXT,
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`)
|
||||
|
||||
// Themes table
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS themes (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
is_default INTEGER DEFAULT 0,
|
||||
is_system INTEGER DEFAULT 0,
|
||||
variables TEXT NOT NULL,
|
||||
metadata TEXT,
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`)
|
||||
|
||||
// Project canvas table
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS project_canvas (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
type TEXT NOT NULL DEFAULT 'project',
|
||||
theme_id TEXT,
|
||||
config TEXT,
|
||||
tools TEXT,
|
||||
is_default INTEGER DEFAULT 0,
|
||||
is_system INTEGER DEFAULT 0,
|
||||
show_in_toolbar INTEGER DEFAULT 0,
|
||||
toolbar_icon TEXT,
|
||||
toolbar_order INTEGER DEFAULT 99,
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`)
|
||||
|
||||
// Canvas-components relation table
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS canvas_components (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
canvas_id TEXT NOT NULL,
|
||||
component_id TEXT NOT NULL,
|
||||
position INTEGER DEFAULT 0,
|
||||
props TEXT,
|
||||
layout TEXT,
|
||||
is_visible INTEGER DEFAULT 1,
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(canvas_id, component_id)
|
||||
)
|
||||
`)
|
||||
|
||||
// Run column migrations for existing tables
|
||||
runColumnMigrations(db)
|
||||
}
|
||||
|
||||
function runColumnMigrations(db: Database) {
|
||||
// Add toolbar columns to project_canvas if missing
|
||||
const alterStatements = [
|
||||
'ALTER TABLE project_canvas ADD COLUMN show_in_toolbar INTEGER DEFAULT 0',
|
||||
'ALTER TABLE project_canvas ADD COLUMN toolbar_icon TEXT',
|
||||
'ALTER TABLE project_canvas ADD COLUMN toolbar_order INTEGER DEFAULT 99'
|
||||
]
|
||||
|
||||
for (const sql of alterStatements) {
|
||||
try {
|
||||
db.run(sql)
|
||||
} catch {
|
||||
// Column already exists
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user