aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-03 10:23:44 +0100
committerChocobozzz <me@florianbigard.com>2022-03-03 10:23:44 +0100
commit156c44c8f6522686635089ecff7a358b4145d545 (patch)
tree770d4b8b154ee0942fc7e37ffd697a3b7ec02c71
parentbf521f51fc561b38d2e1432213a75427f671dda9 (diff)
downloadPeerTube-156c44c8f6522686635089ecff7a358b4145d545.tar.gz
PeerTube-156c44c8f6522686635089ecff7a358b4145d545.tar.zst
PeerTube-156c44c8f6522686635089ecff7a358b4145d545.zip
Refactor a little bit raw sql builders
-rw-r--r--server/models/shared/abstract-run-query.ts (renamed from server/models/video/sql/video/shared/abstract-run-query.ts)10
-rw-r--r--server/models/shared/index.ts1
-rw-r--r--server/models/user/sql/user-notitication-list-query-builder.ts25
-rw-r--r--server/models/user/user-notification.ts5
-rw-r--r--server/models/video/sql/video/shared/abstract-video-query-builder.ts12
-rw-r--r--server/models/video/sql/video/shared/video-file-query-builder.ts2
-rw-r--r--server/models/video/sql/video/shared/video-model-builder.ts4
-rw-r--r--server/models/video/sql/video/shared/video-table-attributes.ts2
-rw-r--r--server/models/video/sql/video/video-model-get-query-builder.ts2
-rw-r--r--server/models/video/sql/video/videos-id-list-query-builder.ts4
-rw-r--r--server/models/video/sql/video/videos-model-list-query-builder.ts2
11 files changed, 35 insertions, 34 deletions
diff --git a/server/models/video/sql/video/shared/abstract-run-query.ts b/server/models/shared/abstract-run-query.ts
index 8e7a7642d..c39b7bcfe 100644
--- a/server/models/video/sql/video/shared/abstract-run-query.ts
+++ b/server/models/shared/abstract-run-query.ts
@@ -7,18 +7,20 @@ import { QueryTypes, Sequelize, Transaction } from 'sequelize'
7 */ 7 */
8 8
9export class AbstractRunQuery { 9export class AbstractRunQuery {
10 protected sequelize: Sequelize
11
12 protected query: string 10 protected query: string
13 protected replacements: any = {} 11 protected replacements: any = {}
14 12
15 protected runQuery (options: { transaction?: Transaction, logging?: boolean } = {}) { 13 constructor (protected readonly sequelize: Sequelize) {
14
15 }
16
17 protected runQuery (options: { nest?: boolean, transaction?: Transaction, logging?: boolean } = {}) {
16 const queryOptions = { 18 const queryOptions = {
17 transaction: options.transaction, 19 transaction: options.transaction,
18 logging: options.logging, 20 logging: options.logging,
19 replacements: this.replacements, 21 replacements: this.replacements,
20 type: QueryTypes.SELECT as QueryTypes.SELECT, 22 type: QueryTypes.SELECT as QueryTypes.SELECT,
21 nest: false 23 nest: options.nest ?? false
22 } 24 }
23 25
24 return this.sequelize.query<any>(this.query, queryOptions) 26 return this.sequelize.query<any>(this.query, queryOptions)
diff --git a/server/models/shared/index.ts b/server/models/shared/index.ts
index 802404555..04528929c 100644
--- a/server/models/shared/index.ts
+++ b/server/models/shared/index.ts
@@ -1,3 +1,4 @@
1export * from './abstract-run-query'
1export * from './model-builder' 2export * from './model-builder'
2export * from './query' 3export * from './query'
3export * from './update' 4export * from './update'
diff --git a/server/models/user/sql/user-notitication-list-query-builder.ts b/server/models/user/sql/user-notitication-list-query-builder.ts
index 9eae4fc22..6a6a71e3a 100644
--- a/server/models/user/sql/user-notitication-list-query-builder.ts
+++ b/server/models/user/sql/user-notitication-list-query-builder.ts
@@ -1,5 +1,5 @@
1import { QueryTypes, Sequelize } from 'sequelize' 1import { Sequelize } from 'sequelize'
2import { ModelBuilder } from '@server/models/shared' 2import { AbstractRunQuery, ModelBuilder } from '@server/models/shared'
3import { getSort } from '@server/models/utils' 3import { getSort } from '@server/models/utils'
4import { UserNotificationModelForApi } from '@server/types/models' 4import { UserNotificationModelForApi } from '@server/types/models'
5import { ActorImageType } from '@shared/models' 5import { ActorImageType } from '@shared/models'
@@ -10,28 +10,23 @@ export interface ListNotificationsOptions {
10 sort: string 10 sort: string
11 offset: number 11 offset: number
12 limit: number 12 limit: number
13 sequelize: Sequelize
14} 13}
15 14
16export class UserNotificationListQueryBuilder { 15export class UserNotificationListQueryBuilder extends AbstractRunQuery {
17 private innerQuery: string 16 private innerQuery: string
18 private replacements: any = {}
19 private query: string
20
21 constructor (private readonly options: ListNotificationsOptions) {
22 17
18 constructor (
19 protected readonly sequelize: Sequelize,
20 private readonly options: ListNotificationsOptions
21 ) {
22 super(sequelize)
23 } 23 }
24 24
25 async listNotifications () { 25 async listNotifications () {
26 this.buildQuery() 26 this.buildQuery()
27 27
28 const results = await this.options.sequelize.query(this.query, { 28 const results = await this.runQuery({ nest: true })
29 replacements: this.replacements, 29 const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.sequelize)
30 type: QueryTypes.SELECT,
31 nest: true
32 })
33
34 const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.options.sequelize)
35 30
36 return modelBuilder.createModels(results, 'UserNotification') 31 return modelBuilder.createModels(results, 'UserNotification')
37 } 32 }
diff --git a/server/models/user/user-notification.ts b/server/models/user/user-notification.ts
index eca127e7e..6209cb4bf 100644
--- a/server/models/user/user-notification.ts
+++ b/server/models/user/user-notification.ts
@@ -249,8 +249,7 @@ export class UserNotificationModel extends Model<Partial<AttributesOnly<UserNoti
249 offset: start, 249 offset: start,
250 limit: count, 250 limit: count,
251 sort, 251 sort,
252 where, 252 where
253 sequelize: this.sequelize
254 } 253 }
255 254
256 if (unread !== undefined) query.where['read'] = !unread 255 if (unread !== undefined) query.where['read'] = !unread
@@ -261,7 +260,7 @@ export class UserNotificationModel extends Model<Partial<AttributesOnly<UserNoti
261 260
262 count === 0 261 count === 0
263 ? [] as UserNotificationModelForApi[] 262 ? [] as UserNotificationModelForApi[]
264 : new UserNotificationListQueryBuilder(query).listNotifications() 263 : new UserNotificationListQueryBuilder(this.sequelize, query).listNotifications()
265 ]).then(([ total, data ]) => ({ total, data })) 264 ]).then(([ total, data ]) => ({ total, data }))
266 } 265 }
267 266
diff --git a/server/models/video/sql/video/shared/abstract-video-query-builder.ts b/server/models/video/sql/video/shared/abstract-video-query-builder.ts
index 490e5e6e0..b79d20ade 100644
--- a/server/models/video/sql/video/shared/abstract-video-query-builder.ts
+++ b/server/models/video/sql/video/shared/abstract-video-query-builder.ts
@@ -1,8 +1,9 @@
1import { Sequelize } from 'sequelize'
2import validator from 'validator'
1import { createSafeIn } from '@server/models/utils' 3import { createSafeIn } from '@server/models/utils'
2import { MUserAccountId } from '@server/types/models' 4import { MUserAccountId } from '@server/types/models'
3import { ActorImageType } from '@shared/models' 5import { ActorImageType } from '@shared/models'
4import validator from 'validator' 6import { AbstractRunQuery } from '../../../../shared/abstract-run-query'
5import { AbstractRunQuery } from './abstract-run-query'
6import { VideoTableAttributes } from './video-table-attributes' 7import { VideoTableAttributes } from './video-table-attributes'
7 8
8/** 9/**
@@ -19,8 +20,11 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
19 20
20 protected tables: VideoTableAttributes 21 protected tables: VideoTableAttributes
21 22
22 constructor (protected readonly mode: 'list' | 'get') { 23 constructor (
23 super() 24 protected readonly sequelize: Sequelize,
25 protected readonly mode: 'list' | 'get'
26 ) {
27 super(sequelize)
24 28
25 this.tables = new VideoTableAttributes(this.mode) 29 this.tables = new VideoTableAttributes(this.mode)
26 } 30 }
diff --git a/server/models/video/sql/video/shared/video-file-query-builder.ts b/server/models/video/sql/video/shared/video-file-query-builder.ts
index 3eb3dc07d..50c12f627 100644
--- a/server/models/video/sql/video/shared/video-file-query-builder.ts
+++ b/server/models/video/sql/video/shared/video-file-query-builder.ts
@@ -12,7 +12,7 @@ export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
12 protected attributes: { [key: string]: string } 12 protected attributes: { [key: string]: string }
13 13
14 constructor (protected readonly sequelize: Sequelize) { 14 constructor (protected readonly sequelize: Sequelize) {
15 super('get') 15 super(sequelize, 'get')
16 } 16 }
17 17
18 queryWebTorrentVideos (options: BuildVideoGetQueryOptions) { 18 queryWebTorrentVideos (options: BuildVideoGetQueryOptions) {
diff --git a/server/models/video/sql/video/shared/video-model-builder.ts b/server/models/video/sql/video/shared/video-model-builder.ts
index 166ff9d31..0a2beb7db 100644
--- a/server/models/video/sql/video/shared/video-model-builder.ts
+++ b/server/models/video/sql/video/shared/video-model-builder.ts
@@ -51,8 +51,8 @@ export class VideoModelBuilder {
51 private readonly buildOpts = { raw: true, isNewRecord: false } 51 private readonly buildOpts = { raw: true, isNewRecord: false }
52 52
53 constructor ( 53 constructor (
54 readonly mode: 'get' | 'list', 54 private readonly mode: 'get' | 'list',
55 readonly tables: VideoTableAttributes 55 private readonly tables: VideoTableAttributes
56 ) { 56 ) {
57 57
58 } 58 }
diff --git a/server/models/video/sql/video/shared/video-table-attributes.ts b/server/models/video/sql/video/shared/video-table-attributes.ts
index df2ed3fb0..f4d9e99fd 100644
--- a/server/models/video/sql/video/shared/video-table-attributes.ts
+++ b/server/models/video/sql/video/shared/video-table-attributes.ts
@@ -6,7 +6,7 @@
6 */ 6 */
7export class VideoTableAttributes { 7export class VideoTableAttributes {
8 8
9 constructor (readonly mode: 'get' | 'list') { 9 constructor (private readonly mode: 'get' | 'list') {
10 10
11 } 11 }
12 12
diff --git a/server/models/video/sql/video/video-model-get-query-builder.ts b/server/models/video/sql/video/video-model-get-query-builder.ts
index a65c96097..b0879c9ac 100644
--- a/server/models/video/sql/video/video-model-get-query-builder.ts
+++ b/server/models/video/sql/video/video-model-get-query-builder.ts
@@ -110,7 +110,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
110 ]) 110 ])
111 111
112 constructor (protected readonly sequelize: Sequelize) { 112 constructor (protected readonly sequelize: Sequelize) {
113 super('get') 113 super(sequelize, 'get')
114 } 114 }
115 115
116 queryVideos (options: BuildVideoGetQueryOptions) { 116 queryVideos (options: BuildVideoGetQueryOptions) {
diff --git a/server/models/video/sql/video/videos-id-list-query-builder.ts b/server/models/video/sql/video/videos-id-list-query-builder.ts
index 098e15359..19aff631d 100644
--- a/server/models/video/sql/video/videos-id-list-query-builder.ts
+++ b/server/models/video/sql/video/videos-id-list-query-builder.ts
@@ -5,7 +5,7 @@ import { WEBSERVER } from '@server/initializers/constants'
5import { buildDirectionAndField, createSafeIn } from '@server/models/utils' 5import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
6import { MUserAccountId, MUserId } from '@server/types/models' 6import { MUserAccountId, MUserId } from '@server/types/models'
7import { VideoInclude, VideoPrivacy, VideoState } from '@shared/models' 7import { VideoInclude, VideoPrivacy, VideoState } from '@shared/models'
8import { AbstractRunQuery } from './shared/abstract-run-query' 8import { AbstractRunQuery } from '../../../shared/abstract-run-query'
9 9
10/** 10/**
11 * 11 *
@@ -93,7 +93,7 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
93 private offset = '' 93 private offset = ''
94 94
95 constructor (protected readonly sequelize: Sequelize) { 95 constructor (protected readonly sequelize: Sequelize) {
96 super() 96 super(sequelize)
97 } 97 }
98 98
99 queryVideoIds (options: BuildVideosListQueryOptions) { 99 queryVideoIds (options: BuildVideosListQueryOptions) {
diff --git a/server/models/video/sql/video/videos-model-list-query-builder.ts b/server/models/video/sql/video/videos-model-list-query-builder.ts
index b15b29ec3..2a4afc389 100644
--- a/server/models/video/sql/video/videos-model-list-query-builder.ts
+++ b/server/models/video/sql/video/videos-model-list-query-builder.ts
@@ -19,7 +19,7 @@ export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
19 private readonly videoModelBuilder: VideoModelBuilder 19 private readonly videoModelBuilder: VideoModelBuilder
20 20
21 constructor (protected readonly sequelize: Sequelize) { 21 constructor (protected readonly sequelize: Sequelize) {
22 super('list') 22 super(sequelize, 'list')
23 23
24 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables) 24 this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
25 } 25 }