PeerTube/server/models/job/job.ts

80 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-05-15 15:22:03 -05:00
import { values } from 'lodash'
2017-12-12 10:53:50 -06:00
import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { JobCategory, JobState } from '../../../shared/models'
2017-11-30 03:51:13 -06:00
import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
2017-12-12 10:53:50 -06:00
import { getSort } from '../utils'
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@Table({
tableName: 'job',
indexes: [
{
2017-12-12 10:53:50 -06:00
fields: [ 'state', 'category' ]
}
2017-11-30 03:51:13 -06:00
]
2017-12-12 10:53:50 -06:00
})
export class JobModel extends Model<JobModel> {
@AllowNull(false)
@Column(DataType.ENUM(values(JOB_STATES)))
state: JobState
2017-05-22 13:58:25 -05:00
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column(DataType.ENUM(values(JOB_CATEGORIES)))
category: JobCategory
2017-12-12 10:53:50 -06:00
@AllowNull(false)
@Column
handlerName: string
2017-11-30 03:51:13 -06:00
2017-12-12 10:53:50 -06:00
@AllowNull(true)
@Column(DataType.JSON)
handlerInputData: any
2017-12-12 10:53:50 -06:00
@CreatedAt
2017-12-13 10:00:01 -06:00
createdAt: Date
2017-12-12 10:53:50 -06:00
@UpdatedAt
2017-12-13 10:00:01 -06:00
updatedAt: Date
2017-12-12 10:53:50 -06:00
static listWithLimitByCategory (limit: number, state: JobState, jobCategory: JobCategory) {
const query = {
order: [
[ 'id', 'ASC' ]
],
limit: limit,
where: {
state,
category: jobCategory
}
}
2017-12-12 10:53:50 -06:00
return JobModel.findAll(query)
}
2017-12-12 10:53:50 -06:00
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: [ getSort(sort) ]
}
2017-11-30 03:51:13 -06:00
2017-12-12 10:53:50 -06:00
return JobModel.findAndCountAll(query).then(({ rows, count }) => {
return {
data: rows,
total: count
}
})
2017-11-30 03:51:13 -06:00
}
2017-12-12 10:53:50 -06:00
toFormattedJSON () {
2017-11-30 03:51:13 -06:00
return {
2017-12-12 10:53:50 -06:00
id: this.id,
state: this.state,
category: this.category,
handlerName: this.handlerName,
handlerInputData: this.handlerInputData,
createdAt: this.createdAt,
updatedAt: this.updatedAt
2017-11-30 03:51:13 -06:00
}
2017-12-12 10:53:50 -06:00
}
2017-11-30 03:51:13 -06:00
}