19 lines
725 B
TypeScript
19 lines
725 B
TypeScript
import { DatabaseSync } from 'node:sqlite'
|
|
import { mkdirSync } from 'node:fs'
|
|
import { dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { TABLE_DEFINITIONS } from './db/tableRegistry'
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
const dbPath = join(__dirname, 'data', 'assignment-monitor.sqlite3')
|
|
mkdirSync(dirname(dbPath), { recursive: true })
|
|
|
|
export const db = new DatabaseSync(dbPath)
|
|
|
|
for (const table of TABLE_DEFINITIONS) {
|
|
const columnsSql = table.columns.map(c => `"${c.name}" ${c.sqlType}`).join(', ')
|
|
db.exec(`CREATE TABLE IF NOT EXISTS "${table.name}" (id INTEGER PRIMARY KEY AUTOINCREMENT, ${columnsSql})`)
|
|
}
|
|
|
|
console.log(`[db] SQLite bereit unter ${dbPath}`)
|