post.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. let marked = require('marked')
  2. module.exports = (sequelize, DataTypes) => {
  3. let Post = sequelize.define('Post', {
  4. content: {
  5. type: DataTypes.STRING,
  6. set (val) {
  7. this.setDataValue('content', marked(val))
  8. }
  9. },
  10. replyingToUsername: DataTypes.STRING
  11. }, {
  12. instanceMethods: {
  13. getReplyingTo () {
  14. return Post.findByPrimary(this.replyId)
  15. },
  16. setReplyingTo (post) {
  17. return post.getUser().then(user => {
  18. return this.update({ replyingToUsername: user.username, replyId: post.id })
  19. })
  20. }
  21. },
  22. classMethods: {
  23. associate (models) {
  24. Post.belongsTo(models.User)
  25. Post.belongsTo(models.Thread)
  26. Post.hasMany(models.Post, { as: 'Replies', foreignKey: 'replyId' })
  27. },
  28. includeOptions () {
  29. let models = sequelize.models
  30. return [
  31. { model: models.User, attributes: ['username', 'createdAt', 'id', 'color'] },
  32. { model: models.Thread, include: [models.Category]} ,
  33. {
  34. model: models.Post, as: 'Replies', include:
  35. [{ model: models.User, attributes: ['username', 'id', 'color'] }]
  36. }
  37. ]
  38. }
  39. }
  40. })
  41. return Post
  42. }