wikijs-fork/server/models/settings.js

46 lines
991 B
JavaScript
Raw Normal View History

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