diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/job/job.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/models/job/job.ts')
-rw-r--r-- | server/models/job/job.ts | 137 |
1 files changed, 60 insertions, 77 deletions
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 | } |