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.

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