]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Refactor a little bit raw sql builders
authorChocobozzz <me@florianbigard.com>
Thu, 3 Mar 2022 09:23:44 +0000 (10:23 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 3 Mar 2022 09:23:44 +0000 (10:23 +0100)
server/models/shared/abstract-run-query.ts [moved from server/models/video/sql/video/shared/abstract-run-query.ts with 70% similarity]
server/models/shared/index.ts
server/models/user/sql/user-notitication-list-query-builder.ts
server/models/user/user-notification.ts
server/models/video/sql/video/shared/abstract-video-query-builder.ts
server/models/video/sql/video/shared/video-file-query-builder.ts
server/models/video/sql/video/shared/video-model-builder.ts
server/models/video/sql/video/shared/video-table-attributes.ts
server/models/video/sql/video/video-model-get-query-builder.ts
server/models/video/sql/video/videos-id-list-query-builder.ts
server/models/video/sql/video/videos-model-list-query-builder.ts

similarity index 70%
rename from server/models/video/sql/video/shared/abstract-run-query.ts
rename to server/models/shared/abstract-run-query.ts
index 8e7a7642d0513835988ef92727bc7b6f751606ca..c39b7bcfe0bf5e33d1e7af9900037072db0b6b68 100644 (file)
@@ -7,18 +7,20 @@ import { QueryTypes, Sequelize, Transaction } from 'sequelize'
  */
 
 export class AbstractRunQuery {
-  protected sequelize: Sequelize
-
   protected query: string
   protected replacements: any = {}
 
-  protected runQuery (options: { transaction?: Transaction, logging?: boolean } = {}) {
+  constructor (protected readonly sequelize: Sequelize) {
+
+  }
+
+  protected runQuery (options: { nest?: boolean, transaction?: Transaction, logging?: boolean } = {}) {
     const queryOptions = {
       transaction: options.transaction,
       logging: options.logging,
       replacements: this.replacements,
       type: QueryTypes.SELECT as QueryTypes.SELECT,
-      nest: false
+      nest: options.nest ?? false
     }
 
     return this.sequelize.query<any>(this.query, queryOptions)
index 802404555a2565d0ba9ec9379551f648ce71516d..04528929c71351946da4bec8ede3500634bb2245 100644 (file)
@@ -1,3 +1,4 @@
+export * from './abstract-run-query'
 export * from './model-builder'
 export * from './query'
 export * from './update'
index 9eae4fc22cecbb1d0c7847c931d5557672927b27..6a6a71e3ade02d50a8f8bc61f911e1fd235cf808 100644 (file)
@@ -1,5 +1,5 @@
-import { QueryTypes, Sequelize } from 'sequelize'
-import { ModelBuilder } from '@server/models/shared'
+import { Sequelize } from 'sequelize'
+import { AbstractRunQuery, ModelBuilder } from '@server/models/shared'
 import { getSort } from '@server/models/utils'
 import { UserNotificationModelForApi } from '@server/types/models'
 import { ActorImageType } from '@shared/models'
@@ -10,28 +10,23 @@ export interface ListNotificationsOptions {
   sort: string
   offset: number
   limit: number
-  sequelize: Sequelize
 }
 
-export class UserNotificationListQueryBuilder {
+export class UserNotificationListQueryBuilder extends AbstractRunQuery {
   private innerQuery: string
-  private replacements: any = {}
-  private query: string
-
-  constructor (private readonly options: ListNotificationsOptions) {
 
+  constructor (
+    protected readonly sequelize: Sequelize,
+    private readonly options: ListNotificationsOptions
+  ) {
+    super(sequelize)
   }
 
   async listNotifications () {
     this.buildQuery()
 
-    const results = await this.options.sequelize.query(this.query, {
-      replacements: this.replacements,
-      type: QueryTypes.SELECT,
-      nest: true
-    })
-
-    const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.options.sequelize)
+    const results = await this.runQuery({ nest: true })
+    const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.sequelize)
 
     return modelBuilder.createModels(results, 'UserNotification')
   }
index eca127e7e1075fc44414db66ade1928b21d12094..6209cb4bfa81ae8f8c4f35a1d40551dbb8088eac 100644 (file)
@@ -249,8 +249,7 @@ export class UserNotificationModel extends Model<Partial<AttributesOnly<UserNoti
       offset: start,
       limit: count,
       sort,
-      where,
-      sequelize: this.sequelize
+      where
     }
 
     if (unread !== undefined) query.where['read'] = !unread
@@ -261,7 +260,7 @@ export class UserNotificationModel extends Model<Partial<AttributesOnly<UserNoti
 
       count === 0
         ? [] as UserNotificationModelForApi[]
-        : new UserNotificationListQueryBuilder(query).listNotifications()
+        : new UserNotificationListQueryBuilder(this.sequelize, query).listNotifications()
     ]).then(([ total, data ]) => ({ total, data }))
   }
 
index 490e5e6e04508582b9100bf3922bf1d6a2d3e452..b79d20ade50fe2f423883963f2e09c6d16e2fc74 100644 (file)
@@ -1,8 +1,9 @@
+import { Sequelize } from 'sequelize'
+import validator from 'validator'
 import { createSafeIn } from '@server/models/utils'
 import { MUserAccountId } from '@server/types/models'
 import { ActorImageType } from '@shared/models'
-import validator from 'validator'
-import { AbstractRunQuery } from './abstract-run-query'
+import { AbstractRunQuery } from '../../../../shared/abstract-run-query'
 import { VideoTableAttributes } from './video-table-attributes'
 
 /**
@@ -19,8 +20,11 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
 
   protected tables: VideoTableAttributes
 
-  constructor (protected readonly mode: 'list' | 'get') {
-    super()
+  constructor (
+    protected readonly sequelize: Sequelize,
+    protected readonly mode: 'list' | 'get'
+  ) {
+    super(sequelize)
 
     this.tables = new VideoTableAttributes(this.mode)
   }
index 3eb3dc07d559b86c86ad6d4244493944be1c8d76..50c12f627f9b8356d39e163a95f4c553fc1b0f57 100644 (file)
@@ -12,7 +12,7 @@ export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
   protected attributes: { [key: string]: string }
 
   constructor (protected readonly sequelize: Sequelize) {
-    super('get')
+    super(sequelize, 'get')
   }
 
   queryWebTorrentVideos (options: BuildVideoGetQueryOptions) {
index 166ff9d31bc6166abe6ca79ad29fba15dafd0e60..0a2beb7dbcbd2d70aeb53dc6cd1fd6a3c0f3f008 100644 (file)
@@ -51,8 +51,8 @@ export class VideoModelBuilder {
   private readonly buildOpts = { raw: true, isNewRecord: false }
 
   constructor (
-    readonly mode: 'get' | 'list',
-    readonly tables: VideoTableAttributes
+    private readonly mode: 'get' | 'list',
+    private readonly tables: VideoTableAttributes
   ) {
 
   }
index df2ed3fb040c59f76f7d5d508777a67c05a93fce..f4d9e99fd2da76e39aea34297846f07b6a904297 100644 (file)
@@ -6,7 +6,7 @@
  */
 export class VideoTableAttributes {
 
-  constructor (readonly mode: 'get' | 'list') {
+  constructor (private readonly mode: 'get' | 'list') {
 
   }
 
index a65c96097cae77741f21d7728f4677548eca8d41..b0879c9ac2abd3b4a8e4de1f384450133d2b4cc0 100644 (file)
@@ -110,7 +110,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
   ])
 
   constructor (protected readonly sequelize: Sequelize) {
-    super('get')
+    super(sequelize, 'get')
   }
 
   queryVideos (options: BuildVideoGetQueryOptions) {
index 098e153590ce2ea35459f206a91461bc45c7ff1c..19aff631de4e651dde0bfd5526371434a515375e 100644 (file)
@@ -5,7 +5,7 @@ import { WEBSERVER } from '@server/initializers/constants'
 import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
 import { MUserAccountId, MUserId } from '@server/types/models'
 import { VideoInclude, VideoPrivacy, VideoState } from '@shared/models'
-import { AbstractRunQuery } from './shared/abstract-run-query'
+import { AbstractRunQuery } from '../../../shared/abstract-run-query'
 
 /**
  *
@@ -93,7 +93,7 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
   private offset = ''
 
   constructor (protected readonly sequelize: Sequelize) {
-    super()
+    super(sequelize)
   }
 
   queryVideoIds (options: BuildVideosListQueryOptions) {
index b15b29ec3a59f42a219c2b363357e2afd64d9b38..2a4afc3894e81da9657252d2a0a2290f07b6b985 100644 (file)
@@ -19,7 +19,7 @@ export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
   private readonly videoModelBuilder: VideoModelBuilder
 
   constructor (protected readonly sequelize: Sequelize) {
-    super('list')
+    super(sequelize, 'list')
 
     this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
   }