PeerTube/server/models/job/job.ts

68 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-05-15 22:22:03 +02:00
import { values } from 'lodash'
2017-05-22 20:58:25 +02:00
import * as Sequelize from 'sequelize'
2017-11-09 17:51:58 +01:00
import { JOB_STATES, JOB_CATEGORIES } from '../../initializers'
2017-06-16 09:45:46 +02:00
import { addMethodsToModel } from '../utils'
2017-05-22 20:58:25 +02:00
import {
JobInstance,
JobAttributes,
JobMethods
} from './job-interface'
2017-06-16 10:36:18 +02:00
import { JobState } from '../../../shared/models/job.model'
2017-05-22 20:58:25 +02:00
let Job: Sequelize.Model<JobInstance, JobAttributes>
2017-11-09 17:51:58 +01:00
let listWithLimitByCategory: JobMethods.ListWithLimitByCategory
2017-06-11 17:35:32 +02:00
export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
2017-05-22 20:58:25 +02:00
Job = sequelize.define<JobInstance, JobAttributes>('Job',
{
state: {
2017-05-15 22:22:03 +02:00
type: DataTypes.ENUM(values(JOB_STATES)),
allowNull: false
},
2017-11-09 17:51:58 +01:00
category: {
type: DataTypes.ENUM(values(JOB_CATEGORIES)),
allowNull: false
},
handlerName: {
type: DataTypes.STRING,
allowNull: false
},
handlerInputData: {
type: DataTypes.JSON,
allowNull: true
}
},
{
indexes: [
{
fields: [ 'state' ]
}
2017-05-22 20:58:25 +02:00
]
}
)
2017-11-09 17:51:58 +01:00
const classMethods = [ listWithLimitByCategory ]
2017-05-22 20:58:25 +02:00
addMethodsToModel(Job, classMethods)
return Job
}
// ---------------------------------------------------------------------------
2017-11-09 17:51:58 +01:00
listWithLimitByCategory = function (limit: number, state: JobState) {
const query = {
order: [
[ 'id', 'ASC' ]
],
limit: limit,
where: {
2017-05-05 17:24:16 +02:00
state
}
}
return Job.findAll(query)
}