refactor: knex remaining models
This commit is contained in:
70
server/db/models/pages.js
Normal file
70
server/db/models/pages.js
Normal file
@@ -0,0 +1,70 @@
|
||||
const Model = require('objection').Model
|
||||
|
||||
/**
|
||||
* 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'
|
||||
}
|
||||
},
|
||||
locale: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: require('./locales'),
|
||||
join: {
|
||||
from: 'users.locale',
|
||||
to: 'locales.code'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$beforeUpdate() {
|
||||
this.updatedAt = new Date().toISOString()
|
||||
}
|
||||
$beforeInsert() {
|
||||
this.createdAt = new Date().toISOString()
|
||||
this.updatedAt = new Date().toISOString()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user