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.

48 lines
1007 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'],
  14. properties: {
  15. key: {type: 'string'},
  16. createdAt: {type: 'string'},
  17. updatedAt: {type: 'string'}
  18. }
  19. }
  20. }
  21. static get jsonAttributes() {
  22. return ['value']
  23. }
  24. $beforeUpdate() {
  25. this.updatedAt = new Date().toISOString()
  26. }
  27. $beforeInsert() {
  28. this.updatedAt = new Date().toISOString()
  29. }
  30. static async getConfig() {
  31. const settings = await WIKI.models.settings.query()
  32. if (settings.length > 0) {
  33. return _.reduce(settings, (res, val, key) => {
  34. _.set(res, val.key, (_.has(val.value, 'v')) ? val.value.v : val.value)
  35. return res
  36. }, {})
  37. } else {
  38. return false
  39. }
  40. }
  41. }