PeerTube/server/core/models/video/video-source.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-07-19 09:02:49 -05:00
import { Transaction } from 'sequelize'
2024-02-22 03:12:04 -06:00
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoSource } from '@peertube/peertube-models'
2024-02-22 03:12:04 -06:00
import { SequelizeModel, getSort } from '../shared/index.js'
import { VideoModel } from './video.js'
@Table({
tableName: 'videoSource',
indexes: [
{
2023-07-19 09:02:49 -05:00
fields: [ 'videoId' ]
},
{
fields: [ { name: 'createdAt', order: 'DESC' } ]
}
]
})
2024-02-22 03:12:04 -06:00
export class VideoSourceModel extends SequelizeModel<VideoSourceModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column
filename: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
2023-07-19 09:02:49 -05:00
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
2023-07-19 09:02:49 -05:00
static loadLatest (videoId: number, transaction?: Transaction) {
return VideoSourceModel.findOne({
where: { videoId },
order: getSort('-createdAt'),
transaction
})
}
2023-07-19 09:02:49 -05:00
toFormattedJSON (): VideoSource {
return {
2023-07-19 09:02:49 -05:00
filename: this.filename,
createdAt: this.createdAt.toISOString()
}
}
}