aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-source.ts
blob: 1b6868b857d3106027610ef51d2dcf43b242436b (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
import { Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoSource } from '@shared/models/videos/video-source'
import { AttributesOnly } from '@shared/typescript-utils'
import { getSort } from '../shared'
import { VideoModel } from './video'

@Table({
  tableName: 'videoSource',
  indexes: [
    {
      fields: [ 'videoId' ]
    },
    {
      fields: [ { name: 'createdAt', order: 'DESC' } ]
    }
  ]
})
export class VideoSourceModel extends Model<Partial<AttributesOnly<VideoSourceModel>>> {
  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  @AllowNull(false)
  @Column
  filename: string

  @ForeignKey(() => VideoModel)
  @Column
  videoId: number

  @BelongsTo(() => VideoModel, {
    foreignKey: {
      allowNull: false
    },
    onDelete: 'cascade'
  })
  Video: VideoModel

  static loadLatest (videoId: number, transaction?: Transaction) {
    return VideoSourceModel.findOne({
      where: { videoId },
      order: getSort('-createdAt'),
      transaction
    })
  }

  toFormattedJSON (): VideoSource {
    return {
      filename: this.filename,
      createdAt: this.createdAt.toISOString()
    }
  }
}