1 import { FindOptions, literal } from 'sequelize'
2 import { AllowNull, Column, CreatedAt, HasMany, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MRunnerRegistrationToken } from '@server/types/models/runners'
4 import { RunnerRegistrationToken } from '@shared/models'
5 import { AttributesOnly } from '@shared/typescript-utils'
6 import { getSort } from '../shared'
7 import { RunnerModel } from './runner'
11 * Tokens used by PeerTube runners to register themselves to the PeerTube instance
16 tableName: 'runnerRegistrationToken',
19 fields: [ 'registrationToken' ],
24 export class RunnerRegistrationTokenModel extends Model<Partial<AttributesOnly<RunnerRegistrationTokenModel>>> {
28 registrationToken: string
36 @HasMany(() => RunnerModel, {
42 Runners: RunnerModel[]
44 static load (id: number) {
45 return RunnerRegistrationTokenModel.findByPk(id)
48 static loadByRegistrationToken (registrationToken: string) {
50 where: { registrationToken }
53 return RunnerRegistrationTokenModel.findOne(query)
56 static countTotal () {
57 return RunnerRegistrationTokenModel.unscoped().count()
60 static listForApi (options: {
65 const { start, count, sort } = options
67 const query: FindOptions = {
71 literal('(SELECT COUNT(*) FROM "runner" WHERE "runner"."runnerRegistrationTokenId" = "RunnerRegistrationTokenModel"."id")'),
72 'registeredRunnersCount'
82 RunnerRegistrationTokenModel.count(query),
83 RunnerRegistrationTokenModel.findAll<MRunnerRegistrationToken>(query)
84 ]).then(([ total, data ]) => ({ total, data }))
87 // ---------------------------------------------------------------------------
89 toFormattedJSON (this: MRunnerRegistrationToken): RunnerRegistrationToken {
90 const registeredRunnersCount = this.get('registeredRunnersCount') as number
95 registrationToken: this.registrationToken,
97 createdAt: this.createdAt,
98 updatedAt: this.updatedAt,
100 registeredRunnersCount