2023-07-19 10:02:49 -04:00
|
|
|
import { Transaction } from 'sequelize'
|
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
|
|
|
import { VideoSource } from '@shared/models/videos/video-source'
|
2022-06-21 09:31:25 -04:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2023-07-19 10:02:49 -04:00
|
|
|
import { getSort } from '../shared'
|
2022-06-21 09:31:25 -04:00
|
|
|
import { VideoModel } from './video'
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoSource',
|
|
|
|
indexes: [
|
|
|
|
{
|
2023-07-19 10:02:49 -04:00
|
|
|
fields: [ 'videoId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ { name: 'createdAt', order: 'DESC' } ]
|
2022-06-21 09:31:25 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
export class VideoSourceModel extends Model<Partial<AttributesOnly<VideoSourceModel>>> {
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
filename: string
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
2023-07-19 10:02:49 -04:00
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2022-06-21 09:31:25 -04:00
|
|
|
Video: VideoModel
|
|
|
|
|
2023-07-19 10:02:49 -04:00
|
|
|
static loadLatest (videoId: number, transaction?: Transaction) {
|
|
|
|
return VideoSourceModel.findOne({
|
|
|
|
where: { videoId },
|
|
|
|
order: getSort('-createdAt'),
|
|
|
|
transaction
|
|
|
|
})
|
2022-06-21 09:31:25 -04:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:02:49 -04:00
|
|
|
toFormattedJSON (): VideoSource {
|
2022-06-21 09:31:25 -04:00
|
|
|
return {
|
2023-07-19 10:02:49 -04:00
|
|
|
filename: this.filename,
|
|
|
|
createdAt: this.createdAt.toISOString()
|
2022-06-21 09:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|