-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
58 lines (55 loc) · 1.65 KB
/
db.js
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
import { DB } from "https://deno.land/x/sqlite@v3.1.1/mod.ts";
import { ensureDirSync } from "https://deno.land/std@0.106.0/fs/mod.ts";
// Open a database
ensureDirSync("data/")
const db = new DB("data/comments.db");
db.query(`
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id TEXT,
comment_id INTEGER,
log_filename TEXT,
log_git_hash TEXT,
log_time_created timestamp,
time_created timestamp,
time_updated timestamp,
ip_address TEXT,
text TEXT,
user TEXT,
auth TEXT,
visible INTEGER default true,
FOREIGN KEY(comment_id) REFERENCES comments(id)
);
CREATE TABLE IF NOT EXISTS votes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
comment_id INTEGER,
value INTEGER,
log_filename TEXT,
log_git_hash TEXT,
log_time_created timestamp,
time_created timestamp,
time_updated timestamp,
ip_address TEXT,
user TEXT,
auth TEXT,
FOREIGN KEY(comment_id) REFERENCES comments(id)
)
`);
export default function (logs) {
// Run a simple query
for (const logn of logs) {
let log = { ...logn };
if (log.action === "comment") {
delete log.action;
db.query(
`INSERT INTO comments (${Object.keys(log).join(", ")}) VALUES (${Object.keys(log).map(log => "?").join(", ")})`,
Object.values(log)
);
} else if (log.action === "upvote" || log.action === "downvote") {
db.query(
`INSERT INTO votes ${Object.keys(log)} VALUES (${Object.keys(log)}.map(log => "?").join(", "))`,
Object.values(log)
);
}
}
}