pages.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const Model = require('objection').Model
  2. /**
  3. * Pages model
  4. */
  5. module.exports = class Page extends Model {
  6. static get tableName() { return 'pages' }
  7. static get jsonSchema () {
  8. return {
  9. type: 'object',
  10. required: ['path', 'title'],
  11. properties: {
  12. id: {type: 'integer'},
  13. path: {type: 'string'},
  14. title: {type: 'string'},
  15. description: {type: 'string'},
  16. isPublished: {type: 'boolean'},
  17. publishStartDate: {type: 'string'},
  18. publishEndDate: {type: 'string'},
  19. content: {type: 'string'},
  20. createdAt: {type: 'string'},
  21. updatedAt: {type: 'string'}
  22. }
  23. }
  24. }
  25. static get relationMappings() {
  26. return {
  27. tags: {
  28. relation: Model.ManyToManyRelation,
  29. modelClass: require('./tags'),
  30. join: {
  31. from: 'pages.id',
  32. through: {
  33. from: 'pageTags.pageId',
  34. to: 'pageTags.tagId'
  35. },
  36. to: 'tags.id'
  37. }
  38. },
  39. author: {
  40. relation: Model.BelongsToOneRelation,
  41. modelClass: require('./users'),
  42. join: {
  43. from: 'pages.authorId',
  44. to: 'users.id'
  45. }
  46. },
  47. editor: {
  48. relation: Model.BelongsToOneRelation,
  49. modelClass: require('./editors'),
  50. join: {
  51. from: 'pages.editorKey',
  52. to: 'editors.key'
  53. }
  54. },
  55. locale: {
  56. relation: Model.BelongsToOneRelation,
  57. modelClass: require('./locales'),
  58. join: {
  59. from: 'pages.localeCode',
  60. to: 'locales.code'
  61. }
  62. }
  63. }
  64. }
  65. $beforeUpdate() {
  66. this.updatedAt = new Date().toISOString()
  67. }
  68. $beforeInsert() {
  69. this.createdAt = new Date().toISOString()
  70. this.updatedAt = new Date().toISOString()
  71. }
  72. }