2018-05-20 22:50:51 +00:00
|
|
|
const Model = require('objection').Model
|
|
|
|
|
2018-07-22 20:25:39 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
/**
|
|
|
|
* Pages model
|
|
|
|
*/
|
|
|
|
module.exports = class Page extends Model {
|
|
|
|
static get tableName() { return 'pages' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['path', 'title'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
path: {type: 'string'},
|
|
|
|
title: {type: 'string'},
|
|
|
|
description: {type: 'string'},
|
|
|
|
isPublished: {type: 'boolean'},
|
|
|
|
publishStartDate: {type: 'string'},
|
|
|
|
publishEndDate: {type: 'string'},
|
|
|
|
content: {type: 'string'},
|
|
|
|
|
|
|
|
createdAt: {type: 'string'},
|
|
|
|
updatedAt: {type: 'string'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get relationMappings() {
|
|
|
|
return {
|
|
|
|
tags: {
|
|
|
|
relation: Model.ManyToManyRelation,
|
|
|
|
modelClass: require('./tags'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.id',
|
|
|
|
through: {
|
|
|
|
from: 'pageTags.pageId',
|
|
|
|
to: 'pageTags.tagId'
|
|
|
|
},
|
|
|
|
to: 'tags.id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./users'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.authorId',
|
|
|
|
to: 'users.id'
|
|
|
|
}
|
|
|
|
},
|
2018-07-22 04:29:39 +00:00
|
|
|
editor: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./editors'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.editorKey',
|
|
|
|
to: 'editors.key'
|
|
|
|
}
|
|
|
|
},
|
2018-05-20 22:50:51 +00:00
|
|
|
locale: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./locales'),
|
|
|
|
join: {
|
2018-07-22 04:29:39 +00:00
|
|
|
from: 'pages.localeCode',
|
2018-05-20 22:50:51 +00:00
|
|
|
to: 'locales.code'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$beforeUpdate() {
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
|
|
|
$beforeInsert() {
|
|
|
|
this.createdAt = new Date().toISOString()
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
2018-07-22 20:25:39 +00:00
|
|
|
|
|
|
|
static async createPage(opts) {
|
|
|
|
const page = await WIKI.db.pages.query().insertAndFetch({
|
|
|
|
authorId: opts.authorId,
|
|
|
|
content: opts.content,
|
|
|
|
description: opts.description,
|
|
|
|
editorKey: opts.editor,
|
|
|
|
isPrivate: opts.isPrivate,
|
|
|
|
isPublished: opts.isPublished,
|
|
|
|
localeCode: opts.locale,
|
|
|
|
path: opts.path,
|
|
|
|
publishEndDate: opts.publishEndDate,
|
|
|
|
publishStartDate: opts.publishStartDate,
|
|
|
|
title: opts.title
|
|
|
|
})
|
|
|
|
await WIKI.db.storage.createPage(page)
|
|
|
|
return page
|
|
|
|
}
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|