-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.js
58 lines (55 loc) · 1.74 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
const sqlite3 = require('sqlite3').verbose();
exports.openDB = function(){
return new sqlite3.Database('./test.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the test database.');
});
}
exports.serialize = function(db){
return new Promise(function(resolve,reject){
db.serialize(
function(){
let data = [];
db.each(`SELECT ID as ID,
company as company,
name as name,
phone as phone,
email as email
FROM clients`,
(err, row) =>{
if (err) {
console.error(err.message);
}
data.push({
"ID":row.ID,
"company":row.company,
"name":row.name,
"phone":row.phone,
"email":row.email
});
},
() => {
resolve(data);
});
});
});
}
exports.closeDB = function(db){
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log('Close the database connection.');
});
}
exports.edit_client = function(db,data){
db.run(`INSERT INTO clients (company, name, email, phone) VALUES(?,?,?,?)`, [data.company,data.name,data.email,data.phone], function(err) {
if (err) {
return console.log(err.message);
}
// get the last insert id
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
}