thread.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const Thread = db.define('thread', {
  4. id: {
  5. type: DataTypes.INTEGER,
  6. primaryKey: true,
  7. },
  8. createdAt: DataTypes.INTEGER,
  9. title: DataTypes.TEXT,
  10. nrInCategory: DataTypes.INTEGER,
  11. })
  12. Thread.findAllWithIncludes = function () {
  13. return this.findAll({
  14. include: [
  15. { model: db.models.category },
  16. { model: db.models.post, include: [{ association: 'author' }] },
  17. { association: 'creator' },
  18. { association: 'moderator' },
  19. ],
  20. })
  21. }
  22. Thread.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  23. return this.findByPk(id, {
  24. ...args,
  25. include: [
  26. { model: db.models.category },
  27. { model: db.models.post, include: [{ association: 'author' }] },
  28. { association: 'creator' },
  29. { association: 'moderator' },
  30. ],
  31. })
  32. }
  33. Thread.findWithIncludes = function (args?: { where: any }) {
  34. return this.findAll({
  35. ...args,
  36. include: [
  37. { model: db.models.category },
  38. { model: db.models.post, include: [{ association: 'author' }] },
  39. { association: 'creator' },
  40. { association: 'moderator' },
  41. ],
  42. })
  43. }
  44. export default Thread