2017-11-18 10:53:58 +00:00
|
|
|
import sql from "./sql.js";
|
2017-11-07 17:22:41 +00:00
|
|
|
|
|
|
|
let cfg = {
|
2017-11-08 20:40:59 +00:00
|
|
|
client: {},
|
2017-11-07 17:22:41 +00:00
|
|
|
main: {},
|
|
|
|
websrv: {},
|
|
|
|
trigger: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
const read = () => new Promise((resolve, reject) => {
|
2017-11-28 01:27:25 +00:00
|
|
|
sql.any("select * from cfg").then(rows => {
|
|
|
|
for (let row in rows) {
|
|
|
|
cfg[rows[row].class][rows[row].key] = {
|
|
|
|
val: ((type, value) => {
|
|
|
|
switch (type) {
|
|
|
|
case "string":
|
|
|
|
return value;
|
|
|
|
case "int":
|
|
|
|
return parseInt(value);
|
|
|
|
case "bool":
|
|
|
|
return value === "true";
|
|
|
|
case "json":
|
|
|
|
return JSON.parse(value);
|
|
|
|
}
|
|
|
|
})(rows[row].type, rows[row].value),
|
|
|
|
hidden: rows[row].hidden === 1,
|
|
|
|
type: rows[row].type
|
2017-11-07 17:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-28 01:27:25 +00:00
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject("no cfg");
|
|
|
|
})
|
2017-11-07 17:22:41 +00:00
|
|
|
});
|
|
|
|
|
2017-11-22 04:44:02 +00:00
|
|
|
const write = (kat, key, value, type, cb) => {
|
|
|
|
if (type === "json")
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
|
2017-11-22 18:23:43 +00:00
|
|
|
console.log(kat, key, value, type);
|
2017-11-28 15:39:36 +00:00
|
|
|
sql.any("select * from cfg where class = $1 && key = $2 limit 1", [kat, key]).then(rows => {
|
2017-11-22 04:44:02 +00:00
|
|
|
if (rows.length > 0) {
|
2017-11-28 15:39:36 +00:00
|
|
|
sql.any("update cfg set value = $1 where class = $2 && key = $3", [value, kat, key]).then(rows => {
|
2017-11-22 18:23:43 +00:00
|
|
|
read().then(() => {
|
|
|
|
if(cb) cb();
|
2017-11-22 04:44:02 +00:00
|
|
|
});
|
2017-11-28 15:39:36 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log("on update", err);
|
2017-11-22 04:44:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2017-11-28 15:39:36 +00:00
|
|
|
sql.any("insert into cfg (class, key, value, type) values ($1, $2, $3, $4)", [kat, key, value, type]).then(rows => {
|
2017-11-22 18:23:43 +00:00
|
|
|
read().then(() => {
|
|
|
|
if (cb) cb();
|
2017-11-22 04:44:02 +00:00
|
|
|
});
|
2017-11-28 15:39:36 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log("on insert", err);
|
2017-11-22 04:44:02 +00:00
|
|
|
});
|
|
|
|
}
|
2017-11-28 15:39:36 +00:00
|
|
|
})
|
|
|
|
.then(err => {
|
|
|
|
console.log("on select", err);
|
2017-11-22 04:44:02 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export { cfg, read, write };
|