From 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Dec 2017 17:53:50 +0100 Subject: Move models to typescript-sequelize --- server/models/job/job.ts | 137 +++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 77 deletions(-) (limited to 'server/models/job/job.ts') 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 @@ import { values } from 'lodash' -import * as Sequelize from 'sequelize' -import { JobCategory, JobState } from '../../../shared/models/job.model' +import { AllowNull, Column, CreatedAt, DataType, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { JobCategory, JobState } from '../../../shared/models' import { JOB_CATEGORIES, JOB_STATES } from '../../initializers' -import { addMethodsToModel, getSort } from '../utils' -import { JobAttributes, JobInstance, JobMethods } from './job-interface' +import { getSort } from '../utils' -let Job: Sequelize.Model -let listWithLimitByCategory: JobMethods.ListWithLimitByCategory -let listForApi: JobMethods.ListForApi -let toFormattedJSON: JobMethods.ToFormattedJSON - -export default function defineJob (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - Job = sequelize.define('Job', - { - state: { - type: DataTypes.ENUM(values(JOB_STATES)), - allowNull: false - }, - category: { - type: DataTypes.ENUM(values(JOB_CATEGORIES)), - allowNull: false - }, - handlerName: { - type: DataTypes.STRING, - allowNull: false - }, - handlerInputData: { - type: DataTypes.JSON, - allowNull: true - } - }, +@Table({ + tableName: 'job', + indexes: [ { - indexes: [ - { - fields: [ 'state', 'category' ] - } - ] + fields: [ 'state', 'category' ] } - ) - - const classMethods = [ - listWithLimitByCategory, - listForApi ] - const instanceMethods = [ - toFormattedJSON - ] - addMethodsToModel(Job, classMethods, instanceMethods) +}) +export class JobModel extends Model { + @AllowNull(false) + @Column(DataType.ENUM(values(JOB_STATES))) + state: JobState - return Job -} + @AllowNull(false) + @Column(DataType.ENUM(values(JOB_CATEGORIES))) + category: JobCategory -toFormattedJSON = function (this: JobInstance) { - return { - id: this.id, - state: this.state, - category: this.category, - handlerName: this.handlerName, - handlerInputData: this.handlerInputData, - createdAt: this.createdAt, - updatedAt: this.updatedAt - } -} + @AllowNull(false) + @Column + handlerName: string -// --------------------------------------------------------------------------- + @AllowNull(true) + @Column(DataType.JSON) + handlerInputData: any -listWithLimitByCategory = function (limit: number, state: JobState, jobCategory: JobCategory) { - const query = { - order: [ - [ 'id', 'ASC' ] - ], - limit: limit, - where: { - state, - category: jobCategory + @CreatedAt + creationDate: Date + + @UpdatedAt + updatedOn: Date + + static listWithLimitByCategory (limit: number, state: JobState, jobCategory: JobCategory) { + const query = { + order: [ + [ 'id', 'ASC' ] + ], + limit: limit, + where: { + state, + category: jobCategory + } } + + return JobModel.findAll(query) } - return Job.findAll(query) -} + static listForApi (start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: [ getSort(sort) ] + } -listForApi = function (start: number, count: number, sort: string) { - const query = { - offset: start, - limit: count, - order: [ getSort(sort) ] + return JobModel.findAndCountAll(query).then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) } - return Job.findAndCountAll(query).then(({ rows, count }) => { + toFormattedJSON () { return { - data: rows, - total: count + id: this.id, + state: this.state, + category: this.category, + handlerName: this.handlerName, + handlerInputData: this.handlerInputData, + createdAt: this.createdAt, + updatedAt: this.updatedAt } - }) + } } -- cgit v1.2.3