aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-18 12:00:49 +0200
committerChocobozzz <me@florianbigard.com>2018-09-19 09:54:37 +0200
commit627621c1e8d37c33f7b3dd59f4c8907b12c630bc (patch)
tree007e7118f482c543d3898b222b62c185fda4fd2b /server/models/video/video.ts
parente972e046dbe9b499944c4fab9220eee13e31ac1b (diff)
downloadPeerTube-627621c1e8d37c33f7b3dd59f4c8907b12c630bc.tar.gz
PeerTube-627621c1e8d37c33f7b3dd59f4c8907b12c630bc.tar.zst
PeerTube-627621c1e8d37c33f7b3dd59f4c8907b12c630bc.zip
Optimize SQL requests of watch page API endpoints
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r--server/models/video/video.ts74
1 files changed, 41 insertions, 33 deletions
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index ce856aed2..c7cd2890c 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -91,6 +91,7 @@ import {
91 videoModelToFormattedDetailsJSON, 91 videoModelToFormattedDetailsJSON,
92 videoModelToFormattedJSON 92 videoModelToFormattedJSON
93} from './video-format-utils' 93} from './video-format-utils'
94import * as validator from 'validator'
94 95
95// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation 96// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
96const indexes: Sequelize.DefineIndexesOptions[] = [ 97const indexes: Sequelize.DefineIndexesOptions[] = [
@@ -466,6 +467,7 @@ type AvailableForListIDsOptions = {
466 required: false, 467 required: false,
467 include: [ 468 include: [
468 { 469 {
470 attributes: [ 'fileUrl' ],
469 model: () => VideoRedundancyModel.unscoped(), 471 model: () => VideoRedundancyModel.unscoped(),
470 required: false 472 required: false
471 } 473 }
@@ -1062,44 +1064,34 @@ export class VideoModel extends Model<VideoModel> {
1062 return VideoModel.getAvailableForApi(query, queryOptions) 1064 return VideoModel.getAvailableForApi(query, queryOptions)
1063 } 1065 }
1064 1066
1065 static load (id: number, t?: Sequelize.Transaction) { 1067 static load (id: number | string, t?: Sequelize.Transaction) {
1066 return VideoModel.findById(id, { transaction: t }) 1068 const where = VideoModel.buildWhereIdOrUUID(id)
1067 } 1069 const options = {
1068 1070 where,
1069 static loadWithFile (id: number, t?: Sequelize.Transaction, logging?: boolean) { 1071 transaction: t
1070 return VideoModel.scope(ScopeNames.WITH_FILES)
1071 .findById(id, { transaction: t, logging })
1072 }
1073
1074 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
1075 const query: IFindOptions<VideoModel> = {
1076 where: {
1077 url
1078 }
1079 } 1072 }
1080 1073
1081 if (t !== undefined) query.transaction = t 1074 return VideoModel.findOne(options)
1082
1083 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
1084 } 1075 }
1085 1076
1086 static loadAndPopulateAccountAndServerAndTags (id: number) { 1077 static loadOnlyId (id: number | string, t?: Sequelize.Transaction) {
1078 const where = VideoModel.buildWhereIdOrUUID(id)
1079
1087 const options = { 1080 const options = {
1088 order: [ [ 'Tags', 'name', 'ASC' ] ] 1081 attributes: [ 'id' ],
1082 where,
1083 transaction: t
1089 } 1084 }
1090 1085
1091 return VideoModel 1086 return VideoModel.findOne(options)
1092 .scope([ 1087 }
1093 ScopeNames.WITH_TAGS, 1088
1094 ScopeNames.WITH_BLACKLISTED, 1089 static loadWithFile (id: number, t?: Sequelize.Transaction, logging?: boolean) {
1095 ScopeNames.WITH_FILES, 1090 return VideoModel.scope(ScopeNames.WITH_FILES)
1096 ScopeNames.WITH_ACCOUNT_DETAILS, 1091 .findById(id, { transaction: t, logging })
1097 ScopeNames.WITH_SCHEDULED_UPDATE
1098 ])
1099 .findById(id, options)
1100 } 1092 }
1101 1093
1102 static loadByUUID (uuid: string) { 1094 static loadByUUIDWithFile (uuid: string) {
1103 const options = { 1095 const options = {
1104 where: { 1096 where: {
1105 uuid 1097 uuid
@@ -1111,12 +1103,24 @@ export class VideoModel extends Model<VideoModel> {
1111 .findOne(options) 1103 .findOne(options)
1112 } 1104 }
1113 1105
1114 static loadByUUIDAndPopulateAccountAndServerAndTags (uuid: string, t?: Sequelize.Transaction) { 1106 static loadByUrlAndPopulateAccount (url: string, t?: Sequelize.Transaction) {
1107 const query: IFindOptions<VideoModel> = {
1108 where: {
1109 url
1110 }
1111 }
1112
1113 if (t !== undefined) query.transaction = t
1114
1115 return VideoModel.scope([ ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_FILES ]).findOne(query)
1116 }
1117
1118 static loadAndPopulateAccountAndServerAndTags (id: number | string, t?: Sequelize.Transaction) {
1119 const where = VideoModel.buildWhereIdOrUUID(id)
1120
1115 const options = { 1121 const options = {
1116 order: [ [ 'Tags', 'name', 'ASC' ] ], 1122 order: [ [ 'Tags', 'name', 'ASC' ] ],
1117 where: { 1123 where,
1118 uuid
1119 },
1120 transaction: t 1124 transaction: t
1121 } 1125 }
1122 1126
@@ -1277,6 +1281,10 @@ export class VideoModel extends Model<VideoModel> {
1277 return VIDEO_STATES[ id ] || 'Unknown' 1281 return VIDEO_STATES[ id ] || 'Unknown'
1278 } 1282 }
1279 1283
1284 static buildWhereIdOrUUID (id: number | string) {
1285 return validator.isInt('' + id) ? { id } : { uuid: id }
1286 }
1287
1280 getOriginalFile () { 1288 getOriginalFile () {
1281 if (Array.isArray(this.VideoFiles) === false) return undefined 1289 if (Array.isArray(this.VideoFiles) === false) return undefined
1282 1290