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) => {
|
|
|
|
sql.exec("select * from `cfg`", (err, rows) => {
|
|
|
|
if(err || rows.length < 1)
|
|
|
|
reject({ error: "no cfg" });
|
|
|
|
else {
|
2017-11-22 04:44:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-22 04:44:02 +00:00
|
|
|
const write = (kat, key, value, type, cb) => {
|
|
|
|
if (type === "json")
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
|
|
|
|
self.sql.query("select * from `cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => {
|
|
|
|
if (rows.length > 0) {
|
|
|
|
self.sql.query("update `cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => {
|
|
|
|
self.read(cfg => {
|
|
|
|
self.cfg = cfg
|
|
|
|
if (cb) cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.sql.query("insert into `cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => {
|
|
|
|
self.read(cfg => {
|
|
|
|
self.cfg = cfg
|
|
|
|
if (cb) cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export { cfg, read, write };
|