aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-share.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-14 10:07:57 +0100
committerChocobozzz <me@florianbigard.com>2017-12-14 10:07:57 +0100
commitd48ff09d27d234425c3e9f091ae9072d8e6d8b7a (patch)
tree3a842b79aec40ca55d68c1cb6cf9b8aadcf2a1d1 /server/models/video/video-share.ts
parent94edfc3b2a9cf83f1c9c470a76e4769bc37aad14 (diff)
downloadPeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.gz
PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.zst
PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.zip
Use sequelize scopes
Diffstat (limited to 'server/models/video/video-share.ts')
-rw-r--r--server/models/video/video-share.ts36
1 files changed, 30 insertions, 6 deletions
diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts
index 01b6d3d34..e1733b3a7 100644
--- a/server/models/video/video-share.ts
+++ b/server/models/video/video-share.ts
@@ -1,8 +1,35 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' 2import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3import { AccountModel } from '../account/account' 3import { AccountModel } from '../account/account'
4import { VideoModel } from './video' 4import { VideoModel } from './video'
5 5
6enum 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: () => VideoModel,
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: 'videoShare', 34 tableName: 'videoShare',
8 indexes: [ 35 indexes: [
@@ -46,14 +73,11 @@ export class VideoShareModel extends Model<VideoShareModel> {
46 Video: VideoModel 73 Video: VideoModel
47 74
48 static load (accountId: number, videoId: number, t: Sequelize.Transaction) { 75 static load (accountId: number, videoId: number, t: Sequelize.Transaction) {
49 return VideoShareModel.findOne({ 76 return VideoShareModel.scope(ScopeNames.WITH_ACCOUNT).findOne({
50 where: { 77 where: {
51 accountId, 78 accountId,
52 videoId 79 videoId
53 }, 80 },
54 include: [
55 AccountModel
56 ],
57 transaction: t 81 transaction: t
58 }) 82 })
59 } 83 }
@@ -72,7 +96,7 @@ export class VideoShareModel extends Model<VideoShareModel> {
72 transaction: t 96 transaction: t
73 } 97 }
74 98
75 return VideoShareModel.findAll(query) 99 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
76 .then(res => res.map(r => r.Account)) 100 .then(res => res.map(r => r.Account))
77 } 101 }
78} 102}