proposal.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const order = [['id', 'ASC']]
  4. const Proposal = db.define('proposal', {
  5. id: {
  6. type: DataTypes.INTEGER,
  7. primaryKey: true,
  8. },
  9. created: DataTypes.INTEGER,
  10. finalizedAt: DataTypes.INTEGER,
  11. title: DataTypes.STRING,
  12. type: DataTypes.STRING,
  13. stage: DataTypes.STRING,
  14. result: DataTypes.STRING,
  15. executed: DataTypes.STRING,
  16. parameters: DataTypes.STRING,
  17. description: DataTypes.TEXT,
  18. })
  19. Proposal.findAllWithIncludes = function () {
  20. return this.findAll({
  21. order,
  22. include: [
  23. { association: 'author', attributes: ['handle'] },
  24. {
  25. association: 'posts',
  26. include: [{ association: 'author', attributes: ['handle'] }],
  27. },
  28. {
  29. association: 'votes',
  30. attributes: ['id', 'vote'],
  31. include: [{ model: db.models.member, attributes: ['id', 'handle'] }],
  32. },
  33. ],
  34. })
  35. }
  36. Proposal.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  37. return this.findByPk(id, {
  38. ...args,
  39. include: [
  40. { association: 'author', attributes: ['handle'] },
  41. {
  42. association: 'posts',
  43. include: [{ association: 'author', attributes: ['handle'] }],
  44. },
  45. {
  46. association: 'votes',
  47. attributes: ['id', 'vote'],
  48. include: [{ model: db.models.member, attributes: ['id', 'handle'] }],
  49. },
  50. ],
  51. })
  52. }
  53. Proposal.findWithIncludes = function (args: { where: any }) {
  54. return this.findAll({
  55. ...args,
  56. order,
  57. include: [
  58. { association: 'author', attributes: ['handle'] },
  59. {
  60. association: 'posts',
  61. include: [{ association: 'author', attributes: ['handle'] }],
  62. },
  63. {
  64. association: 'votes',
  65. attributes: ['id', 'vote'],
  66. include: [{ association: 'author', attributes: ['id', 'handle'] }],
  67. },
  68. ],
  69. })
  70. }
  71. export default Proposal