diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-14 10:07:57 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-14 10:07:57 +0100 |
commit | d48ff09d27d234425c3e9f091ae9072d8e6d8b7a (patch) | |
tree | 3a842b79aec40ca55d68c1cb6cf9b8aadcf2a1d1 /server/models/video/video-channel-share.ts | |
parent | 94edfc3b2a9cf83f1c9c470a76e4769bc37aad14 (diff) | |
download | PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.gz PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.zst PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.zip |
Use sequelize scopes
Diffstat (limited to 'server/models/video/video-channel-share.ts')
-rw-r--r-- | server/models/video/video-channel-share.ts | 43 |
1 files changed, 30 insertions, 13 deletions
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | 2 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
3 | import { AccountModel } from '../account/account' | 3 | import { AccountModel } from '../account/account' |
4 | import { VideoChannelModel } from './video-channel' | 4 | import { VideoChannelModel } from './video-channel' |
5 | 5 | ||
6 | enum ScopeNames { | ||
7 | FULL = 'FULL', | ||
8 | WITH_ACCOUNT = 'WITH_ACCOUNT' | ||
9 | } | ||
10 | |||
11 | @Scopes({ | ||
12 | [ScopeNames.FULL]: { | ||
13 | include: [ | ||
14 | { | ||
15 | model: () => AccountModel, | ||
16 | required: true | ||
17 | }, | ||
18 | { | ||
19 | model: () => VideoChannelModel, | ||
20 | required: true | ||
21 | } | ||
22 | ] | ||
23 | }, | ||
24 | [ScopeNames.WITH_ACCOUNT]: { | ||
25 | include: [ | ||
26 | { | ||
27 | model: () => AccountModel, | ||
28 | required: true | ||
29 | } | ||
30 | ] | ||
31 | } | ||
32 | }) | ||
6 | @Table({ | 33 | @Table({ |
7 | tableName: 'videoChannelShare', | 34 | tableName: 'videoChannelShare', |
8 | indexes: [ | 35 | indexes: [ |
@@ -46,15 +73,11 @@ export class VideoChannelShareModel extends Model<VideoChannelShareModel> { | |||
46 | VideoChannel: VideoChannelModel | 73 | VideoChannel: VideoChannelModel |
47 | 74 | ||
48 | static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { | 75 | static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { |
49 | return VideoChannelShareModel.findOne({ | 76 | return VideoChannelShareModel.scope(ScopeNames.FULL).findOne({ |
50 | where: { | 77 | where: { |
51 | accountId, | 78 | accountId, |
52 | videoChannelId | 79 | videoChannelId |
53 | }, | 80 | }, |
54 | include: [ | ||
55 | AccountModel, | ||
56 | VideoChannelModel | ||
57 | ], | ||
58 | transaction: t | 81 | transaction: t |
59 | }) | 82 | }) |
60 | } | 83 | } |
@@ -64,16 +87,10 @@ export class VideoChannelShareModel extends Model<VideoChannelShareModel> { | |||
64 | where: { | 87 | where: { |
65 | videoChannelId | 88 | videoChannelId |
66 | }, | 89 | }, |
67 | include: [ | ||
68 | { | ||
69 | model: AccountModel, | ||
70 | required: true | ||
71 | } | ||
72 | ], | ||
73 | transaction: t | 90 | transaction: t |
74 | } | 91 | } |
75 | 92 | ||
76 | return VideoChannelShareModel.findAll(query) | 93 | return VideoChannelShareModel.scope(ScopeNames.WITH_ACCOUNT).findAll(query) |
77 | .then(res => res.map(r => r.Account)) | 94 | .then(res => res.map(r => r.Account)) |
78 | } | 95 | } |
79 | } | 96 | } |