You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.4 KiB

  1. const Model = require('objection').Model
  2. /* global WIKI */
  3. /**
  4. * Page History model
  5. */
  6. module.exports = class PageHistory extends Model {
  7. static get tableName() { return 'pageHistory' }
  8. static get jsonSchema () {
  9. return {
  10. type: 'object',
  11. required: ['path', 'title'],
  12. properties: {
  13. id: {type: 'integer'},
  14. path: {type: 'string'},
  15. title: {type: 'string'},
  16. description: {type: 'string'},
  17. isPublished: {type: 'boolean'},
  18. publishStartDate: {type: 'string'},
  19. publishEndDate: {type: 'string'},
  20. content: {type: 'string'},
  21. createdAt: {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: 'pageHistory.id',
  32. through: {
  33. from: 'pageHistoryTags.pageId',
  34. to: 'pageHistoryTags.tagId'
  35. },
  36. to: 'tags.id'
  37. }
  38. },
  39. page: {
  40. relation: Model.BelongsToOneRelation,
  41. modelClass: require('./pages'),
  42. join: {
  43. from: 'pageHistory.pageId',
  44. to: 'pages.id'
  45. }
  46. },
  47. author: {
  48. relation: Model.BelongsToOneRelation,
  49. modelClass: require('./users'),
  50. join: {
  51. from: 'pageHistory.authorId',
  52. to: 'users.id'
  53. }
  54. },
  55. editor: {
  56. relation: Model.BelongsToOneRelation,
  57. modelClass: require('./editors'),
  58. join: {
  59. from: 'pageHistory.editorKey',
  60. to: 'editors.key'
  61. }
  62. },
  63. locale: {
  64. relation: Model.BelongsToOneRelation,
  65. modelClass: require('./locales'),
  66. join: {
  67. from: 'pageHistory.localeCode',
  68. to: 'locales.code'
  69. }
  70. }
  71. }
  72. }
  73. $beforeInsert() {
  74. this.createdAt = new Date().toISOString()
  75. }
  76. static async addVersion(opts) {
  77. await WIKI.models.pageHistory.query().insert({
  78. pageId: opts.id,
  79. authorId: opts.authorId,
  80. content: opts.content,
  81. description: opts.description,
  82. editorKey: opts.editorKey,
  83. isPrivate: opts.isPrivate,
  84. isPublished: opts.isPublished,
  85. localeCode: opts.localeCode,
  86. path: opts.path,
  87. publishEndDate: opts.publishEndDate,
  88. publishStartDate: opts.publishStartDate,
  89. title: opts.title
  90. })
  91. }
  92. }