-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
112 lines (97 loc) · 3.61 KB
/
Copy pathdb.js
File metadata and controls
112 lines (97 loc) · 3.61 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
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
const sqlite3 = require('sqlite3').verbose();
// A class implementing a database for flashcards
class Database extends sqlite3.Database {
constructor(createTables = true) {
super('flashcards.db');
const db = this;
if (createTables) { // run when initializing the db
db.run(
'CREATE TABLE flashcards(\
id INTEGER PRIMARY KEY,\
user INT,\
text CHAR(100),\
translation CHAR(100),\
asked INT,\
answered INT\
)',
error => { if (error) console.log('Table creation error ', error); }
);
db.run(
'CREATE TABLE users(\
id INT PRIMARY KEY,\
hash CHAR(64)\
)',
error => { if (error) console.log('Table creation error ', error); }
);
}
}
addUser(hash) {
if (hash.length != 64 || hash.includes(';')) throw 'invalid hash';
const db = this;
db.all(
'SELECT * FROM users',
(err, data) => {
const id = data.length + 1 || 1;
db.run(
'INSERT INTO users VALUES(?, ?)',
[id, hash]
);
}
);
}
getUser(hash, callback) {
if (hash.length != 64 || hash.includes(';')) throw 'invalid hash';
const db = this;
db.get(
'SELECT id FROM users WHERE hash = ?',
[hash],
(err, data) => { callback(data && data.id ? data.id : undefined); }
);
}
addCard(hash, text, translation, asked = 0, answered = 0) {
const db = this;
db.getUser(hash, user => {
try { // validate and format input
if (isNaN(user) || user % 1 !== 0) throw `user (${user}) must be an int`;
if (typeof text != 'string' || typeof translation != 'string') throw 'text and translation must be strings';
if (text.length > 100) text = text.substring(0, 100);
if (translation.length > 100) translation = translation.substring(0, 100);
if (text.includes(';') || translation.includes(';')) throw 'text and translation must not include semicolons';
} catch (error) {
console.log(error);
return;
}
db.run(
'INSERT INTO flashcards (user, text, translation, asked, answered) VALUES(?, ?, ?, ?, ?)',
[user, text, translation, asked, answered],
() => { console.log('Adding: ', user, text, translation); }
);
});
}
updateCard(id, asked, answered) {
const db = this;
db.get(
'SELECT * FROM flashcards WHERE id = ?',
[id],
(err, card) => {
if (!card || !card.id) throw 'could not update the card';
db.run(
'REPLACE INTO flashcards VALUES(?, ?, ?, ?, ?, ?)',
[card.id, card.user, card.text, card.translation, asked, answered]
);
}
);
}
getCards(hash, callback) {
if (hash.length != 64 || hash.includes(';')) throw 'invalid hash';
const db = this;
db.getUser(hash, user => {
db.all(
'SELECT id, text, translation, asked, answered FROM flashcards WHERE user = ?',
[user],
(err, cards) => { callback(cards); }
);
});
}
}
module.exports = Database;