26 lines
677 B
TypeScript
26 lines
677 B
TypeScript
|
|
import sqlite3 from 'sqlite3';
|
||
|
|
import { open, Database } from 'sqlite';
|
||
|
|
|
||
|
|
let db: Database | null = null;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Opens a read-only connection to the webserver's visitors.db.
|
||
|
|
* Returns null if the DB doesn't exist yet (webserver hasn't created it).
|
||
|
|
* Retries on each call until the DB becomes available.
|
||
|
|
*/
|
||
|
|
export async function getVisitorsDb(): Promise<Database | null> {
|
||
|
|
if (db) return db;
|
||
|
|
|
||
|
|
try {
|
||
|
|
db = await open({
|
||
|
|
filename: '/server_storage/visitors.db',
|
||
|
|
driver: sqlite3.Database,
|
||
|
|
mode: sqlite3.OPEN_READONLY,
|
||
|
|
});
|
||
|
|
return db;
|
||
|
|
} catch {
|
||
|
|
// DB doesn't exist yet — don't cache the failure so we retry next time
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|