post.ts 1.0 KB

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