-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
63 lines (56 loc) · 1.44 KB
/
database.js
File metadata and controls
63 lines (56 loc) · 1.44 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
const sqlite3 = require("sqlite3").verbose();
const path = require("path");
const dbPath = path.join(__dirname, "budget.db");
const db = new sqlite3.Database(dbPath);
// Create the bills table
db.serialize(() => {
db.run(`
CREATE TABLE IF NOT EXISTS bills (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
amount REAL NOT NULL,
type TEXT NOT NULL
)
`);
});
function addBill(name, amount, type) {
return new Promise((resolve, reject) => {
db.run(
"INSERT INTO bills (name, amount, type) VALUES (?, ?, ?)",
[name, amount, type],
function (err) {
if (err) reject(err);
else resolve(this.lastID);
}
);
});
}
function getBills() {
return new Promise((resolve, reject) => {
db.all("SELECT * FROM bills", (err, rows) => {
if (err) reject(err);
else resolve(rows);
});
});
}
function editBill(id, newName, newAmount) {
return new Promise((resolve, reject) => {
db.run(
"UPDATE bills SET name = ?, amount = ? WHERE id = ?",
[newName, newAmount, id],
function (err) {
if (err) reject(err);
else resolve(this.changes);
}
);
});
}
function deleteBill(id) {
return new Promise((resolve, reject) => {
db.run("DELETE FROM bills WHERE id = ?", [id], function (err) {
if (err) reject(err);
else resolve(this.changes);
});
});
}
module.exports = { addBill, getBills, editBill, deleteBill };