1
0

post.ts 1013 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import db from '../db'
  2. import { DataTypes } from 'sequelize'
  3. const Post = db.define('post', {
  4. id: {
  5. type: DataTypes.INTEGER,
  6. primaryKey: true,
  7. },
  8. text: DataTypes.TEXT,
  9. createdAt: DataTypes.INTEGER,
  10. })
  11. Post.findAllWithIncludes = function () {
  12. return this.findAll({
  13. include: [
  14. { model: db.models.thread, include: [{ model: db.models.category }] },
  15. { association: 'author' },
  16. { association: 'moderator' },
  17. ],
  18. })
  19. }
  20. Post.findByIdWithIncludes = function (id: number, args?: { where: any }) {
  21. return this.findByPk(id, {
  22. ...args,
  23. include: [
  24. { model: db.models.thread, include: [{ model: db.models.category }] },
  25. { association: 'moderator' },
  26. ],
  27. })
  28. }
  29. Post.findWithIncludes = function (args: { where: any }) {
  30. return this.findAll({
  31. ...args,
  32. include: [
  33. { model: db.models.thread, include: [{ model: db.models.category }] },
  34. { model: db.models.thread },
  35. { association: 'moderator' },
  36. ],
  37. })
  38. }
  39. export default Post