1 import { FindOptions } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MRunner } from '@server/types/models/runners'
4 import { Runner } from '@shared/models'
5 import { AttributesOnly } from '@shared/typescript-utils'
6 import { getSort } from '../shared'
7 import { RunnerRegistrationTokenModel } from './runner-registration-token'
8 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
14 fields: [ 'runnerToken' ],
18 fields: [ 'runnerRegistrationTokenId' ]
22 export class RunnerModel extends Model<Partial<AttributesOnly<RunnerModel>>> {
24 // Used to identify the appropriate runner when it uses the runner REST API
34 @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
51 @ForeignKey(() => RunnerRegistrationTokenModel)
53 runnerRegistrationTokenId: number
55 @BelongsTo(() => RunnerRegistrationTokenModel, {
61 RunnerRegistrationToken: RunnerRegistrationTokenModel
63 // ---------------------------------------------------------------------------
65 static load (id: number) {
66 return RunnerModel.findByPk(id)
69 static loadByToken (runnerToken: string) {
71 where: { runnerToken }
74 return RunnerModel.findOne(query)
77 static listForApi (options: {
82 const { start, count, sort } = options
84 const query: FindOptions = {
91 RunnerModel.count(query),
92 RunnerModel.findAll<MRunner>(query)
93 ]).then(([ total, data ]) => ({ total, data }))
96 // ---------------------------------------------------------------------------
98 toFormattedJSON (this: MRunner): Runner {
103 description: this.description,
106 lastContact: this.lastContact,
108 createdAt: this.createdAt,
109 updatedAt: this.updatedAt