-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.mjs
More file actions
25 lines (22 loc) · 816 Bytes
/
db.mjs
File metadata and controls
25 lines (22 loc) · 816 Bytes
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
import Database from "better-sqlite3";
import { TABLE_KEY_NAME, TABLE_NAME, TABLE_VALUE_NAME } from "./constants.mjs";
const db = new Database(`${TABLE_NAME}.db`);
export function createTable() {
db.exec(`CREATE TABLE ${TABLE_NAME} (
${TABLE_KEY_NAME} INTEGER PRIMARY KEY,
${TABLE_VALUE_NAME} TEXT NOT NULL
)`);
}
export function getMostRecent() {
const getFromTable = db.prepare(
`SELECT ${TABLE_KEY_NAME}, ${TABLE_VALUE_NAME} FROM ${TABLE_NAME} ORDER BY ${TABLE_KEY_NAME} DESC LIMIT 1`
);
const mostRecent = getFromTable.get();
return mostRecent.record_value ? mostRecent.record_value : null;
}
export function insert(value) {
const insert = db.prepare(
`INSERT INTO ${TABLE_NAME} (${TABLE_VALUE_NAME}) VALUES (@${TABLE_VALUE_NAME})`
);
insert.run({ [TABLE_VALUE_NAME]: value });
}