category.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: 'author' },
  23. { association: 'moderator' },
  24. ],
  25. },
  26. { association: 'moderator' },
  27. ],
  28. })
  29. }
  30. Category.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  31. return this.findByPk(id, {
  32. ...args,
  33. include: [
  34. {
  35. model: db.models.thread,
  36. include: [
  37. { model: db.models.post, include: [{ association: 'author' }] },
  38. { association: 'author' },
  39. { association: 'moderator' },
  40. ],
  41. },
  42. { association: 'moderator' },
  43. ],
  44. })
  45. }
  46. Category.findWithIncludes = function (args?: { where: any }) {
  47. return this.findAll({
  48. ...args,
  49. include: [
  50. {
  51. model: db.models.thread,
  52. include: [
  53. { model: db.models.post, include: [{ association: 'author' }] },
  54. { association: 'author' },
  55. { association: 'moderator' },
  56. ],
  57. },
  58. { association: 'moderator' },
  59. ],
  60. })
  61. }
  62. export default Category