]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
74889a71 | 3 | import { addMethodsToModel } from '../utils' |
e02643f3 | 4 | import { |
e02643f3 C |
5 | ApplicationAttributes, |
6 | ApplicationInstance, | |
7 | ||
8 | ApplicationMethods | |
9 | } from './application-interface' | |
10 | ||
11 | let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes> | |
12 | let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion | |
13 | let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion | |
14 | ||
127944aa | 15 | export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
e02643f3 | 16 | Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application', |
feb4bdfd | 17 | { |
b769007f | 18 | migrationVersion: { |
feb4bdfd | 19 | type: DataTypes.INTEGER, |
67bf9b96 C |
20 | defaultValue: 0, |
21 | allowNull: false, | |
22 | validate: { | |
23 | isInt: true | |
24 | } | |
feb4bdfd | 25 | } |
feb4bdfd C |
26 | } |
27 | ) | |
28 | ||
e02643f3 C |
29 | const classMethods = [ loadMigrationVersion, updateMigrationVersion ] |
30 | addMethodsToModel(Application, classMethods) | |
31 | ||
feb4bdfd C |
32 | return Application |
33 | } | |
00d6b0dd C |
34 | |
35 | // --------------------------------------------------------------------------- | |
36 | ||
6fcd19ba | 37 | loadMigrationVersion = function () { |
feb4bdfd | 38 | const query = { |
b769007f | 39 | attributes: [ 'migrationVersion' ] |
00d6b0dd | 40 | } |
00d6b0dd | 41 | |
6fcd19ba | 42 | return Application.findOne(query).then(data => data ? data.migrationVersion : null) |
00d6b0dd C |
43 | } |
44 | ||
6fcd19ba | 45 | updateMigrationVersion = function (newVersion: number, transaction: Sequelize.Transaction) { |
e02643f3 | 46 | const options: Sequelize.UpdateOptions = { |
69818c93 C |
47 | where: {}, |
48 | transaction: transaction | |
b769007f C |
49 | } |
50 | ||
6fcd19ba | 51 | return Application.update({ migrationVersion: newVersion }, options) |
00d6b0dd | 52 | } |