aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/job
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-12 17:53:50 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 16:50:33 +0100
commit3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch)
treee5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/job
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/models/job')
-rw-r--r--server/models/job/index.ts1
-rw-r--r--server/models/job/job-interface.ts33
-rw-r--r--server/models/job/job.ts137
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 @@
1export * 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 @@
1import * as Bluebird from 'bluebird'
2import * as Sequelize from 'sequelize'
3import { Job as FormattedJob, JobCategory, JobState } from '../../../shared/models/job.model'
4import { ResultList } from '../../../shared/models/result-list.model'
5
6export 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
13export interface JobClass {
14 listWithLimitByCategory: JobMethods.ListWithLimitByCategory
15 listForApi: JobMethods.ListForApi,
16}
17
18export interface JobAttributes {
19 state: JobState
20 category: JobCategory
21 handlerName: string
22 handlerInputData: any
23}
24
25export interface JobInstance extends JobClass, JobAttributes, Sequelize.Instance<JobAttributes> {
26 id: number
27 createdAt: Date
28 updatedAt: Date
29
30 toFormattedJSON: JobMethods.ToFormattedJSON
31}
32
33export 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 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as Sequelize from 'sequelize' 2import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { JobCategory, JobState } from '../../../shared/models/job.model' 3import { JobCategory, JobState } from '../../../shared/models'
4import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' 4import { JOB_CATEGORIES, JOB_STATES } from '../../initializers'
5import { addMethodsToModel, getSort } from '../utils' 5import { getSort } from '../utils'
6import { JobAttributes, JobInstance, JobMethods } from './job-interface'
7 6
8let Job: Sequelize.Model<JobInstance, JobAttributes> 7@Table({
9let listWithLimitByCategory: JobMethods.ListWithLimitByCategory 8 tableName: 'job',
10let listForApi: JobMethods.ListForApi 9 indexes: [
11let toFormattedJSON: JobMethods.ToFormattedJSON
12
13export 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 15export 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
54toFormattedJSON = 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
68listWithLimitByCategory = 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
83listForApi = 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}