pageHistory.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. hash: {type: 'string'},
  16. title: {type: 'string'},
  17. description: {type: 'string'},
  18. isPublished: {type: 'boolean'},
  19. publishStartDate: {type: 'string'},
  20. publishEndDate: {type: 'string'},
  21. content: {type: 'string'},
  22. contentType: {type: 'string'},
  23. createdAt: {type: 'string'}
  24. }
  25. }
  26. }
  27. static get relationMappings() {
  28. return {
  29. tags: {
  30. relation: Model.ManyToManyRelation,
  31. modelClass: require('./tags'),
  32. join: {
  33. from: 'pageHistory.id',
  34. through: {
  35. from: 'pageHistoryTags.pageId',
  36. to: 'pageHistoryTags.tagId'
  37. },
  38. to: 'tags.id'
  39. }
  40. },
  41. page: {
  42. relation: Model.BelongsToOneRelation,
  43. modelClass: require('./pages'),
  44. join: {
  45. from: 'pageHistory.pageId',
  46. to: 'pages.id'
  47. }
  48. },
  49. author: {
  50. relation: Model.BelongsToOneRelation,
  51. modelClass: require('./users'),
  52. join: {
  53. from: 'pageHistory.authorId',
  54. to: 'users.id'
  55. }
  56. },
  57. editor: {
  58. relation: Model.BelongsToOneRelation,
  59. modelClass: require('./editors'),
  60. join: {
  61. from: 'pageHistory.editorKey',
  62. to: 'editors.key'
  63. }
  64. },
  65. locale: {
  66. relation: Model.BelongsToOneRelation,
  67. modelClass: require('./locales'),
  68. join: {
  69. from: 'pageHistory.localeCode',
  70. to: 'locales.code'
  71. }
  72. }
  73. }
  74. }
  75. $beforeInsert() {
  76. this.createdAt = new Date().toISOString()
  77. }
  78. static async addVersion(opts) {
  79. await WIKI.models.pageHistory.query().insert({
  80. pageId: opts.id,
  81. authorId: opts.authorId,
  82. content: opts.content,
  83. contentType: opts.contentType,
  84. description: opts.description,
  85. editorKey: opts.editorKey,
  86. hash: opts.hash,
  87. isPrivate: opts.isPrivate,
  88. isPublished: opts.isPublished,
  89. localeCode: opts.localeCode,
  90. path: opts.path,
  91. publishEndDate: opts.publishEndDate || '',
  92. publishStartDate: opts.publishStartDate || '',
  93. title: opts.title
  94. })
  95. }
  96. }