-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
68 lines (61 loc) · 1.47 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
65
66
67
68
import sqlite from 'sqlite3'
export function iniDatabase() {
return new sqlite.Database('data', (err) => {
if (err) {
throw err
}
console.log('Init DB success!')
})
}
/**
*
* @param {sqlite.Database} db
*/
export function iniTable(db) {
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS product (
id INTEGER PRIMARY KEY AUTOINCREMENT,
photo TEXT NOT NULL,
name VARCHAR(56) NOT NULL,
price INTEGER NOT NULL
);`)
})
}
/**
*
* @param {sqlite.Database} db
* @param {string} name
* @param {number} price
* @param {string} photo
*/
export function insertProduct(db, name, price, photo) {
db.run('INSERT INTO product (photo,name,price) VALUES ($photo,$name,$price)', { $photo: photo, $name: name, $price: price }, (err) => {
if (err) {
throw err
}
console.log('Product saved!')
})
}
/**
*
* @param {sqlite.Database} db
*/
export function getProduct(db) {
/*db.get('SELECT * FROM product', (err, result) => {
if(err) {
console.log(err)
throw err
}
console.log('Query Result', result)
return result
})*/
return new Promise((resolve, reject) => {
db.all('SELECT * FROM product', (err, result) => {
if(err) {
reject(err)
}
console.log('Query Result', result)
resolve(result)
})
})
}