aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/runner/runner.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/models/runner/runner.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/models/runner/runner.ts')
-rw-r--r--server/models/runner/runner.ts124
1 files changed, 0 insertions, 124 deletions
diff --git a/server/models/runner/runner.ts b/server/models/runner/runner.ts
deleted file mode 100644
index 4d07707d8..000000000
--- a/server/models/runner/runner.ts
+++ /dev/null
@@ -1,124 +0,0 @@
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 fields: [ 'name' ],
22 unique: true
23 }
24 ]
25})
26export class RunnerModel extends Model<Partial<AttributesOnly<RunnerModel>>> {
27
28 // Used to identify the appropriate runner when it uses the runner REST API
29 @AllowNull(false)
30 @Column
31 runnerToken: string
32
33 @AllowNull(false)
34 @Column
35 name: string
36
37 @AllowNull(true)
38 @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
39 description: string
40
41 @AllowNull(false)
42 @Column
43 lastContact: Date
44
45 @AllowNull(false)
46 @Column
47 ip: string
48
49 @CreatedAt
50 createdAt: Date
51
52 @UpdatedAt
53 updatedAt: Date
54
55 @ForeignKey(() => RunnerRegistrationTokenModel)
56 @Column
57 runnerRegistrationTokenId: number
58
59 @BelongsTo(() => RunnerRegistrationTokenModel, {
60 foreignKey: {
61 allowNull: false
62 },
63 onDelete: 'cascade'
64 })
65 RunnerRegistrationToken: RunnerRegistrationTokenModel
66
67 // ---------------------------------------------------------------------------
68
69 static load (id: number) {
70 return RunnerModel.findByPk(id)
71 }
72
73 static loadByToken (runnerToken: string) {
74 const query = {
75 where: { runnerToken }
76 }
77
78 return RunnerModel.findOne(query)
79 }
80
81 static loadByName (name: string) {
82 const query = {
83 where: { name }
84 }
85
86 return RunnerModel.findOne(query)
87 }
88
89 static listForApi (options: {
90 start: number
91 count: number
92 sort: string
93 }) {
94 const { start, count, sort } = options
95
96 const query: FindOptions = {
97 offset: start,
98 limit: count,
99 order: getSort(sort)
100 }
101
102 return Promise.all([
103 RunnerModel.count(query),
104 RunnerModel.findAll<MRunner>(query)
105 ]).then(([ total, data ]) => ({ total, data }))
106 }
107
108 // ---------------------------------------------------------------------------
109
110 toFormattedJSON (this: MRunner): Runner {
111 return {
112 id: this.id,
113
114 name: this.name,
115 description: this.description,
116
117 ip: this.ip,
118 lastContact: this.lastContact,
119
120 createdAt: this.createdAt,
121 updatedAt: this.updatedAt
122 }
123 }
124}