aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/application
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/application')
-rw-r--r--server/models/application/application-interface.ts31
-rw-r--r--server/models/application/application.ts80
-rw-r--r--server/models/application/index.ts1
3 files changed, 27 insertions, 85 deletions
diff --git a/server/models/application/application-interface.ts b/server/models/application/application-interface.ts
deleted file mode 100644
index 2c391dba3..000000000
--- a/server/models/application/application-interface.ts
+++ /dev/null
@@ -1,31 +0,0 @@
1import * as Sequelize from 'sequelize'
2import * as Bluebird from 'bluebird'
3
4export namespace ApplicationMethods {
5 export type LoadMigrationVersion = () => Bluebird<number>
6
7 export type UpdateMigrationVersion = (
8 newVersion: number,
9 transaction: Sequelize.Transaction
10 ) => Bluebird<[ number, ApplicationInstance[] ]>
11
12 export type CountTotal = () => Bluebird<number>
13}
14
15export interface ApplicationClass {
16 loadMigrationVersion: ApplicationMethods.LoadMigrationVersion
17 updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion
18 countTotal: ApplicationMethods.CountTotal
19}
20
21export interface ApplicationAttributes {
22 migrationVersion: number
23}
24
25export interface ApplicationInstance extends ApplicationClass, ApplicationAttributes, Sequelize.Instance<ApplicationAttributes> {
26 id: number
27 createdAt: Date
28 updatedAt: Date
29}
30
31export interface ApplicationModel extends ApplicationClass, Sequelize.Model<ApplicationInstance, ApplicationAttributes> {}
diff --git a/server/models/application/application.ts b/server/models/application/application.ts
index 8ba40a895..f3c0f1052 100644
--- a/server/models/application/application.ts
+++ b/server/models/application/application.ts
@@ -1,61 +1,35 @@
1import * as Sequelize from 'sequelize' 1import { Transaction } from 'sequelize'
2 2import { AllowNull, Column, Default, IsInt, Model, Table } from 'sequelize-typescript'
3import { addMethodsToModel } from '../utils' 3
4import { 4@Table({
5 ApplicationAttributes, 5 tableName: 'application'
6 ApplicationInstance, 6})
7 7export class ApplicationModel extends Model<ApplicationModel> {
8 ApplicationMethods 8
9} from './application-interface' 9 @AllowNull(false)
10 10 @Default(0)
11let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes> 11 @IsInt
12let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion 12 @Column
13let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion 13 migrationVersion: number
14let countTotal: ApplicationMethods.CountTotal 14
15 static countTotal () {
16 return ApplicationModel.count()
17 }
15 18
16export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { 19 static loadMigrationVersion () {
17 Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application', 20 const query = {
18 { 21 attributes: [ 'migrationVersion' ]
19 migrationVersion: {
20 type: DataTypes.INTEGER,
21 defaultValue: 0,
22 allowNull: false,
23 validate: {
24 isInt: true
25 }
26 }
27 } 22 }
28 )
29
30 const classMethods = [
31 countTotal,
32 loadMigrationVersion,
33 updateMigrationVersion
34 ]
35 addMethodsToModel(Application, classMethods)
36
37 return Application
38}
39 23
40// --------------------------------------------------------------------------- 24 return ApplicationModel.findOne(query).then(data => data ? data.migrationVersion : null)
41
42countTotal = function () {
43 return this.count()
44}
45
46loadMigrationVersion = function () {
47 const query = {
48 attributes: [ 'migrationVersion' ]
49 } 25 }
50 26
51 return Application.findOne(query).then(data => data ? data.migrationVersion : null) 27 static updateMigrationVersion (newVersion: number, transaction: Transaction) {
52} 28 const options = {
29 where: {},
30 transaction: transaction
31 }
53 32
54updateMigrationVersion = function (newVersion: number, transaction: Sequelize.Transaction) { 33 return ApplicationModel.update({ migrationVersion: newVersion }, options)
55 const options: Sequelize.UpdateOptions = {
56 where: {},
57 transaction: transaction
58 } 34 }
59
60 return Application.update({ migrationVersion: newVersion }, options)
61} 35}
diff --git a/server/models/application/index.ts b/server/models/application/index.ts
deleted file mode 100644
index 706f85cb9..000000000
--- a/server/models/application/index.ts
+++ /dev/null
@@ -1 +0,0 @@
1export * from './application-interface'