1
0
Fork 0
peertube/server/models/job/job.ts

64 lines
1.3 KiB
TypeScript
Raw Normal View History

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