]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/sql/videos-model-list-query-builder.ts
Use raw SQL for most of video queries
[github/Chocobozzz/PeerTube.git] / server / models / video / sql / videos-model-list-query-builder.ts
CommitLineData
e5dbd508 1import { Sequelize } from 'sequelize'
d9bf974f 2import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
1d43c3a6 3import { VideoModelBuilder } from './shared/video-model-builder'
e5dbd508
C
4import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder'
5
1d43c3a6
C
6/**
7 *
8 * Build videos list SQL query and create video models
9 *
10 */
11
d9bf974f
C
12export class VideosModelListQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
e5dbd508
C
14
15 private innerQuery: string
16 private innerSort: string
17
1d43c3a6
C
18 private readonly videoModelBuilder: VideoModelBuilder
19
e5dbd508 20 constructor (protected readonly sequelize: Sequelize) {
d9bf974f 21 super('list')
1d43c3a6 22
17bb4538 23 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
e5dbd508
C
24 }
25
26 queryVideos (options: BuildVideosListQueryOptions) {
27 this.buildInnerQuery(options)
28 this.buildListQueryFromIdsQuery(options)
29
71d4af1e
C
30 return this.runQuery()
31 .then(rows => this.videoModelBuilder.buildVideosFromRows(rows))
e5dbd508
C
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
3c79c2ce 48 this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
e5dbd508
C
49
50 this.includeChannels()
51 this.includeAccounts()
52 this.includeThumbnails()
53
54 if (options.withFiles) {
1d43c3a6
C
55 this.includeWebtorrentFiles(false)
56 this.includeStreamingPlaylistFiles(false)
e5dbd508
C
57 }
58
59 if (options.user) {
d9bf974f 60 this.includeUserHistory(options.user.id)
e5dbd508
C
61 }
62
63 if (options.videoPlaylistId) {
64 this.includePlaylist(options.videoPlaylistId)
65 }
66
67 const select = this.buildSelect()
68
3c79c2ce 69 this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
e5dbd508 70 }
e5dbd508 71}