-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.js
60 lines (42 loc) · 1.29 KB
/
crud.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
const sqlite3 = require('sqlite3')
const db = new sqlite3.Database('database.db')
db.serialize(() => {
const sql = `
CREATE TABLE IF NOT EXISTS users
(id integer primary key, username TEXT unique, password_hash TEXT)
`;
db.run(sql);
const sql_faq = `
CREATE TABLE IF NOT EXISTS freq_ask_questions
(id integer primary key, question TEXT unique, answer TEXT)
`;
db.run(sql_faq);
});
class Crud {
static create_table() {
db.run("CREATE TABLE...", function(error){
if(error){
// Query could not be executed.
}else{
// Query successfully executed.
}
})
}
static save_hashed_password(username, passwd, cb) {
const sql = `INSERT INTO users(username, password_hash) values(?, ?)`
db.run(sql, username, passwd, cb)
}
static add_faq(question, answer, cb) {
const sql = `INSERT INTO freq_ask_questions(question, answer) values(?, ?)`
db.run(sql, question, answer, cb)
}
static add_blog(title, content, cb) {
const sql = `INSERT INTO articles(title, content, createTime) values(?, ?, ?)`
db.run(sql, title, content, new Date(), cb)
}
static get_hashed_by_name(username, cb) {
const query = "SELECT * FROM users WHERE username = ?"
db.get(query, username, cb)
}
}
module.exports.CrudAPI = Crud;