member.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const Member = db.define('member', {
  4. id: {
  5. type: DataTypes.INTEGER,
  6. primaryKey: true,
  7. },
  8. createdAt: DataTypes.INTEGER,
  9. account: DataTypes.STRING,
  10. handle: DataTypes.STRING,
  11. about: DataTypes.TEXT,
  12. })
  13. Member.findAllWithIncludes = function () {
  14. return this.findAll({
  15. include: [
  16. {
  17. model: db.models.post,
  18. required: false,
  19. include: [{ model: db.models.thread }],
  20. },
  21. { model: db.models.thread, include: [{ model: db.models.category }] },
  22. { model: db.models.proposal, include: [{ association: 'votes' }] },
  23. { model: db.models.account },
  24. {
  25. association: 'terms',
  26. include: [
  27. {
  28. association: 'votes',
  29. include: [
  30. {
  31. model: db.models.proposal,
  32. include: [{ association: 'author' }],
  33. },
  34. ],
  35. },
  36. { association: 'voters', include: [{ model: db.models.member }] },
  37. ],
  38. },
  39. {
  40. association: 'votes',
  41. include: [
  42. {
  43. model: db.models.consul,
  44. include: [{ model: db.models.member }],
  45. },
  46. ],
  47. },
  48. ],
  49. })
  50. }
  51. Member.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  52. return this.findByPk(id, {
  53. ...args,
  54. })
  55. }
  56. Member.findWithIncludes = function (args: { where: any }) {
  57. return this.findAll({
  58. ...args,
  59. include: [
  60. { model: db.models.post, include: [{ model: db.models.thread }] },
  61. { model: db.models.proposal, include: [{ association: 'votes' }] },
  62. { model: db.models.account },
  63. {
  64. association: 'terms',
  65. include: [
  66. {
  67. association: 'votes',
  68. include: [
  69. {
  70. model: db.models.proposal,
  71. include: [{ association: 'author' }],
  72. },
  73. ],
  74. },
  75. { association: 'voters', include: [{ model: db.models.member }] },
  76. ],
  77. },
  78. {
  79. association: 'votes',
  80. include: [
  81. {
  82. model: db.models.consul,
  83. include: [{ model: db.models.member }],
  84. },
  85. ],
  86. },
  87. ],
  88. })
  89. }
  90. export default Member