]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/job/job.ts
Create types for model enums
[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
C
7import {
8 JobClass,
9 JobInstance,
10 JobAttributes,
11
12 JobMethods
13} from './job-interface'
ee9e7b61 14import { JobState } from '../../../shared/models/job.model'
e02643f3
C
15
16let Job: Sequelize.Model<JobInstance, JobAttributes>
17let listWithLimit: JobMethods.ListWithLimit
227d02fe 18
127944aa 19export 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 52listWithLimit = 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}