-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
39 lines (33 loc) · 1.38 KB
/
Copy pathindex.ts
File metadata and controls
39 lines (33 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { drizzle } from 'drizzle-orm/better-sqlite3';
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import Database from 'better-sqlite3';
import * as schema from './schema.js';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Ensure the data directory exists
const dataDir = path.join(__dirname, '../../data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
const dbPath = path.join(__dirname, '../../data/stats.db');
const migrationsFolder = path.join(__dirname, '../../drizzle');
const sqlite = new Database(dbPath);
// Enable WAL mode for better concurrent access
sqlite.pragma('journal_mode = WAL');
sqlite.pragma('busy_timeout = 5000'); // Wait up to 5 seconds if database is locked
export const db = drizzle(sqlite, { schema });
// Run all pending Drizzle migrations on startup
migrate(db, { migrationsFolder });
// Migrate legacy visitors table into badges (no-op if already done or table absent)
const legacyExists = sqlite
.prepare(`SELECT 1 FROM sqlite_master WHERE type='table' AND name='visitors'`)
.get();
if (legacyExists) {
sqlite.exec(`
INSERT OR IGNORE INTO badges (username, visitors)
SELECT username, count FROM visitors;
`);
}