]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/job/job.ts
Create types for model enums
[github/Chocobozzz/PeerTube.git] / server / models / job / job.ts
1 import { values } from 'lodash'
2 import * as Sequelize from 'sequelize'
3
4 import { JOB_STATES } from '../../initializers'
5
6 import { addMethodsToModel } from '../utils'
7 import {
8 JobClass,
9 JobInstance,
10 JobAttributes,
11
12 JobMethods
13 } from './job-interface'
14 import { JobState } from '../../../shared/models/job.model'
15
16 let Job: Sequelize.Model<JobInstance, JobAttributes>
17 let listWithLimit: JobMethods.ListWithLimit
18
19 export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
20 Job = sequelize.define<JobInstance, JobAttributes>('Job',
21 {
22 state: {
23 type: DataTypes.ENUM(values(JOB_STATES)),
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 }
40 ]
41 }
42 )
43
44 const classMethods = [ listWithLimit ]
45 addMethodsToModel(Job, classMethods)
46
47 return Job
48 }
49
50 // ---------------------------------------------------------------------------
51
52 listWithLimit = function (limit: number, state: JobState, callback: JobMethods.ListWithLimitCallback) {
53 const query = {
54 order: [
55 [ 'id', 'ASC' ]
56 ],
57 limit: limit,
58 where: {
59 state
60 }
61 }
62
63 return Job.findAll(query).asCallback(callback)
64 }