wikijs-fork/server/models/settings.js

49 lines
1007 B
JavaScript
Raw Permalink 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',
2019-02-10 00:10:34 +00:00
required: ['key'],
properties: {
key: {type: 'string'},
createdAt: {type: 'string'},
updatedAt: {type: 'string'}
}
}
}
2019-02-10 00:10:34 +00:00
static get jsonAttributes() {
return ['value']
}
$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
}
}
}