forked from actualbudget/actual-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
41 lines (32 loc) · 776 Bytes
/
db.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
let Database = require('better-sqlite3');
class WrappedDatabase {
constructor(db) {
this.db = db;
}
all(sql, params = []) {
let stmt = this.db.prepare(sql);
return stmt.all(...params);
}
first(sql, params = []) {
let rows = this.all(sql, params);
return rows.length === 0 ? null : rows[0];
}
exec(sql) {
this.db.exec(sql);
}
mutate(sql, params = []) {
let stmt = this.db.prepare(sql);
let info = stmt.run(...params);
return { changes: info.changes, insertId: info.lastInsertRowid };
}
transaction(fn) {
return this.db.transaction(fn)();
}
close() {
this.db.close();
}
}
function openDatabase(filename) {
return new WrappedDatabase(new Database(filename));
}
module.exports = { openDatabase };