]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/sql/videos-model-list-query-builder.ts
Optimize rows parsing
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / videos-model-list-query-builder.ts
1 import { Sequelize } from 'sequelize'
2 import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
3 import { VideoModelBuilder } from './shared/video-model-builder'
4 import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
5
6 /**
7 *
8 * Build videos list SQL query and create video models
9 *
10 */
11
12 export class VideosModelListQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
14 protected joins: string[] = []
15
16 private innerQuery: string
17 private innerSort: string
18
19 private readonly videoModelBuilder: VideoModelBuilder
20
21 constructor (protected readonly sequelize: Sequelize) {
22 super('list')
23
24 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
25 }
26
27 queryVideos (options: BuildVideosListQueryOptions) {
28 this.buildInnerQuery(options)
29 this.buildListQueryFromIdsQuery(options)
30
31 return this.runQuery(undefined).then(rows => this.videoModelBuilder.buildVideosFromRows(rows))
32 }
33
34 private buildInnerQuery (options: BuildVideosListQueryOptions) {
35 const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
36 const { query, sort, replacements } = idsQueryBuilder.getIdsListQueryAndSort(options)
37
38 this.replacements = replacements
39 this.innerQuery = query
40 this.innerSort = sort
41 }
42
43 private buildListQueryFromIdsQuery (options: BuildVideosListQueryOptions) {
44 this.attributes = {
45 '"video".*': ''
46 }
47
48 this.joins = [ 'INNER JOIN "video" ON "tmp"."id" = "video"."id"' ]
49
50 this.includeChannels()
51 this.includeAccounts()
52 this.includeThumbnails()
53
54 if (options.withFiles) {
55 this.includeWebtorrentFiles(false)
56 this.includeStreamingPlaylistFiles(false)
57 }
58
59 if (options.user) {
60 this.includeUserHistory(options.user.id)
61 }
62
63 if (options.videoPlaylistId) {
64 this.includePlaylist(options.videoPlaylistId)
65 }
66
67 const select = this.buildSelect()
68
69 this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins.join(' ')} ${this.innerSort}`
70 }
71 }