PeerTube/server/models/job/job.ts

80 lines
1.7 KiB
TypeScript
Raw Normal View History

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