diff options
Diffstat (limited to 'server/models/application.ts')
-rw-r--r-- | server/models/application.ts | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/server/models/application.ts b/server/models/application.ts index 38a57e327..acd0dfbf2 100644 --- a/server/models/application.ts +++ b/server/models/application.ts | |||
@@ -1,5 +1,20 @@ | |||
1 | module.exports = function (sequelize, DataTypes) { | 1 | import * as Sequelize from 'sequelize' |
2 | const Application = sequelize.define('Application', | 2 | |
3 | import { addMethodsToModel } from './utils' | ||
4 | import { | ||
5 | ApplicationClass, | ||
6 | ApplicationAttributes, | ||
7 | ApplicationInstance, | ||
8 | |||
9 | ApplicationMethods | ||
10 | } from './application-interface' | ||
11 | |||
12 | let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes> | ||
13 | let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion | ||
14 | let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion | ||
15 | |||
16 | export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes) { | ||
17 | Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application', | ||
3 | { | 18 | { |
4 | migrationVersion: { | 19 | migrationVersion: { |
5 | type: DataTypes.INTEGER, | 20 | type: DataTypes.INTEGER, |
@@ -9,34 +24,31 @@ module.exports = function (sequelize, DataTypes) { | |||
9 | isInt: true | 24 | isInt: true |
10 | } | 25 | } |
11 | } | 26 | } |
12 | }, | ||
13 | { | ||
14 | classMethods: { | ||
15 | loadMigrationVersion, | ||
16 | updateMigrationVersion | ||
17 | } | ||
18 | } | 27 | } |
19 | ) | 28 | ) |
20 | 29 | ||
30 | const classMethods = [ loadMigrationVersion, updateMigrationVersion ] | ||
31 | addMethodsToModel(Application, classMethods) | ||
32 | |||
21 | return Application | 33 | return Application |
22 | } | 34 | } |
23 | 35 | ||
24 | // --------------------------------------------------------------------------- | 36 | // --------------------------------------------------------------------------- |
25 | 37 | ||
26 | function loadMigrationVersion (callback) { | 38 | loadMigrationVersion = function (callback: (err: Error, version: number) => void) { |
27 | const query = { | 39 | const query = { |
28 | attributes: [ 'migrationVersion' ] | 40 | attributes: [ 'migrationVersion' ] |
29 | } | 41 | } |
30 | 42 | ||
31 | return this.findOne(query).asCallback(function (err, data) { | 43 | return Application.findOne(query).asCallback(function (err, data) { |
32 | const version = data ? data.migrationVersion : null | 44 | const version = data ? data.migrationVersion : null |
33 | 45 | ||
34 | return callback(err, version) | 46 | return callback(err, version) |
35 | }) | 47 | }) |
36 | } | 48 | } |
37 | 49 | ||
38 | function updateMigrationVersion (newVersion, transaction, callback) { | 50 | updateMigrationVersion = function (newVersion: number, transaction: any, callback: any) { |
39 | const options: { where?: any, transaction?: any } = { | 51 | const options: Sequelize.UpdateOptions = { |
40 | where: {} | 52 | where: {} |
41 | } | 53 | } |
42 | 54 | ||
@@ -46,5 +58,5 @@ function updateMigrationVersion (newVersion, transaction, callback) { | |||
46 | options.transaction = transaction | 58 | options.transaction = transaction |
47 | } | 59 | } |
48 | 60 | ||
49 | return this.update({ migrationVersion: newVersion }, options).asCallback(callback) | 61 | return Application.update({ migrationVersion: newVersion }, options).asCallback(callback) |
50 | } | 62 | } |