aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/view/local-video-viewer-watch-section.ts
blob: e29bb78478e4f05d9ec6be038ec86f5a65c61d76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table } from 'sequelize-typescript'
import { MLocalVideoViewerWatchSection } from '@server/types/models'
import { AttributesOnly } from '@shared/typescript-utils'
import { LocalVideoViewerModel } from './local-video-viewer'

@Table({
  tableName: 'localVideoViewerWatchSection',
  updatedAt: false,
  indexes: [
    {
      fields: [ 'localVideoViewerId' ]
    }
  ]
})
export class LocalVideoViewerWatchSectionModel extends Model<Partial<AttributesOnly<LocalVideoViewerWatchSectionModel>>> {
  @CreatedAt
  createdAt: Date

  @AllowNull(false)
  @Column
  watchStart: number

  @AllowNull(false)
  @Column
  watchEnd: number

  @ForeignKey(() => LocalVideoViewerModel)
  @Column
  localVideoViewerId: number

  @BelongsTo(() => LocalVideoViewerModel, {
    foreignKey: {
      allowNull: false
    },
    onDelete: 'CASCADE'
  })
  LocalVideoViewer: LocalVideoViewerModel

  static async bulkCreateSections (options: {
    localVideoViewerId: number
    watchSections: {
      start: number
      end: number
    }[]
    transaction?: Transaction
  }) {
    const { localVideoViewerId, watchSections, transaction } = options
    const models: MLocalVideoViewerWatchSection[] = []

    for (const section of watchSections) {
      const model = await this.create({
        watchStart: section.start,
        watchEnd: section.end,
        localVideoViewerId
      }, { transaction })

      models.push(model)
    }

    return models
  }
}