-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
77 lines (66 loc) · 2.48 KB
/
Copy pathdb.js
File metadata and controls
77 lines (66 loc) · 2.48 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const path = require('path');
// node:sqlite is built-in from Node 22.5+; fall back to better-sqlite3 on older versions
let DatabaseSync;
try {
({ DatabaseSync } = require('node:sqlite'));
} catch {
DatabaseSync = require('better-sqlite3');
}
const db = new DatabaseSync(path.join(__dirname, 'audionote.db'));
try { db.exec('PRAGMA journal_mode = WAL;'); } catch {}
db.exec('PRAGMA foreign_keys = ON;');
db.exec(`
CREATE TABLE IF NOT EXISTS visits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TEXT DEFAULT (datetime('now')),
method TEXT,
path TEXT,
status INTEGER,
ip TEXT,
referrer TEXT,
ua TEXT,
ms INTEGER
);
CREATE INDEX IF NOT EXISTS visits_ts ON visits(ts);
`);
db.exec(`
CREATE TABLE IF NOT EXISTS scan_dirs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dirpath TEXT UNIQUE NOT NULL,
label TEXT DEFAULT '',
created_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS songs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filepath TEXT UNIQUE NOT NULL,
title TEXT NOT NULL,
artist TEXT DEFAULT '',
album TEXT DEFAULT '',
duration_sec REAL DEFAULT 0,
created_at TEXT DEFAULT (datetime('now')),
deleted_at TEXT
);
CREATE TABLE IF NOT EXISTS song_notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
song_id INTEGER NOT NULL REFERENCES songs(id) ON DELETE CASCADE,
note_text TEXT DEFAULT '',
updated_at TEXT DEFAULT (datetime('now'))
);
CREATE TABLE IF NOT EXISTS timestamps (
id INTEGER PRIMARY KEY AUTOINCREMENT,
song_id INTEGER NOT NULL REFERENCES songs(id) ON DELETE CASCADE,
time_seconds REAL NOT NULL,
label TEXT DEFAULT '',
category TEXT DEFAULT '',
created_at TEXT DEFAULT (datetime('now'))
);
`);
// ── Migrations for upgrading older databases ──────────────
// Run AFTER table creation so a fresh install already has the columns/tables.
// Each is wrapped: it harmlessly fails (and is ignored) when already applied.
// Legacy DBs created before deleted_at existed get the column added here.
try { db.exec('ALTER TABLE songs ADD COLUMN deleted_at TEXT'); } catch {}
// One note per song — app upserts assume this; skipped silently if legacy dup rows exist
try { db.exec('CREATE UNIQUE INDEX IF NOT EXISTS song_notes_song_id ON song_notes(song_id)'); } catch {}
module.exports = db;