PeerTube/server/models/application/application.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-05-22 20:58:25 +02:00
import * as Sequelize from 'sequelize'
2017-06-16 09:45:46 +02:00
import { addMethodsToModel } from '../utils'
2017-05-22 20:58:25 +02:00
import {
ApplicationAttributes,
ApplicationInstance,
ApplicationMethods
} from './application-interface'
let Application: Sequelize.Model<ApplicationInstance, ApplicationAttributes>
let loadMigrationVersion: ApplicationMethods.LoadMigrationVersion
let updateMigrationVersion: ApplicationMethods.UpdateMigrationVersion
2017-11-14 17:31:26 +01:00
let countTotal: ApplicationMethods.CountTotal
2017-05-22 20:58:25 +02:00
2017-06-11 17:35:32 +02:00
export default function defineApplication (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
2017-05-22 20:58:25 +02:00
Application = sequelize.define<ApplicationInstance, ApplicationAttributes>('Application',
2016-12-11 21:50:51 +01:00
{
2016-12-25 09:44:57 +01:00
migrationVersion: {
2016-12-11 21:50:51 +01:00
type: DataTypes.INTEGER,
2016-12-28 15:49:23 +01:00
defaultValue: 0,
allowNull: false,
validate: {
isInt: true
}
2016-12-11 21:50:51 +01:00
}
}
)
2017-11-14 17:31:26 +01:00
const classMethods = [
countTotal,
loadMigrationVersion,
updateMigrationVersion
]
2017-05-22 20:58:25 +02:00
addMethodsToModel(Application, classMethods)
2016-12-11 21:50:51 +01:00
return Application
}
// ---------------------------------------------------------------------------
2017-11-14 17:31:26 +01:00
countTotal = function () {
return this.count()
}
loadMigrationVersion = function () {
2016-12-11 21:50:51 +01:00
const query = {
2016-12-25 09:44:57 +01:00
attributes: [ 'migrationVersion' ]
}
return Application.findOne(query).then(data => data ? data.migrationVersion : null)
}
updateMigrationVersion = function (newVersion: number, transaction: Sequelize.Transaction) {
2017-05-22 20:58:25 +02:00
const options: Sequelize.UpdateOptions = {
2017-06-10 22:15:25 +02:00
where: {},
transaction: transaction
2016-12-25 09:44:57 +01:00
}
return Application.update({ migrationVersion: newVersion }, options)
}