-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
49 lines (41 loc) · 1.33 KB
/
Copy pathdatabase.js
File metadata and controls
49 lines (41 loc) · 1.33 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
import Database from 'better-sqlite3';
// import { homedir } from 'os';
import { join } from 'path';
// import { mkdirSync } from 'fs';
const dbPath = join(process.cwd(), 'mood_tracker.db');
const db = new Database(dbPath);
db.exec(`
CREATE TABLE IF NOT EXISTS mood_board (
id INTEGER PRIMARY KEY AUTOINCREMENT,
mood_value INTEGER NOT NULL,
timestamp TEXT NOT NULL DEFAULT (datetime('now')),
notes TEXT
)
`);
export function saveMoodEntry(moodValue, notes) {
const stmt = db.prepare(`INSERT INTO mood_board (mood_value, notes) VALUES (?, ?)`);
stmt.run(moodValue, notes || null);
}
db.exec(`
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
`);
export function getLogoColourIndexFromDb() {
const colour = db.prepare(`SELECT value FROM settings WHERE key = 'Colour'`);
const object = colour.get();
if (object?.value === undefined) {
return 0;
} else {
return parseInt(object.value);
}
}
export function saveLogoColourIndex(colourScheme) {
const prep = db.prepare(`INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)`)
prep.run("Colour", colourScheme);
}
export function getMoodEntriesFromDb() {
const results = db.prepare(`SELECT * FROM mood_board ORDER BY id DESC`);
return results.all();
}