thread.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. let slug = require('slug')
  2. module.exports = (sequelize, DataTypes) => {
  3. let Thread = sequelize.define('Thread', {
  4. name: {
  5. type: DataTypes.TEXT,
  6. set (val) {
  7. this.setDataValue('name', val)
  8. this.setDataValue('slug', slug(val).toLowerCase())
  9. }
  10. },
  11. slug: DataTypes.TEXT,
  12. postsCount: {
  13. type: DataTypes.INTEGER,
  14. defaultValue: 0
  15. },
  16. locked: {
  17. type: DataTypes.BOOLEAN,
  18. defaultValue: false
  19. }
  20. }, {
  21. classMethods: {
  22. associate (models) {
  23. Thread.belongsTo(models.User)
  24. Thread.belongsTo(models.Category)
  25. Thread.hasMany(models.Post)
  26. },
  27. includeOptions (from, limit) {
  28. let models = sequelize.models
  29. return [
  30. { model: models.User, attributes: ['username', 'createdAt', 'color', 'updatedAt', 'id'] },
  31. models.Category,
  32. {
  33. model: models.Post,
  34. where: { postNumber: { $gte: from } },
  35. order: [['id', 'ASC']],
  36. limit,
  37. include: [
  38. { model: models.Thread, attributes: ['slug'] },
  39. { model: models.User, as: 'Likes', attributes: ['username', 'createdAt', 'id', 'color'] },
  40. { model: models.User, attributes: ['username', 'createdAt', 'id', 'color'] },
  41. {
  42. model: models.Post, as: 'Replies', include:
  43. [{ model: models.User, attributes: ['username', 'id', 'color'] }]
  44. }
  45. ]
  46. }
  47. ]
  48. }
  49. }
  50. })
  51. return Thread
  52. }