-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlWrap.js
54 lines (41 loc) · 1.37 KB
/
sqlWrap.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
'use strict'
const sql = require('sqlite3');
const util = require('util');
// old-fashioned database creation code
// creates a new database object, not a
// new database.
const db = new sql.Database("activities.db");
// check if database exists
let cmd = " SELECT name FROM sqlite_master WHERE type='table' AND name='ActivityTable' ";
db.get(cmd, function (err, val) {
if (val == undefined) {
console.log("No database file - creating one");
createActivityTable();
} else {
console.log("Database file found");
}
});
// called to create table if needed
function createActivityTable() {
// explicitly declaring the rowIdNum protects rowids from changing if the
// table is compacted; not an issue here, but good practice
const cmd = 'CREATE TABLE ActivityTable (rowIdNum INTEGER PRIMARY KEY, activity TEXT, date INTEGER, amount FLOAT)';
db.run(cmd, function(err, val) {
if (err) {
console.log("Database creation failure",err.message);
} else {
console.log("Created database");
}
});
}
// wrap all database commands in promises
db.run = util.promisify(db.run);
db.get = util.promisify(db.get);
db.all = util.promisify(db.all);
// empty all data from db
db.deleteEverything = async function() {
await db.run("delete from ActivityTable");
db.run("vacuum");
}
// allow code in index.js to use the db object
module.exports = db;