-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
113 lines (103 loc) · 2.41 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const sqlite3 = require("sqlite3").verbose();
let db;
if (process.env.IN_MEMORY_DB) {
db = new sqlite3.Database(":memory:");
} else {
// This will create the file if it doesn't exist.
db = new sqlite3.Database(process.env.DB_PATH || "wikig.db");
}
function init(cb) {
db.serialize(() => {
db.run(`
CREATE TABLE pages (
page_id INTEGER PRIMARY KEY,
name VARCHAR(1024) NOT NULL UNIQUE,
content TEXT NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)`);
// TODO: Enable foreign key constraints.
// https://stackoverflow.com/q/15301643/509706
db.run(
`
CREATE TABLE page_revisions (
revision_id INTEGER PRIMARY KEY,
name VARCHAR(1024) NOT NULL,
content TEXT NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
page_id INTEGER NOT NULL,
FOREIGN KEY (page_id) references pages(page_id)
)`,
cb
);
});
}
function allPages(callback) {
db.all(
`SELECT rowid, name, created, updated, content
FROM pages
ORDER BY updated DESC`,
callback
);
}
function allPageNames(callback) {
db.all(
`SELECT name
FROM pages`,
callback
);
}
function getPageByName(name, callback) {
db.get(
`SELECT page_id, name, content, created, updated
FROM pages WHERE name = ?`,
[name],
callback
);
}
function getPage(rowid, callback) {
db.get(
`SELECT page_id, name, content, created, updated
FROM pages WHERE rowid = ?`,
[rowid],
callback
);
}
function updatePage(pageid, name, content, callback) {
// Based on https://stackoverflow.com/a/4330694/509706
db.get(
`INSERT INTO page_revisions (page_id, name, content) VALUES(?, ?, ?)`,
[pageid, name, content],
() =>
db.get(
`UPDATE pages SET name = ?, content = ?, updated = ?
WHERE page_id = ?`,
[name, content, new Date().toISOString(), pageid],
callback
)
);
}
// Create a page with this name and content, then return the newly
// created page.
function createPage(name, content, callback) {
// Based on https://stackoverflow.com/a/4330694/509706
db.run(
`INSERT INTO pages (name, content) VALUES(?, ?)`,
[name, content],
err => {
if (err) {
return err;
}
return getPageByName(name, callback);
}
);
}
module.exports = {
init,
allPages,
allPageNames,
getPage,
getPageByName,
createPage,
updatePage
};