-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.js
64 lines (43 loc) · 1.27 KB
/
database.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
const sqlite = require('sqlite3')
const db = new sqlite.Database('peter-ab.db')
db.run(`
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
description TEXT
)
`)
exports.getAllProducts = function(callback){
const query = "SELECT * FROM products"
db.all(query, function(error, products){
callback(error, products)
})
}
exports.createProduct = function(name, description, callback){
const query = "INSERT INTO products (name, description) VALUES (?, ?)"
const values = [name, description]
db.run(query, values, function(error){
callback(error, this.lastID)
})
}
exports.getProductById = function(id, callback){
const query = "SELECT * FROM products WHERE id = ? LIMIT 1"
const values = [id]
db.get(query, values, function(error, product){
callback(error, product)
})
}
exports.updateProductById = function(id, name, description, callback){
const query = "UPDATE products SET name = ?, description = ? WHERE id = ?"
const values = [name, description, id]
db.run(query, values, function(error){
callback(error)
})
}
exports.deleteProductById = function(id, callback){
const query = "DELETE FROM products WHERE id = ?"
const values = [id]
db.run(query, values, function(error){
callback(error)
})
}