category.ts 1.7 KB

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