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/account/user.ts | |
parent | 94edfc3b2a9cf83f1c9c470a76e4769bc37aad14 (diff) | |
download | PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.gz PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.zst PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.zip |
Use sequelize scopes
Diffstat (limited to 'server/models/account/user.ts')
-rw-r--r-- | server/models/account/user.ts | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 84adad96e..26f04dcb5 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -5,12 +5,12 @@ import { | |||
5 | BeforeUpdate, | 5 | BeforeUpdate, |
6 | Column, CreatedAt, | 6 | Column, CreatedAt, |
7 | DataType, | 7 | DataType, |
8 | Default, | 8 | Default, DefaultScope, |
9 | HasMany, | 9 | HasMany, |
10 | HasOne, | 10 | HasOne, |
11 | Is, | 11 | Is, |
12 | IsEmail, | 12 | IsEmail, |
13 | Model, | 13 | Model, Scopes, |
14 | Table, UpdatedAt | 14 | Table, UpdatedAt |
15 | } from 'sequelize-typescript' | 15 | } from 'sequelize-typescript' |
16 | import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' | 16 | import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' |
@@ -27,6 +27,25 @@ import { getSort, throwIfNotValid } from '../utils' | |||
27 | import { VideoChannelModel } from '../video/video-channel' | 27 | import { VideoChannelModel } from '../video/video-channel' |
28 | import { AccountModel } from './account' | 28 | import { AccountModel } from './account' |
29 | 29 | ||
30 | @DefaultScope({ | ||
31 | include: [ | ||
32 | { | ||
33 | model: () => AccountModel, | ||
34 | required: true | ||
35 | } | ||
36 | ] | ||
37 | }) | ||
38 | @Scopes({ | ||
39 | withVideoChannel: { | ||
40 | include: [ | ||
41 | { | ||
42 | model: () => AccountModel, | ||
43 | required: true, | ||
44 | include: [ () => VideoChannelModel ] | ||
45 | } | ||
46 | ] | ||
47 | } | ||
48 | }) | ||
30 | @Table({ | 49 | @Table({ |
31 | tableName: 'user', | 50 | tableName: 'user', |
32 | indexes: [ | 51 | indexes: [ |
@@ -122,8 +141,7 @@ export class UserModel extends Model<UserModel> { | |||
122 | const query = { | 141 | const query = { |
123 | offset: start, | 142 | offset: start, |
124 | limit: count, | 143 | limit: count, |
125 | order: [ getSort(sort) ], | 144 | order: [ getSort(sort) ] |
126 | include: [ { model: AccountModel, required: true } ] | ||
127 | } | 145 | } |
128 | 146 | ||
129 | return UserModel.findAndCountAll(query) | 147 | return UserModel.findAndCountAll(query) |
@@ -136,19 +154,14 @@ export class UserModel extends Model<UserModel> { | |||
136 | } | 154 | } |
137 | 155 | ||
138 | static loadById (id: number) { | 156 | static loadById (id: number) { |
139 | const options = { | 157 | return UserModel.findById(id) |
140 | include: [ { model: AccountModel, required: true } ] | ||
141 | } | ||
142 | |||
143 | return UserModel.findById(id, options) | ||
144 | } | 158 | } |
145 | 159 | ||
146 | static loadByUsername (username: string) { | 160 | static loadByUsername (username: string) { |
147 | const query = { | 161 | const query = { |
148 | where: { | 162 | where: { |
149 | username | 163 | username |
150 | }, | 164 | } |
151 | include: [ { model: AccountModel, required: true } ] | ||
152 | } | 165 | } |
153 | 166 | ||
154 | return UserModel.findOne(query) | 167 | return UserModel.findOne(query) |
@@ -158,29 +171,20 @@ export class UserModel extends Model<UserModel> { | |||
158 | const query = { | 171 | const query = { |
159 | where: { | 172 | where: { |
160 | username | 173 | username |
161 | }, | 174 | } |
162 | include: [ | ||
163 | { | ||
164 | model: AccountModel, | ||
165 | required: true, | ||
166 | include: [ VideoChannelModel ] | ||
167 | } | ||
168 | ] | ||
169 | } | 175 | } |
170 | 176 | ||
171 | return UserModel.findOne(query) | 177 | return UserModel.scope('withVideoChannel').findOne(query) |
172 | } | 178 | } |
173 | 179 | ||
174 | static loadByUsernameOrEmail (username: string, email: string) { | 180 | static loadByUsernameOrEmail (username: string, email: string) { |
175 | const query = { | 181 | const query = { |
176 | include: [ { model: AccountModel, required: true } ], | ||
177 | where: { | 182 | where: { |
178 | [ Sequelize.Op.or ]: [ { username }, { email } ] | 183 | [ Sequelize.Op.or ]: [ { username }, { email } ] |
179 | } | 184 | } |
180 | } | 185 | } |
181 | 186 | ||
182 | // FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387 | 187 | return UserModel.findOne(query) |
183 | return (UserModel as any).findOne(query) | ||
184 | } | 188 | } |
185 | 189 | ||
186 | private static getOriginalVideoFileTotalFromUser (user: UserModel) { | 190 | private static getOriginalVideoFileTotalFromUser (user: UserModel) { |