diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/application/application.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/models/application/application.ts')
-rw-r--r-- | server/models/application/application.ts | 80 |
1 files changed, 27 insertions, 53 deletions
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
2 | 2 | import { AllowNull, Column, Default, IsInt, Model, Table } from 'sequelize-typescript' | |
3 | import { addMethodsToModel } from '../utils' | 3 | |
4 | import { | 4 | @Table({ |
5 | ApplicationAttributes, | 5 | tableName: 'application' |
6 | ApplicationInstance, | 6 | }) |
7 | 7 | export class ApplicationModel extends Model<ApplicationModel> { | |
8 | ApplicationMethods | 8 | |
9 | } from './application-interface' | 9 | @AllowNull(false) |
10 | 10 | @Default(0) | |
11 | let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes> | 11 | @IsInt |
12 | let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion | 12 | @Column |
13 | let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion | 13 | migrationVersion: number |
14 | let countTotal: ApplicationMethods.CountTotal | 14 | |
15 | static countTotal () { | ||
16 | return ApplicationModel.count() | ||
17 | } | ||
15 | 18 | ||
16 | export 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 | |||
42 | countTotal = function () { | ||
43 | return this.count() | ||
44 | } | ||
45 | |||
46 | loadMigrationVersion = 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 | ||
54 | updateMigrationVersion = 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 | } |