You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
981 B

  1. const Model = require('objection').Model
  2. const _ = require('lodash')
  3. /* global WIKI */
  4. /**
  5. * Settings model
  6. */
  7. module.exports = class Setting extends Model {
  8. static get tableName() { return 'settings' }
  9. static get idColumn() { return 'key' }
  10. static get jsonSchema () {
  11. return {
  12. type: 'object',
  13. required: ['key', 'value'],
  14. properties: {
  15. key: {type: 'string'},
  16. value: {type: 'object'},
  17. createdAt: {type: 'string'},
  18. updatedAt: {type: 'string'}
  19. }
  20. }
  21. }
  22. $beforeUpdate() {
  23. this.updatedAt = new Date().toISOString()
  24. }
  25. $beforeInsert() {
  26. this.updatedAt = new Date().toISOString()
  27. }
  28. static async getConfig() {
  29. const settings = await WIKI.models.settings.query()
  30. if (settings.length > 0) {
  31. return _.reduce(settings, (res, val, key) => {
  32. _.set(res, val.key, (val.value.v) ? val.value.v : val.value)
  33. return res
  34. }, {})
  35. } else {
  36. return false
  37. }
  38. }
  39. }