3
1

member.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. handle: DataTypes.STRING,
  10. about: DataTypes.TEXT,
  11. })
  12. Member.findAllWithIncludes = function () {
  13. return this.findAll({
  14. include: [
  15. { model: db.models.post, include: [{ model: db.models.thread }] },
  16. { model: db.models.thread, include: [{ model: db.models.category }] },
  17. { model: db.models.proposal, include: [{ association: 'votes' }] },
  18. {
  19. model: db.models.account,
  20. include: [
  21. { association: 'validated', attributes: ['id', 'timestamp'] },
  22. ],
  23. },
  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. include: [
  55. { model: db.models.post, include: [{ model: db.models.thread }] },
  56. { model: db.models.proposal, include: [{ association: 'votes' }] },
  57. {
  58. model: db.models.account,
  59. include: [
  60. { association: 'validated', attributes: ['id', 'timestamp'] },
  61. ],
  62. },
  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. Member.findWithIncludes = function (args: { where: any }) {
  91. return this.findAll({
  92. ...args,
  93. include: [
  94. { model: db.models.post, include: [{ model: db.models.thread }] },
  95. { model: db.models.proposal, include: [{ association: 'votes' }] },
  96. {
  97. model: db.models.account,
  98. include: [
  99. { association: 'validated', attributes: ['id', 'timestamp'] },
  100. ],
  101. },
  102. {
  103. association: 'terms',
  104. include: [
  105. {
  106. association: 'votes',
  107. include: [
  108. {
  109. model: db.models.proposal,
  110. include: [{ association: 'author' }],
  111. },
  112. ],
  113. },
  114. { association: 'voters', include: [{ model: db.models.member }] },
  115. ],
  116. },
  117. {
  118. association: 'votes',
  119. include: [
  120. {
  121. model: db.models.consul,
  122. include: [{ model: db.models.member }],
  123. },
  124. ],
  125. },
  126. {
  127. model: db.models.account,
  128. include: [
  129. { association: 'validated', attributes: ['id', 'timestamp'] },
  130. ],
  131. },
  132. ],
  133. })
  134. }
  135. export default Member