1
0

member.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const order = [['id', 'ASC']]
  4. const Member = db.define('member', {
  5. id: {
  6. type: DataTypes.INTEGER,
  7. primaryKey: true,
  8. },
  9. created: DataTypes.INTEGER,
  10. handle: DataTypes.STRING,
  11. about: DataTypes.TEXT,
  12. })
  13. Member.findAllWithIncludes = function () {
  14. return this.findAll({order,
  15. include: [
  16. { model: db.models.post, include: [{ model: db.models.thread }] },
  17. { model: db.models.thread, include: [{ model: db.models.category }] },
  18. { model: db.models.proposal, include: [{ association: 'votes' }] },
  19. {
  20. model: db.models.account,
  21. include: [
  22. { association: 'validated', attributes: ['id', 'timestamp'] },
  23. ],
  24. },
  25. {
  26. association: 'terms',
  27. include: [
  28. {
  29. association: 'votes',
  30. include: [
  31. {
  32. model: db.models.proposal,
  33. include: [{ association: 'author' }],
  34. },
  35. ],
  36. },
  37. { association: 'voters', include: [{ model: db.models.member }] },
  38. ],
  39. },
  40. {
  41. association: 'votes',
  42. include: [
  43. {
  44. model: db.models.consul,
  45. include: [{ model: db.models.member }],
  46. },
  47. ],
  48. },
  49. ],
  50. })
  51. }
  52. Member.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  53. return this.findByPk(id, {
  54. ...args,
  55. include: [
  56. { model: db.models.post, include: [{ model: db.models.thread }] },
  57. { model: db.models.proposal, include: [{ association: 'votes' }] },
  58. {
  59. model: db.models.account,
  60. include: [
  61. { association: 'validated', attributes: ['id', 'timestamp'] },
  62. ],
  63. },
  64. {
  65. association: 'terms',
  66. include: [
  67. {
  68. association: 'votes',
  69. include: [
  70. {
  71. model: db.models.proposal,
  72. include: [{ association: 'author' }],
  73. },
  74. ],
  75. },
  76. { association: 'voters', include: [{ model: db.models.member }] },
  77. ],
  78. },
  79. {
  80. association: 'votes',
  81. include: [
  82. {
  83. model: db.models.consul,
  84. include: [{ model: db.models.member }],
  85. },
  86. ],
  87. },
  88. ],
  89. })
  90. }
  91. Member.findWithIncludes = function (args: { where: any }) {
  92. return this.findAll({
  93. ...args,
  94. order,
  95. include: [
  96. { model: db.models.post, include: [{ model: db.models.thread }] },
  97. { model: db.models.proposal, include: [{ association: 'votes' }] },
  98. {
  99. model: db.models.account,
  100. include: [
  101. { association: 'validated', attributes: ['id', 'timestamp'] },
  102. ],
  103. },
  104. {
  105. association: 'terms',
  106. include: [
  107. {
  108. association: 'votes',
  109. include: [
  110. {
  111. model: db.models.proposal,
  112. include: [{ association: 'author' }],
  113. },
  114. ],
  115. },
  116. { association: 'voters', include: [{ model: db.models.member }] },
  117. ],
  118. },
  119. {
  120. association: 'votes',
  121. include: [
  122. {
  123. model: db.models.consul,
  124. include: [{ model: db.models.member }],
  125. },
  126. ],
  127. },
  128. {
  129. model: db.models.account,
  130. include: [
  131. { association: 'validated', attributes: ['id', 'timestamp'] },
  132. ],
  133. },
  134. ],
  135. })
  136. }
  137. export default Member