wikijs-fork/server/models/settings.js

49 lines
1007 B
JavaScript
Raw Normal View History

const Model = require('objection').Model
2018-05-28 14:46:55 -04:00
const _ = require('lodash')
/* global WIKI */
/**
* Settings model
*/
2018-05-28 14:46:55 -04:00
module.exports = class Setting extends Model {
static get tableName() { return 'settings' }
2018-09-08 15:49:36 -04:00
static get idColumn() { return 'key' }
static get jsonSchema () {
return {
type: 'object',
2019-02-09 19:10:34 -05:00
required: ['key'],
properties: {
key: {type: 'string'},
createdAt: {type: 'string'},
updatedAt: {type: 'string'}
}
}
}
2019-02-09 19:10:34 -05:00
static get jsonAttributes() {
return ['value']
}
$beforeUpdate() {
this.updatedAt = new Date().toISOString()
}
$beforeInsert() {
this.updatedAt = new Date().toISOString()
}
2018-05-28 14:46:55 -04:00
static async getConfig() {
const settings = await WIKI.models.settings.query()
2018-05-28 14:46:55 -04:00
if (settings.length > 0) {
return _.reduce(settings, (res, val, key) => {
2018-12-24 01:03:10 -05:00
_.set(res, val.key, (_.has(val.value, 'v')) ? val.value.v : val.value)
2018-05-28 14:46:55 -04:00
return res
}, {})
} else {
return false
}
}
}