Files
Webserver/lib/db.ts

29 lines
724 B
TypeScript
Raw Normal View History

import Database from 'better-sqlite3';
import path from 'path';
import fs from 'fs';
const SERVER_STORAGE = '/server_storage';
const dbPath = fs.existsSync(SERVER_STORAGE)
? path.join(SERVER_STORAGE, 'visitors.db')
: path.join(process.cwd(), 'visitors.db');
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.exec(`
CREATE TABLE IF NOT EXISTS visits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ip_address TEXT NOT NULL,
path TEXT NOT NULL,
visited_at TEXT NOT NULL DEFAULT (datetime('now'))
);
`);
const insertStmt = db.prepare(
'INSERT INTO visits (ip_address, path) VALUES (?, ?)'
);
export function logVisit(ip: string, visitPath: string) {
insertStmt.run(ip, visitPath);
}