aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/sql/video-model-get-query-builder.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-10 14:43:55 +0200
committerChocobozzz <me@florianbigard.com>2021-06-10 15:26:18 +0200
commitd9bf974f5df787bbeaab5b04949ca91a2b3ca2a3 (patch)
treeaa02ee0cc28c845432e91da43b1e6de2a2f04039 /server/models/video/sql/video-model-get-query-builder.ts
parente5dbd5084e7ae91ce118c0bccd5b84c47b88c55f (diff)
downloadPeerTube-d9bf974f5df787bbeaab5b04949ca91a2b3ca2a3.tar.gz
PeerTube-d9bf974f5df787bbeaab5b04949ca91a2b3ca2a3.tar.zst
PeerTube-d9bf974f5df787bbeaab5b04949ca91a2b3ca2a3.zip
Use raw SQL for video get request
Diffstat (limited to 'server/models/video/sql/video-model-get-query-builder.ts')
-rw-r--r--server/models/video/sql/video-model-get-query-builder.ts86
1 files changed, 86 insertions, 0 deletions
diff --git a/server/models/video/sql/video-model-get-query-builder.ts b/server/models/video/sql/video-model-get-query-builder.ts
new file mode 100644
index 000000000..0a3723e63
--- /dev/null
+++ b/server/models/video/sql/video-model-get-query-builder.ts
@@ -0,0 +1,86 @@
1import { Sequelize, Transaction } from 'sequelize'
2import validator from 'validator'
3import { AbstractVideosModelQueryBuilder } from './shared/abstract-videos-model-query-builder'
4
5export type BuildVideoGetQueryOptions = {
6 id: number | string
7 transaction?: Transaction
8 userId?: number
9 forGetAPI?: boolean
10}
11
12export class VideosModelGetQueryBuilder extends AbstractVideosModelQueryBuilder {
13 protected attributes: { [key: string]: string }
14 protected joins: string[] = []
15 protected where: string
16
17 constructor (protected readonly sequelize: Sequelize) {
18 super('get')
19 }
20
21 queryVideos (options: BuildVideoGetQueryOptions) {
22 this.buildGetQuery(options)
23
24 return this.runQuery(options.transaction, true).then(rows => {
25 const videos = this.videoModelBuilder.buildVideosFromRows(rows)
26
27 if (videos.length > 1) {
28 throw new Error('Video results is more than ')
29 }
30
31 if (videos.length === 0) return null
32 return videos[0]
33 })
34 }
35
36 private buildGetQuery (options: BuildVideoGetQueryOptions) {
37 this.attributes = {
38 '"video".*': ''
39 }
40
41 this.includeChannels()
42 this.includeAccounts()
43
44 this.includeTags()
45
46 this.includeThumbnails()
47
48 this.includeFiles()
49
50 this.includeBlacklisted()
51
52 this.includeScheduleUpdate()
53
54 this.includeLive()
55
56 if (options.userId) {
57 this.includeUserHistory(options.userId)
58 }
59
60 if (options.forGetAPI === true) {
61 this.includeTrackers()
62 this.includeRedundancies()
63 }
64
65 this.whereId(options.id)
66
67 const select = this.buildSelect()
68 const order = this.buildOrder()
69
70 this.query = `${select} FROM "video" ${this.joins.join(' ')} ${this.where} ${order}`
71 }
72
73 private whereId (id: string | number) {
74 if (validator.isInt('' + id)) {
75 this.where = 'WHERE "video".id = :videoId'
76 } else {
77 this.where = 'WHERE uuid = :videoId'
78 }
79
80 this.replacements.videoId = id
81 }
82
83 private buildOrder () {
84 return 'ORDER BY "Tags"."name" ASC'
85 }
86}