Settings resource; oauth2 app authorization model; UI cleanup
This commit is contained in:
56
app/models/Setting.model.js
Normal file
56
app/models/Setting.model.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const { Model } = require('flitter-orm')
|
||||
|
||||
class SettingModel extends Model {
|
||||
static get services() {
|
||||
return [...super.services, 'utility']
|
||||
}
|
||||
|
||||
static get schema() {
|
||||
return {
|
||||
key: String,
|
||||
value: String,
|
||||
history: [String],
|
||||
}
|
||||
}
|
||||
|
||||
static async guarantee(key, value = '') {
|
||||
if ( !(await this.findOne({ key })) ) {
|
||||
const new_inst = new this({ key })
|
||||
new_inst.set(value)
|
||||
await new_inst.save()
|
||||
}
|
||||
}
|
||||
|
||||
static async get(key) {
|
||||
const inst = await this.findOne({ key })
|
||||
return inst.get()
|
||||
}
|
||||
|
||||
static async set(key, value) {
|
||||
const inst = await this.findOne({ key })
|
||||
inst.set(value)
|
||||
await inst.save()
|
||||
}
|
||||
|
||||
get() {
|
||||
return JSON.parse(this.value)
|
||||
}
|
||||
|
||||
set(value) {
|
||||
if ( Array.isArray(this.history) )
|
||||
this.history.push(this.value)
|
||||
|
||||
this.value = JSON.stringify(value)
|
||||
}
|
||||
|
||||
async to_api() {
|
||||
return {
|
||||
id: this.key,
|
||||
key: this.key,
|
||||
value: this.get(),
|
||||
history: this.history || [],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = SettingModel
|
||||
Reference in New Issue
Block a user