local-video-viewer-watch-section.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { Transaction } from 'sequelize'
  2. import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table } from 'sequelize-typescript'
  3. import { MLocalVideoViewerWatchSection } from '@server/types/models'
  4. import { AttributesOnly } from '@shared/typescript-utils'
  5. import { LocalVideoViewerModel } from './local-video-viewer'
  6. @Table({
  7. tableName: 'localVideoViewerWatchSection',
  8. updatedAt: false,
  9. indexes: [
  10. {
  11. fields: [ 'localVideoViewerId' ]
  12. }
  13. ]
  14. })
  15. export class LocalVideoViewerWatchSectionModel extends Model<Partial<AttributesOnly<LocalVideoViewerWatchSectionModel>>> {
  16. @CreatedAt
  17. createdAt: Date
  18. @AllowNull(false)
  19. @Column
  20. watchStart: number
  21. @AllowNull(false)
  22. @Column
  23. watchEnd: number
  24. @ForeignKey(() => LocalVideoViewerModel)
  25. @Column
  26. localVideoViewerId: number
  27. @BelongsTo(() => LocalVideoViewerModel, {
  28. foreignKey: {
  29. allowNull: false
  30. },
  31. onDelete: 'CASCADE'
  32. })
  33. LocalVideoViewer: LocalVideoViewerModel
  34. static async bulkCreateSections (options: {
  35. localVideoViewerId: number
  36. watchSections: {
  37. start: number
  38. end: number
  39. }[]
  40. transaction?: Transaction
  41. }) {
  42. const { localVideoViewerId, watchSections, transaction } = options
  43. const models: MLocalVideoViewerWatchSection[] = []
  44. for (const section of watchSections) {
  45. const model = await this.create({
  46. watchStart: section.start,
  47. watchEnd: section.end,
  48. localVideoViewerId
  49. }, { transaction })
  50. models.push(model)
  51. }
  52. return models
  53. }
  54. }