aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/runner/runner.ts
blob: 4d07707d8e13de283f211dea0a9297fa424567c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { FindOptions } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { MRunner } from '@server/types/models/runners'
import { Runner } from '@shared/models'
import { AttributesOnly } from '@shared/typescript-utils'
import { getSort } from '../shared'
import { RunnerRegistrationTokenModel } from './runner-registration-token'
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'

@Table({
  tableName: 'runner',
  indexes: [
    {
      fields: [ 'runnerToken' ],
      unique: true
    },
    {
      fields: [ 'runnerRegistrationTokenId' ]
    },
    {
      fields: [ 'name' ],
      unique: true
    }
  ]
})
export class RunnerModel extends Model<Partial<AttributesOnly<RunnerModel>>> {

  // Used to identify the appropriate runner when it uses the runner REST API
  @AllowNull(false)
  @Column
  runnerToken: string

  @AllowNull(false)
  @Column
  name: string

  @AllowNull(true)
  @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
  description: string

  @AllowNull(false)
  @Column
  lastContact: Date

  @AllowNull(false)
  @Column
  ip: string

  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  @ForeignKey(() => RunnerRegistrationTokenModel)
  @Column
  runnerRegistrationTokenId: number

  @BelongsTo(() => RunnerRegistrationTokenModel, {
    foreignKey: {
      allowNull: false
    },
    onDelete: 'cascade'
  })
  RunnerRegistrationToken: RunnerRegistrationTokenModel

  // ---------------------------------------------------------------------------

  static load (id: number) {
    return RunnerModel.findByPk(id)
  }

  static loadByToken (runnerToken: string) {
    const query = {
      where: { runnerToken }
    }

    return RunnerModel.findOne(query)
  }

  static loadByName (name: string) {
    const query = {
      where: { name }
    }

    return RunnerModel.findOne(query)
  }

  static listForApi (options: {
    start: number
    count: number
    sort: string
  }) {
    const { start, count, sort } = options

    const query: FindOptions = {
      offset: start,
      limit: count,
      order: getSort(sort)
    }

    return Promise.all([
      RunnerModel.count(query),
      RunnerModel.findAll<MRunner>(query)
    ]).then(([ total, data ]) => ({ total, data }))
  }

  // ---------------------------------------------------------------------------

  toFormattedJSON (this: MRunner): Runner {
    return {
      id: this.id,

      name: this.name,
      description: this.description,

      ip: this.ip,
      lastContact: this.lastContact,

      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    }
  }
}