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