diff options
Diffstat (limited to 'server/models/job')
-rw-r--r-- | server/models/job/index.ts | 1 | ||||
-rw-r--r-- | server/models/job/job-interface.ts | 33 | ||||
-rw-r--r-- | server/models/job/job.ts | 137 |
3 files changed, 60 insertions, 111 deletions
diff --git a/server/models/job/index.ts b/server/models/job/index.ts deleted file mode 100644 index 56925fd32..000000000 --- a/server/models/job/index.ts +++ /dev/null | |||
@@ -1 +0,0 @@ | |||
1 | export * from './job-interface' | ||
diff --git a/server/models/job/job-interface.ts b/server/models/job/job-interface.ts deleted file mode 100644 index 3cfc0fbed..000000000 --- a/server/models/job/job-interface.ts +++ /dev/null | |||
@@ -1,33 +0,0 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | import * as Sequelize from 'sequelize' | ||
3 | import { Job as FormattedJob, JobCategory, JobState } from '../../../shared/models/job.model' | ||
4 | import { ResultList } from '../../../shared/models/result-list.model' | ||
5 | |||
6 | export namespace JobMethods { | ||
7 | export type ListWithLimitByCategory = (limit: number, state: JobState, category: JobCategory) => Bluebird<JobInstance[]> | ||
8 | export type ListForApi = (start: number, count: number, sort: string) => Bluebird< ResultList<JobInstance> > | ||
9 | |||
10 | export type ToFormattedJSON = (this: JobInstance) => FormattedJob | ||
11 | } | ||
12 | |||
13 | export interface JobClass { | ||
14 | listWithLimitByCategory: JobMethods.ListWithLimitByCategory | ||
15 | listForApi: JobMethods.ListForApi, | ||
16 | } | ||
17 | |||
18 | export interface JobAttributes { | ||
19 | state: JobState | ||
20 | category: JobCategory | ||
21 | handlerName: string | ||
22 | handlerInputData: any | ||
23 | } | ||
24 | |||
25 | export interface JobInstance extends JobClass, JobAttributes, Sequelize.Instance<JobAttributes> { | ||
26 | id: number | ||
27 | createdAt: Date | ||
28 | updatedAt: Date | ||
29 | |||
30 | toFormattedJSON: JobMethods.ToFormattedJSON | ||
31 | } | ||
32 | |||
33 | export interface JobModel extends JobClass, Sequelize.Model<JobInstance, JobAttributes> {} | ||
diff --git a/server/models/job/job.ts b/server/models/job/job.ts index f428e26db..35c357e69 100644 --- a/server/models/job/job.ts +++ b/server/models/job/job.ts | |||
@@ -1,96 +1,79 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import * as Sequelize from 'sequelize' | 2 | import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { JobCategory, JobState } from '../../../shared/models/job.model' | 3 | import { JobCategory, JobState } from '../../../shared/models' |
4 | import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' | 4 | import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' |
5 | import { addMethodsToModel, getSort } from '../utils' | 5 | import { getSort } from '../utils' |
6 | import { JobAttributes, JobInstance, JobMethods } from './job-interface' | ||
7 | 6 | ||
8 | let Job: Sequelize.Model<JobInstance, JobAttributes> | 7 | @Table({ |
9 | let listWithLimitByCategory: JobMethods.ListWithLimitByCategory | 8 | tableName: 'job', |
10 | let listForApi: JobMethods.ListForApi | 9 | indexes: [ |
11 | let toFormattedJSON: JobMethods.ToFormattedJSON | ||
12 | |||
13 | export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | ||
14 | Job = sequelize.define<JobInstance, JobAttributes>('Job', | ||
15 | { | ||
16 | state: { | ||
17 | type: DataTypes.ENUM(values(JOB_STATES)), | ||
18 | allowNull: false | ||
19 | }, | ||
20 | category: { | ||
21 | type: DataTypes.ENUM(values(JOB_CATEGORIES)), | ||
22 | allowNull: false | ||
23 | }, | ||
24 | handlerName: { | ||
25 | type: DataTypes.STRING, | ||
26 | allowNull: false | ||
27 | }, | ||
28 | handlerInputData: { | ||
29 | type: DataTypes.JSON, | ||
30 | allowNull: true | ||
31 | } | ||
32 | }, | ||
33 | { | 10 | { |
34 | indexes: [ | 11 | fields: [ 'state', 'category' ] |
35 | { | ||
36 | fields: [ 'state', 'category' ] | ||
37 | } | ||
38 | ] | ||
39 | } | 12 | } |
40 | ) | ||
41 | |||
42 | const classMethods = [ | ||
43 | listWithLimitByCategory, | ||
44 | listForApi | ||
45 | ] | 13 | ] |
46 | const instanceMethods = [ | 14 | }) |
47 | toFormattedJSON | 15 | export class JobModel extends Model<JobModel> { |
48 | ] | 16 | @AllowNull(false) |
49 | addMethodsToModel(Job, classMethods, instanceMethods) | 17 | @Column(DataType.ENUM(values(JOB_STATES))) |
18 | state: JobState | ||
50 | 19 | ||
51 | return Job | 20 | @AllowNull(false) |
52 | } | 21 | @Column(DataType.ENUM(values(JOB_CATEGORIES))) |
22 | category: JobCategory | ||
53 | 23 | ||
54 | toFormattedJSON = function (this: JobInstance) { | 24 | @AllowNull(false) |
55 | return { | 25 | @Column |
56 | id: this.id, | 26 | handlerName: string |
57 | state: this.state, | ||
58 | category: this.category, | ||
59 | handlerName: this.handlerName, | ||
60 | handlerInputData: this.handlerInputData, | ||
61 | createdAt: this.createdAt, | ||
62 | updatedAt: this.updatedAt | ||
63 | } | ||
64 | } | ||
65 | 27 | ||
66 | // --------------------------------------------------------------------------- | 28 | @AllowNull(true) |
29 | @Column(DataType.JSON) | ||
30 | handlerInputData: any | ||
67 | 31 | ||
68 | listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { | 32 | @CreatedAt |
69 | const query = { | 33 | creationDate: Date |
70 | order: [ | 34 | |
71 | [ 'id', 'ASC' ] | 35 | @UpdatedAt |
72 | ], | 36 | updatedOn: Date |
73 | limit: limit, | 37 | |
74 | where: { | 38 | static listWithLimitByCategory (limit: number, state: JobState, jobCategory: JobCategory) { |
75 | state, | 39 | const query = { |
76 | category: jobCategory | 40 | order: [ |
41 | [ 'id', 'ASC' ] | ||
42 | ], | ||
43 | limit: limit, | ||
44 | where: { | ||
45 | state, | ||
46 | category: jobCategory | ||
47 | } | ||
77 | } | 48 | } |
49 | |||
50 | return JobModel.findAll(query) | ||
78 | } | 51 | } |
79 | 52 | ||
80 | return Job.findAll(query) | 53 | static listForApi (start: number, count: number, sort: string) { |
81 | } | 54 | const query = { |
55 | offset: start, | ||
56 | limit: count, | ||
57 | order: [ getSort(sort) ] | ||
58 | } | ||
82 | 59 | ||
83 | listForApi = function (start: number, count: number, sort: string) { | 60 | return JobModel.findAndCountAll(query).then(({ rows, count }) => { |
84 | const query = { | 61 | return { |
85 | offset: start, | 62 | data: rows, |
86 | limit: count, | 63 | total: count |
87 | order: [ getSort(sort) ] | 64 | } |
65 | }) | ||
88 | } | 66 | } |
89 | 67 | ||
90 | return Job.findAndCountAll(query).then(({ rows, count }) => { | 68 | toFormattedJSON () { |
91 | return { | 69 | return { |
92 | data: rows, | 70 | id: this.id, |
93 | total: count | 71 | state: this.state, |
72 | category: this.category, | ||
73 | handlerName: this.handlerName, | ||
74 | handlerInputData: this.handlerInputData, | ||
75 | createdAt: this.createdAt, | ||
76 | updatedAt: this.updatedAt | ||
94 | } | 77 | } |
95 | }) | 78 | } |
96 | } | 79 | } |