From e02643f32e4c97ca307f8fc5b69be79c40d70a3b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 22 May 2017 20:58:25 +0200 Subject: Type models --- server/models/user-video-rate.ts | 48 ++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'server/models/user-video-rate.ts') diff --git a/server/models/user-video-rate.ts b/server/models/user-video-rate.ts index 6603c7862..87886d8d0 100644 --- a/server/models/user-video-rate.ts +++ b/server/models/user-video-rate.ts @@ -3,13 +3,24 @@ */ import { values } from 'lodash' +import * as Sequelize from 'sequelize' import { VIDEO_RATE_TYPES } from '../initializers' -// --------------------------------------------------------------------------- +import { addMethodsToModel } from './utils' +import { + UserVideoRateClass, + UserVideoRateInstance, + UserVideoRateAttributes, -module.exports = function (sequelize, DataTypes) { - const UserVideoRate = sequelize.define('UserVideoRate', + UserVideoRateMethods +} from './user-video-rate-interface' + +let UserVideoRate: Sequelize.Model +let load: UserVideoRateMethods.Load + +export default function (sequelize, DataTypes) { + UserVideoRate = sequelize.define('UserVideoRate', { type: { type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)), @@ -22,22 +33,24 @@ module.exports = function (sequelize, DataTypes) { fields: [ 'videoId', 'userId', 'type' ], unique: true } - ], - classMethods: { - associate, - - load - } + ] } ) + const classMethods = [ + associate, + + load + ] + addMethodsToModel(UserVideoRate, classMethods) + return UserVideoRate } // ------------------------------ STATICS ------------------------------ function associate (models) { - this.belongsTo(models.Video, { + UserVideoRate.belongsTo(models.Video, { foreignKey: { name: 'videoId', allowNull: false @@ -45,7 +58,7 @@ function associate (models) { onDelete: 'CASCADE' }) - this.belongsTo(models.User, { + UserVideoRate.belongsTo(models.User, { foreignKey: { name: 'userId', allowNull: false @@ -54,21 +67,14 @@ function associate (models) { }) } -function load (userId, videoId, transaction, callback) { - if (!callback) { - callback = transaction - transaction = null - } - - const query = { +load = function (userId, videoId, transaction, callback) { + const options: Sequelize.FindOptions = { where: { userId, videoId } } - - const options: any = {} if (transaction) options.transaction = transaction - return this.findOne(query, options).asCallback(callback) + return UserVideoRate.findOne(options).asCallback(callback) } -- cgit v1.2.3