1 import { Op, QueryTypes, Transaction } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, IsInt, Model, Table, Unique, UpdatedAt } from 'sequelize-typescript'
3 import { AttributesOnly } from '@shared/core-utils'
4 import { VideoModel } from './video'
7 tableName: 'videoJobInfo',
10 fields: [ 'videoId' ],
20 export class VideoJobInfoModel extends Model<Partial<AttributesOnly<VideoJobInfoModel>>> {
37 pendingTranscode: number
39 @ForeignKey(() => VideoModel)
44 @BelongsTo(() => VideoModel, {
52 static load (videoId: number, transaction: Transaction) {
57 return VideoJobInfoModel.findOne({ where, transaction })
60 static async increaseOrCreate (videoUUID: string, column: 'pendingMove' | 'pendingTranscode'): Promise<number> {
61 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } }
63 const [ { pendingMove } ] = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(`
64 INSERT INTO "videoJobInfo" ("videoId", "${column}", "createdAt", "updatedAt")
66 "video"."id" AS "videoId", 1, NOW(), NOW()
70 "video"."uuid" = $videoUUID
71 ON CONFLICT ("videoId") DO UPDATE
73 "${column}" = "videoJobInfo"."${column}" + 1,
82 static async decrease (videoUUID: string, column: 'pendingMove' | 'pendingTranscode'): Promise<number> {
83 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } }
85 const [ { pendingMove } ] = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(`
89 "${column}" = "videoJobInfo"."${column}" - 1,
93 "video"."id" = "videoJobInfo"."videoId" AND "video"."uuid" = $videoUUID