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