diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-06-16 09:45:46 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-06-16 09:45:46 +0200 |
commit | 74889a71fe687dda74f2a687653122327807af36 (patch) | |
tree | e938e8b6401b74fbec80513a877d9967f2c0dbcd /server/models/application | |
parent | 15a302943d84bc0978b84fe33110c4daa451d311 (diff) | |
download | PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.gz PeerTube-74889a71fe687dda74f2a687653122327807af36.tar.zst PeerTube-74889a71fe687dda74f2a687653122327807af36.zip |
Reorganize model files
Diffstat (limited to 'server/models/application')
-rw-r--r-- | server/models/application/application-interface.ts | 26 | ||||
-rw-r--r-- | server/models/application/application.ts | 57 | ||||
-rw-r--r-- | server/models/application/index.ts | 1 |
3 files changed, 84 insertions, 0 deletions
diff --git a/server/models/application/application-interface.ts b/server/models/application/application-interface.ts new file mode 100644 index 000000000..c03513db1 --- /dev/null +++ b/server/models/application/application-interface.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
3 | export namespace ApplicationMethods { | ||
4 | export type LoadMigrationVersionCallback = (err: Error, version: number) => void | ||
5 | export type LoadMigrationVersion = (callback: LoadMigrationVersionCallback) => void | ||
6 | |||
7 | export type UpdateMigrationVersionCallback = (err: Error, applicationInstance: ApplicationAttributes) => void | ||
8 | export type UpdateMigrationVersion = (newVersion: number, transaction: Sequelize.Transaction, callback: UpdateMigrationVersionCallback) => void | ||
9 | } | ||
10 | |||
11 | export interface ApplicationClass { | ||
12 | loadMigrationVersion: ApplicationMethods.LoadMigrationVersion | ||
13 | updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion | ||
14 | } | ||
15 | |||
16 | export interface ApplicationAttributes { | ||
17 | migrationVersion: number | ||
18 | } | ||
19 | |||
20 | export interface ApplicationInstance extends ApplicationClass, ApplicationAttributes, Sequelize.Instance<ApplicationAttributes> { | ||
21 | id: number | ||
22 | createdAt: Date | ||
23 | updatedAt: Date | ||
24 | } | ||
25 | |||
26 | export interface ApplicationModel extends ApplicationClass, Sequelize.Model<ApplicationInstance, ApplicationAttributes> {} | ||
diff --git a/server/models/application/application.ts b/server/models/application/application.ts new file mode 100644 index 000000000..0e9a1ebb3 --- /dev/null +++ b/server/models/application/application.ts | |||
@@ -0,0 +1,57 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
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: Sequelize.DataTypes) { | ||
17 | Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application', | ||
18 | { | ||
19 | migrationVersion: { | ||
20 | type: DataTypes.INTEGER, | ||
21 | defaultValue: 0, | ||
22 | allowNull: false, | ||
23 | validate: { | ||
24 | isInt: true | ||
25 | } | ||
26 | } | ||
27 | } | ||
28 | ) | ||
29 | |||
30 | const classMethods = [ loadMigrationVersion, updateMigrationVersion ] | ||
31 | addMethodsToModel(Application, classMethods) | ||
32 | |||
33 | return Application | ||
34 | } | ||
35 | |||
36 | // --------------------------------------------------------------------------- | ||
37 | |||
38 | loadMigrationVersion = function (callback: ApplicationMethods.LoadMigrationVersionCallback) { | ||
39 | const query = { | ||
40 | attributes: [ 'migrationVersion' ] | ||
41 | } | ||
42 | |||
43 | return Application.findOne(query).asCallback(function (err, data) { | ||
44 | const version = data ? data.migrationVersion : null | ||
45 | |||
46 | return callback(err, version) | ||
47 | }) | ||
48 | } | ||
49 | |||
50 | updateMigrationVersion = function (newVersion: number, transaction: Sequelize.Transaction, callback: ApplicationMethods.UpdateMigrationVersionCallback) { | ||
51 | const options: Sequelize.UpdateOptions = { | ||
52 | where: {}, | ||
53 | transaction: transaction | ||
54 | } | ||
55 | |||
56 | return Application.update({ migrationVersion: newVersion }, options).asCallback(callback) | ||
57 | } | ||
diff --git a/server/models/application/index.ts b/server/models/application/index.ts new file mode 100644 index 000000000..706f85cb9 --- /dev/null +++ b/server/models/application/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './application-interface' | |||