aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/runner/runner.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/runner/runner.ts')
-rw-r--r--server/models/runner/runner.ts112
1 files changed, 112 insertions, 0 deletions
diff --git a/server/models/runner/runner.ts b/server/models/runner/runner.ts
new file mode 100644
index 000000000..1ef0018b4
--- /dev/null
+++ b/server/models/runner/runner.ts
@@ -0,0 +1,112 @@
1import { FindOptions } from 'sequelize'
2import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { MRunner } from '@server/types/models/runners'
4import { Runner } from '@shared/models'
5import { AttributesOnly } from '@shared/typescript-utils'
6import { getSort } from '../shared'
7import { RunnerRegistrationTokenModel } from './runner-registration-token'
8import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
9
10@Table({
11 tableName: 'runner',
12 indexes: [
13 {
14 fields: [ 'runnerToken' ],
15 unique: true
16 },
17 {
18 fields: [ 'runnerRegistrationTokenId' ]
19 }
20 ]
21})
22export class RunnerModel extends Model<Partial<AttributesOnly<RunnerModel>>> {
23
24 // Used to identify the appropriate runner when it uses the runner REST API
25 @AllowNull(false)
26 @Column
27 runnerToken: string
28
29 @AllowNull(false)
30 @Column
31 name: string
32
33 @AllowNull(true)
34 @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
35 description: string
36
37 @AllowNull(false)
38 @Column
39 lastContact: Date
40
41 @AllowNull(false)
42 @Column
43 ip: string
44
45 @CreatedAt
46 createdAt: Date
47
48 @UpdatedAt
49 updatedAt: Date
50
51 @ForeignKey(() => RunnerRegistrationTokenModel)
52 @Column
53 runnerRegistrationTokenId: number
54
55 @BelongsTo(() => RunnerRegistrationTokenModel, {
56 foreignKey: {
57 allowNull: false
58 },
59 onDelete: 'cascade'
60 })
61 RunnerRegistrationToken: RunnerRegistrationTokenModel
62
63 // ---------------------------------------------------------------------------
64
65 static load (id: number) {
66 return RunnerModel.findByPk(id)
67 }
68
69 static loadByToken (runnerToken: string) {
70 const query = {
71 where: { runnerToken }
72 }
73
74 return RunnerModel.findOne(query)
75 }
76
77 static listForApi (options: {
78 start: number
79 count: number
80 sort: string
81 }) {
82 const { start, count, sort } = options
83
84 const query: FindOptions = {
85 offset: start,
86 limit: count,
87 order: getSort(sort)
88 }
89
90 return Promise.all([
91 RunnerModel.count(query),
92 RunnerModel.findAll<MRunner>(query)
93 ]).then(([ total, data ]) => ({ total, data }))
94 }
95
96 // ---------------------------------------------------------------------------
97
98 toFormattedJSON (this: MRunner): Runner {
99 return {
100 id: this.id,
101
102 name: this.name,
103 description: this.description,
104
105 ip: this.ip,
106 lastContact: this.lastContact,
107
108 createdAt: this.createdAt,
109 updatedAt: this.updatedAt
110 }
111 }
112}