video-view.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table } from 'sequelize-typescript'
  2. import { VideoModel } from './video'
  3. import * as Sequelize from 'sequelize'
  4. @Table({
  5. tableName: 'videoView',
  6. updatedAt: false,
  7. indexes: [
  8. {
  9. fields: [ 'videoId' ]
  10. },
  11. {
  12. fields: [ 'startDate' ]
  13. }
  14. ]
  15. })
  16. export class VideoViewModel extends Model {
  17. @CreatedAt
  18. createdAt: Date
  19. @AllowNull(false)
  20. @Column(Sequelize.DATE)
  21. startDate: Date
  22. @AllowNull(false)
  23. @Column(Sequelize.DATE)
  24. endDate: Date
  25. @AllowNull(false)
  26. @Column
  27. views: number
  28. @ForeignKey(() => VideoModel)
  29. @Column
  30. videoId: number
  31. @BelongsTo(() => VideoModel, {
  32. foreignKey: {
  33. allowNull: false
  34. },
  35. onDelete: 'CASCADE'
  36. })
  37. Video: VideoModel
  38. static removeOldRemoteViewsHistory (beforeDate: string) {
  39. const query = {
  40. where: {
  41. startDate: {
  42. [Sequelize.Op.lt]: beforeDate
  43. },
  44. videoId: {
  45. [Sequelize.Op.in]: Sequelize.literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
  46. }
  47. }
  48. }
  49. return VideoViewModel.destroy(query)
  50. }
  51. }