From d48ff09d27d234425c3e9f091ae9072d8e6d8b7a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Dec 2017 10:07:57 +0100 Subject: Use sequelize scopes --- server/models/video/video-channel-share.ts | 43 +++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'server/models/video/video-channel-share.ts') diff --git a/server/models/video/video-channel-share.ts b/server/models/video/video-channel-share.ts index cdba32fcd..f5b7a7cd5 100644 --- a/server/models/video/video-channel-share.ts +++ b/server/models/video/video-channel-share.ts @@ -1,8 +1,35 @@ import * as Sequelize from 'sequelize' -import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { AccountModel } from '../account/account' import { VideoChannelModel } from './video-channel' +enum ScopeNames { + FULL = 'FULL', + WITH_ACCOUNT = 'WITH_ACCOUNT' +} + +@Scopes({ + [ScopeNames.FULL]: { + include: [ + { + model: () => AccountModel, + required: true + }, + { + model: () => VideoChannelModel, + required: true + } + ] + }, + [ScopeNames.WITH_ACCOUNT]: { + include: [ + { + model: () => AccountModel, + required: true + } + ] + } +}) @Table({ tableName: 'videoChannelShare', indexes: [ @@ -46,15 +73,11 @@ export class VideoChannelShareModel extends Model { VideoChannel: VideoChannelModel static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { - return VideoChannelShareModel.findOne({ + return VideoChannelShareModel.scope(ScopeNames.FULL).findOne({ where: { accountId, videoChannelId }, - include: [ - AccountModel, - VideoChannelModel - ], transaction: t }) } @@ -64,16 +87,10 @@ export class VideoChannelShareModel extends Model { where: { videoChannelId }, - include: [ - { - model: AccountModel, - required: true - } - ], transaction: t } - return VideoChannelShareModel.findAll(query) + return VideoChannelShareModel.scope(ScopeNames.WITH_ACCOUNT).findAll(query) .then(res => res.map(r => r.Account)) } } -- cgit v1.2.3