comments.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const Model = require('objection').Model
  2. /**
  3. * Comments model
  4. */
  5. module.exports = class Comment extends Model {
  6. static get tableName() { return 'comments' }
  7. static get jsonSchema () {
  8. return {
  9. type: 'object',
  10. required: [],
  11. properties: {
  12. id: {type: 'integer'},
  13. content: {type: 'string'},
  14. render: {type: 'string'},
  15. name: {type: 'string'},
  16. email: {type: 'string'},
  17. ip: {type: 'string'},
  18. createdAt: {type: 'string'},
  19. updatedAt: {type: 'string'}
  20. }
  21. }
  22. }
  23. static get relationMappings() {
  24. return {
  25. author: {
  26. relation: Model.BelongsToOneRelation,
  27. modelClass: require('./users'),
  28. join: {
  29. from: 'comments.authorId',
  30. to: 'users.id'
  31. }
  32. },
  33. page: {
  34. relation: Model.BelongsToOneRelation,
  35. modelClass: require('./pages'),
  36. join: {
  37. from: 'comments.pageId',
  38. to: 'pages.id'
  39. }
  40. }
  41. }
  42. }
  43. $beforeUpdate() {
  44. this.updatedAt = new Date().toISOString()
  45. }
  46. $beforeInsert() {
  47. this.createdAt = new Date().toISOString()
  48. this.updatedAt = new Date().toISOString()
  49. }
  50. }