From 3acc50844047a37698f0618fa235c138e386a053 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Apr 2019 09:50:57 +0200 Subject: Upgrade sequelize --- server/models/account/account-blocklist.ts | 10 +++--- server/models/account/account.ts | 10 +++--- server/models/account/user-notification.ts | 44 +++++++++++------------ server/models/account/user.ts | 58 ++++++++++++++---------------- 4 files changed, 59 insertions(+), 63 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index efd6ed59e..d5746ad76 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -8,22 +8,22 @@ enum ScopeNames { WITH_ACCOUNTS = 'WITH_ACCOUNTS' } -@Scopes({ +@Scopes(() => ({ [ScopeNames.WITH_ACCOUNTS]: { include: [ { - model: () => AccountModel, + model: AccountModel, required: true, as: 'ByAccount' }, { - model: () => AccountModel, + model: AccountModel, required: true, as: 'BlockedAccount' } ] } -}) +})) @Table({ tableName: 'accountBlocklist', @@ -83,7 +83,7 @@ export class AccountBlocklistModel extends Model { attributes: [ 'accountId', 'id' ], where: { accountId: { - [Op.any]: accountIds + [Op.in]: accountIds // FIXME: sequelize ANY seems broken }, targetAccountId }, diff --git a/server/models/account/account.ts b/server/models/account/account.ts index bf2ed0a61..c53312990 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -33,15 +33,15 @@ export enum ScopeNames { SUMMARY = 'SUMMARY' } -@DefaultScope({ +@DefaultScope(() => ({ include: [ { - model: () => ActorModel, // Default scope includes avatar and server + model: ActorModel, // Default scope includes avatar and server required: true } ] -}) -@Scopes({ +})) +@Scopes(() => ({ [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { return { attributes: [ 'id', 'name' ], @@ -66,7 +66,7 @@ export enum ScopeNames { ] } } -}) +})) @Table({ tableName: 'account', indexes: [ diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 08388f268..a4f97037b 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts @@ -6,7 +6,7 @@ import { isUserNotificationTypeValid } from '../../helpers/custom-validators/use import { UserModel } from './user' import { VideoModel } from '../video/video' import { VideoCommentModel } from '../video/video-comment' -import { FindOptions, Op } from 'sequelize' +import { FindOptions, ModelIndexesOptions, Op, WhereOptions } from 'sequelize' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from './account' import { VideoAbuseModel } from '../video/video-abuse' @@ -24,17 +24,17 @@ enum ScopeNames { function buildActorWithAvatarInclude () { return { attributes: [ 'preferredUsername' ], - model: () => ActorModel.unscoped(), + model: ActorModel.unscoped(), required: true, include: [ { attributes: [ 'filename' ], - model: () => AvatarModel.unscoped(), + model: AvatarModel.unscoped(), required: false }, { attributes: [ 'host' ], - model: () => ServerModel.unscoped(), + model: ServerModel.unscoped(), required: false } ] @@ -44,7 +44,7 @@ function buildActorWithAvatarInclude () { function buildVideoInclude (required: boolean) { return { attributes: [ 'id', 'uuid', 'name' ], - model: () => VideoModel.unscoped(), + model: VideoModel.unscoped(), required } } @@ -53,7 +53,7 @@ function buildChannelInclude (required: boolean, withActor = false) { return { required, attributes: [ 'id', 'name' ], - model: () => VideoChannelModel.unscoped(), + model: VideoChannelModel.unscoped(), include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] } } @@ -62,12 +62,12 @@ function buildAccountInclude (required: boolean, withActor = false) { return { required, attributes: [ 'id', 'name' ], - model: () => AccountModel.unscoped(), + model: AccountModel.unscoped(), include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] } } -@Scopes({ +@Scopes(() => ({ [ScopeNames.WITH_ALL]: { include: [ Object.assign(buildVideoInclude(false), { @@ -76,7 +76,7 @@ function buildAccountInclude (required: boolean, withActor = false) { { attributes: [ 'id', 'originCommentId' ], - model: () => VideoCommentModel.unscoped(), + model: VideoCommentModel.unscoped(), required: false, include: [ buildAccountInclude(true, true), @@ -86,56 +86,56 @@ function buildAccountInclude (required: boolean, withActor = false) { { attributes: [ 'id' ], - model: () => VideoAbuseModel.unscoped(), + model: VideoAbuseModel.unscoped(), required: false, include: [ buildVideoInclude(true) ] }, { attributes: [ 'id' ], - model: () => VideoBlacklistModel.unscoped(), + model: VideoBlacklistModel.unscoped(), required: false, include: [ buildVideoInclude(true) ] }, { attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], - model: () => VideoImportModel.unscoped(), + model: VideoImportModel.unscoped(), required: false, include: [ buildVideoInclude(false) ] }, { attributes: [ 'id', 'state' ], - model: () => ActorFollowModel.unscoped(), + model: ActorFollowModel.unscoped(), required: false, include: [ { attributes: [ 'preferredUsername' ], - model: () => ActorModel.unscoped(), + model: ActorModel.unscoped(), required: true, as: 'ActorFollower', include: [ { attributes: [ 'id', 'name' ], - model: () => AccountModel.unscoped(), + model: AccountModel.unscoped(), required: true }, { attributes: [ 'filename' ], - model: () => AvatarModel.unscoped(), + model: AvatarModel.unscoped(), required: false }, { attributes: [ 'host' ], - model: () => ServerModel.unscoped(), + model: ServerModel.unscoped(), required: false } ] }, { attributes: [ 'preferredUsername' ], - model: () => ActorModel.unscoped(), + model: ActorModel.unscoped(), required: true, as: 'ActorFollowing', include: [ @@ -147,9 +147,9 @@ function buildAccountInclude (required: boolean, withActor = false) { }, buildAccountInclude(false, true) - ] as any // FIXME: sequelize typings + ] } -}) +})) @Table({ tableName: 'userNotification', indexes: [ @@ -212,7 +212,7 @@ function buildAccountInclude (required: boolean, withActor = false) { } } } - ] as any // FIXME: sequelize typings + ] as (ModelIndexesOptions & { where?: WhereOptions })[] }) export class UserNotificationModel extends Model { @@ -357,7 +357,7 @@ export class UserNotificationModel extends Model { where: { userId, id: { - [Op.any]: notificationIds + [Op.in]: notificationIds // FIXME: sequelize ANY seems broken } } } diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 8bd0397dd..4a9acd703 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,4 +1,4 @@ -import * as Sequelize from 'sequelize' +import { FindOptions, literal, Op, QueryTypes } from 'sequelize' import { AfterDestroy, AfterUpdate, @@ -56,33 +56,33 @@ enum ScopeNames { WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' } -@DefaultScope({ +@DefaultScope(() => ({ include: [ { - model: () => AccountModel, + model: AccountModel, required: true }, { - model: () => UserNotificationSettingModel, + model: UserNotificationSettingModel, required: true } ] -}) -@Scopes({ +})) +@Scopes(() => ({ [ScopeNames.WITH_VIDEO_CHANNEL]: { include: [ { - model: () => AccountModel, + model: AccountModel, required: true, - include: [ () => VideoChannelModel ] + include: [ VideoChannelModel ] }, { - model: () => UserNotificationSettingModel, + model: UserNotificationSettingModel, required: true } - ] as any // FIXME: sequelize typings + ] } -}) +})) @Table({ tableName: 'user', indexes: [ @@ -233,26 +233,26 @@ export class UserModel extends Model { let where = undefined if (search) { where = { - [Sequelize.Op.or]: [ + [Op.or]: [ { email: { - [Sequelize.Op.iLike]: '%' + search + '%' + [Op.iLike]: '%' + search + '%' } }, { username: { - [ Sequelize.Op.iLike ]: '%' + search + '%' + [ Op.iLike ]: '%' + search + '%' } } ] } } - const query = { + const query: FindOptions = { attributes: { include: [ [ - Sequelize.literal( + literal( '(' + 'SELECT COALESCE(SUM("size"), 0) ' + 'FROM (' + @@ -265,7 +265,7 @@ export class UserModel extends Model { ')' ), 'videoQuotaUsed' - ] as any // FIXME: typings + ] ] }, offset: start, @@ -291,7 +291,7 @@ export class UserModel extends Model { const query = { where: { role: { - [Sequelize.Op.in]: roles + [Op.in]: roles } } } @@ -387,7 +387,7 @@ export class UserModel extends Model { const query = { where: { - [ Sequelize.Op.or ]: [ { username }, { email } ] + [ Op.or ]: [ { username }, { email } ] } } @@ -510,7 +510,7 @@ export class UserModel extends Model { const query = { where: { username: { - [ Sequelize.Op.like ]: `%${search}%` + [ Op.like ]: `%${search}%` } }, limit: 10 @@ -591,15 +591,11 @@ export class UserModel extends Model { const uploadedTotal = videoFile.size + totalBytes const uploadedDaily = videoFile.size + totalBytesDaily - if (this.videoQuotaDaily === -1) { - return uploadedTotal < this.videoQuota - } - if (this.videoQuota === -1) { - return uploadedDaily < this.videoQuotaDaily - } - return (uploadedTotal < this.videoQuota) && - (uploadedDaily < this.videoQuotaDaily) + if (this.videoQuotaDaily === -1) return uploadedTotal < this.videoQuota + if (this.videoQuota === -1) return uploadedDaily < this.videoQuotaDaily + + return uploadedTotal < this.videoQuota && uploadedDaily < this.videoQuotaDaily } private static generateUserQuotaBaseSQL (where?: string) { @@ -619,14 +615,14 @@ export class UserModel extends Model { private static getTotalRawQuery (query: string, userId: number) { const options = { bind: { userId }, - type: Sequelize.QueryTypes.SELECT as Sequelize.QueryTypes.SELECT + type: QueryTypes.SELECT as QueryTypes.SELECT } - return UserModel.sequelize.query<{ total: number }>(query, options) + return UserModel.sequelize.query<{ total: string }>(query, options) .then(([ { total } ]) => { if (total === null) return 0 - return parseInt(total + '', 10) + return parseInt(total, 10) }) } } -- cgit v1.2.3