thread.ts 1.5 KB

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