]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { values } from 'lodash' |
e02643f3 | 2 | import * as Sequelize from 'sequelize' |
227d02fe | 3 | |
74889a71 | 4 | import { JOB_STATES } from '../../initializers' |
227d02fe | 5 | |
74889a71 | 6 | import { addMethodsToModel } from '../utils' |
e02643f3 C |
7 | import { |
8 | JobClass, | |
9 | JobInstance, | |
10 | JobAttributes, | |
11 | ||
12 | JobMethods | |
13 | } from './job-interface' | |
ee9e7b61 | 14 | import { JobState } from '../../../shared/models/job.model' |
e02643f3 C |
15 | |
16 | let Job: Sequelize.Model<JobInstance, JobAttributes> | |
17 | let listWithLimit: JobMethods.ListWithLimit | |
227d02fe | 18 | |
127944aa | 19 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
e02643f3 | 20 | Job = sequelize.define<JobInstance, JobAttributes>('Job', |
227d02fe C |
21 | { |
22 | state: { | |
65fcc311 | 23 | type: DataTypes.ENUM(values(JOB_STATES)), |
227d02fe C |
24 | allowNull: false |
25 | }, | |
26 | handlerName: { | |
27 | type: DataTypes.STRING, | |
28 | allowNull: false | |
29 | }, | |
30 | handlerInputData: { | |
31 | type: DataTypes.JSON, | |
32 | allowNull: true | |
33 | } | |
34 | }, | |
35 | { | |
36 | indexes: [ | |
37 | { | |
38 | fields: [ 'state' ] | |
39 | } | |
e02643f3 | 40 | ] |
227d02fe C |
41 | } |
42 | ) | |
43 | ||
e02643f3 C |
44 | const classMethods = [ listWithLimit ] |
45 | addMethodsToModel(Job, classMethods) | |
46 | ||
227d02fe C |
47 | return Job |
48 | } | |
49 | ||
50 | // --------------------------------------------------------------------------- | |
51 | ||
ee9e7b61 | 52 | listWithLimit = function (limit: number, state: JobState, callback: JobMethods.ListWithLimitCallback) { |
227d02fe C |
53 | const query = { |
54 | order: [ | |
55 | [ 'id', 'ASC' ] | |
56 | ], | |
57 | limit: limit, | |
58 | where: { | |
4e284e97 | 59 | state |
227d02fe C |
60 | } |
61 | } | |
62 | ||
e02643f3 | 63 | return Job.findAll(query).asCallback(callback) |
227d02fe | 64 | } |