-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
44 lines (39 loc) · 1.05 KB
/
Copy pathsettings.js
File metadata and controls
44 lines (39 loc) · 1.05 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
const ServerError = require('./server_error')
class SettingsHelper{
constructor(db){
this.db = db;
this.settings = {};
}
initSettings() {
return this.db.any("SELECT * FROM settings")
.then(rows =>{
rows.forEach(row => {
this.settings[row.name] = row.data;
})
}).catch(err =>{
throw(new ServerError(500, err.message));
});
}
getMiddleware(){
let object = this;
return (req, res, next)=>{
res.locals.settings = object.settings;
next();
}
}
changeSettings(settings){
return this.db.tx(async t=>{
for(let key in settings){
await t.none("UPDATE settings set data = $1 WHERE name = $2", [settings[key].toString(), key.toString()])
}
}).then(()=>{
for(let key in settings){
this.settings[key] = settings[key]
}
})
.catch(err =>{
throw(new ServerError(500, err.message));
})
}
}
module.exports = {SettingsHelper: SettingsHelper}