configModel.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
  2. function ConfigModel() {
  3. DatabaseModel.call(this);
  4. this.key = "";
  5. this.value = "";
  6. }
  7. ConfigModel.prototype = Object.create(DatabaseModel.prototype);
  8. ConfigModel.prototype.getTableName = function() {
  9. return "config";
  10. }
  11. ConfigModel.prototype.createOrUpdateBase = async function(dbHelper) {
  12. await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'config' (
  13. key STRING NOT NULL PRIMARY KEY,
  14. value STRING
  15. )`);
  16. }
  17. ConfigModel.prototype.describe = function() {
  18. return {
  19. "key": this.key,
  20. "value": this.value
  21. };
  22. }
  23. ConfigModel.prototype.versionColumn = function() { return ""; }
  24. ConfigModel.prototype.fromDb = function(dbObj) {
  25. this.key = dbObj["key"];
  26. this.value = dbObj["value"];
  27. }
  28. class ConfigLoader
  29. {
  30. #loading = null;
  31. #configEntries = {};
  32. constructor(app) {
  33. this.#loading = this.#init(app);
  34. }
  35. async #init(app) {
  36. // FIXME
  37. }
  38. async getValue(key) {
  39. await this.#loading;
  40. return this.#configEntries[key] || null;
  41. }
  42. async setValue(key, value) {
  43. await this.#loading;
  44. return this.#configEntries[key] = value;
  45. }
  46. async flush(app) {
  47. await this.#loading;
  48. // FIXME
  49. }
  50. }
  51. module.exports.ConfigModel = ConfigModel;
  52. module.exports.ConfigLoader = (app) => new ConfigModel(app);