settings.js 1007 Bytes
Newer Older
1
const Model = require('objection').Model
2 3 4
const _ = require('lodash')

/* global WIKI */
5 6 7 8

/**
 * Settings model
 */
9
module.exports = class Setting extends Model {
10
  static get tableName() { return 'settings' }
11
  static get idColumn() { return 'key' }
12 13 14 15

  static get jsonSchema () {
    return {
      type: 'object',
16
      required: ['key'],
17 18 19 20 21 22 23 24 25

      properties: {
        key: {type: 'string'},
        createdAt: {type: 'string'},
        updatedAt: {type: 'string'}
      }
    }
  }

26 27 28 29
  static get jsonAttributes() {
    return ['value']
  }

30 31 32 33 34 35
  $beforeUpdate() {
    this.updatedAt = new Date().toISOString()
  }
  $beforeInsert() {
    this.updatedAt = new Date().toISOString()
  }
36 37

  static async getConfig() {
38
    const settings = await WIKI.models.settings.query()
39 40
    if (settings.length > 0) {
      return _.reduce(settings, (res, val, key) => {
41
        _.set(res, val.key, (_.has(val.value, 'v')) ? val.value.v : val.value)
42 43 44 45 46 47
        return res
      }, {})
    } else {
      return false
    }
  }
48
}