1 |
rakin |
5 |
const { Database: DB } = require('sqlite3'); |
2 |
|
|
|
3 |
|
|
class Database { |
4 |
|
|
constructor(dbpath) { |
5 |
|
|
this.dbpath = dbpath; |
6 |
|
|
this.db = new DB(dbpath, (err) => { |
7 |
|
|
if (err) { |
8 |
|
|
console.log(err); |
9 |
|
|
} |
10 |
|
|
}); |
11 |
|
|
} |
12 |
|
|
|
13 |
|
|
get(sql, callback1, callback2) { |
14 |
|
|
return this.db.get(sql, callback1, callback2); |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
all(sql, callback1, callback2) { |
18 |
|
|
return this.db.all(sql, callback1, callback2); |
19 |
|
|
} |
20 |
rakin |
45 |
|
21 |
|
|
runAsync(sql, paramsOrCallback = []) { |
22 |
|
|
return new Promise((resolve, reject) => { |
23 |
|
|
this.db.run(sql, paramsOrCallback, err => { |
24 |
|
|
if (err) { |
25 |
|
|
reject(err); |
26 |
|
|
return; |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
resolve(); |
30 |
|
|
}); |
31 |
|
|
}); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
getAsync(sql, paramsOrCallback = []) { |
35 |
|
|
return new Promise((resolve, reject) => { |
36 |
|
|
this.db.get(sql, paramsOrCallback, (err, data) => { |
37 |
|
|
if (err) { |
38 |
|
|
reject(err); |
39 |
|
|
return; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
resolve(data); |
43 |
|
|
}); |
44 |
|
|
}); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
allAsync(sql, paramsOrCallback = []) { |
48 |
|
|
return new Promise((resolve, reject) => { |
49 |
|
|
this.db.all(sql, paramsOrCallback, (err, data) => { |
50 |
|
|
if (err) { |
51 |
|
|
reject(err); |
52 |
|
|
return; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
resolve(data); |
56 |
|
|
}); |
57 |
|
|
}); |
58 |
|
|
} |
59 |
rakin |
5 |
} |
60 |
|
|
|
61 |
|
|
module.exports = Database; |