]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/job/job.ts
Only import bootstrap component that we need
[github/Chocobozzz/PeerTube.git] / server / models / job / job.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
3fd3ab2d
C
2import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { JobCategory, JobState } from '../../../shared/models'
5cd80545 4import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
3fd3ab2d 5import { getSort } from '../utils'
e02643f3 6
3fd3ab2d
C
7@Table({
8 tableName: 'job',
9 indexes: [
227d02fe 10 {
3fd3ab2d 11 fields: [ 'state', 'category' ]
227d02fe 12 }
5cd80545 13 ]
3fd3ab2d
C
14})
15export class JobModel extends Model<JobModel> {
16 @AllowNull(false)
17 @Column(DataType.ENUM(values(JOB_STATES)))
18 state: JobState
e02643f3 19
3fd3ab2d
C
20 @AllowNull(false)
21 @Column(DataType.ENUM(values(JOB_CATEGORIES)))
22 category: JobCategory
227d02fe 23
3fd3ab2d
C
24 @AllowNull(false)
25 @Column
26 handlerName: string
5cd80545 27
3fd3ab2d
C
28 @AllowNull(true)
29 @Column(DataType.JSON)
30 handlerInputData: any
227d02fe 31
3fd3ab2d 32 @CreatedAt
65b3ed25 33 createdAt: Date
3fd3ab2d
C
34
35 @UpdatedAt
65b3ed25 36 updatedAt: Date
3fd3ab2d
C
37
38 static listWithLimitByCategory (limit: number, state: JobState, jobCategory: JobCategory) {
39 const query = {
40 order: [
41 [ 'id', 'ASC' ]
42 ],
43 limit: limit,
44 where: {
45 state,
46 category: jobCategory
47 }
227d02fe 48 }
3fd3ab2d
C
49
50 return JobModel.findAll(query)
227d02fe
C
51 }
52
3fd3ab2d
C
53 static listForApi (start: number, count: number, sort: string) {
54 const query = {
55 offset: start,
56 limit: count,
57 order: [ getSort(sort) ]
58 }
5cd80545 59
3fd3ab2d
C
60 return JobModel.findAndCountAll(query).then(({ rows, count }) => {
61 return {
62 data: rows,
63 total: count
64 }
65 })
5cd80545
C
66 }
67
3fd3ab2d 68 toFormattedJSON () {
5cd80545 69 return {
3fd3ab2d
C
70 id: this.id,
71 state: this.state,
72 category: this.category,
73 handlerName: this.handlerName,
74 handlerInputData: this.handlerInputData,
75 createdAt: this.createdAt,
76 updatedAt: this.updatedAt
5cd80545 77 }
3fd3ab2d 78 }
5cd80545 79}