-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_db_sqlite.js
68 lines (59 loc) · 1.94 KB
/
_db_sqlite.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
59
60
61
62
63
64
65
66
67
68
const config = require('./config');
const sqlite3 = require('sqlite3').verbose();
const sqliteDB = new sqlite3.Database(config.database);
module.exports = {
dbGetAll : function(sql, parameters){
return new Promise((resolve, reject) => {
sqliteDB.all(sql, parameters, (err, rows) => {
if(err){ return reject(err); }
return resolve(rows);
});
});
},
dbGetSingle : function(sql, parameters){
return new Promise((resolve, reject) => {
sqliteDB.get(sql, parameters, (err, row) => {
if(err){ return reject(err); }
return resolve(row);
});
});
},
dbGetSingleField : function(sql, parameters, field){
let _self = this;
return new Promise((resolve, reject) => {
_self.dbGetSingle(sql, parameters).then((row) => {
return resolve(row[field])
}).catch((err) => { return reject(err); });
});
},
dbInsert : function(table, qdata){
return new Promise((resolve, reject) => {
let placeholder_keys = [];
let placeholders = {};
let key_string = [];
Object.getOwnPropertyNames(qdata).forEach((v, i, a) => {
let key = '$'+ v;
placeholder_keys.push(key);
placeholders[key] = qdata[v];
key_string.push(v);
});
let sql = "INSERT INTO "+ table +" ("+ key_string.join(',') +") VALUES ("+ placeholder_keys.join(',') +");";
let stmt = sqliteDB.prepare(sql);
stmt.run(placeholders, (err) => {
if(err){ return reject(err); }
return resolve();
});
});
},
dbRun : function(sql, conlog){
return new Promise((resolve, reject) => {
sqliteDB.serialize(() => {
sqliteDB.run(sql, (err) => {
if(err){ return reject(err); }
if(conlog){ console.log(conlog); }
return resolve();
});
});
});
}
}