Refactor a little bit raw sql builders
This commit is contained in:
parent
bf521f51fc
commit
156c44c8f6
|
@ -7,18 +7,20 @@ import { QueryTypes, Sequelize, Transaction } from 'sequelize'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class AbstractRunQuery {
|
export class AbstractRunQuery {
|
||||||
protected sequelize: Sequelize
|
|
||||||
|
|
||||||
protected query: string
|
protected query: string
|
||||||
protected replacements: any = {}
|
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 = {
|
const queryOptions = {
|
||||||
transaction: options.transaction,
|
transaction: options.transaction,
|
||||||
logging: options.logging,
|
logging: options.logging,
|
||||||
replacements: this.replacements,
|
replacements: this.replacements,
|
||||||
type: QueryTypes.SELECT as QueryTypes.SELECT,
|
type: QueryTypes.SELECT as QueryTypes.SELECT,
|
||||||
nest: false
|
nest: options.nest ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.sequelize.query<any>(this.query, queryOptions)
|
return this.sequelize.query<any>(this.query, queryOptions)
|
|
@ -1,3 +1,4 @@
|
||||||
|
export * from './abstract-run-query'
|
||||||
export * from './model-builder'
|
export * from './model-builder'
|
||||||
export * from './query'
|
export * from './query'
|
||||||
export * from './update'
|
export * from './update'
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { QueryTypes, Sequelize } from 'sequelize'
|
import { Sequelize } from 'sequelize'
|
||||||
import { ModelBuilder } from '@server/models/shared'
|
import { AbstractRunQuery, ModelBuilder } from '@server/models/shared'
|
||||||
import { getSort } from '@server/models/utils'
|
import { getSort } from '@server/models/utils'
|
||||||
import { UserNotificationModelForApi } from '@server/types/models'
|
import { UserNotificationModelForApi } from '@server/types/models'
|
||||||
import { ActorImageType } from '@shared/models'
|
import { ActorImageType } from '@shared/models'
|
||||||
|
@ -10,28 +10,23 @@ export interface ListNotificationsOptions {
|
||||||
sort: string
|
sort: string
|
||||||
offset: number
|
offset: number
|
||||||
limit: number
|
limit: number
|
||||||
sequelize: Sequelize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UserNotificationListQueryBuilder {
|
export class UserNotificationListQueryBuilder extends AbstractRunQuery {
|
||||||
private innerQuery: string
|
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 () {
|
async listNotifications () {
|
||||||
this.buildQuery()
|
this.buildQuery()
|
||||||
|
|
||||||
const results = await this.options.sequelize.query(this.query, {
|
const results = await this.runQuery({ nest: true })
|
||||||
replacements: this.replacements,
|
const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.sequelize)
|
||||||
type: QueryTypes.SELECT,
|
|
||||||
nest: true
|
|
||||||
})
|
|
||||||
|
|
||||||
const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.options.sequelize)
|
|
||||||
|
|
||||||
return modelBuilder.createModels(results, 'UserNotification')
|
return modelBuilder.createModels(results, 'UserNotification')
|
||||||
}
|
}
|
||||||
|
|
|
@ -249,8 +249,7 @@ export class UserNotificationModel extends Model<Partial<AttributesOnly<UserNoti
|
||||||
offset: start,
|
offset: start,
|
||||||
limit: count,
|
limit: count,
|
||||||
sort,
|
sort,
|
||||||
where,
|
where
|
||||||
sequelize: this.sequelize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unread !== undefined) query.where['read'] = !unread
|
if (unread !== undefined) query.where['read'] = !unread
|
||||||
|
@ -261,7 +260,7 @@ export class UserNotificationModel extends Model<Partial<AttributesOnly<UserNoti
|
||||||
|
|
||||||
count === 0
|
count === 0
|
||||||
? [] as UserNotificationModelForApi[]
|
? [] as UserNotificationModelForApi[]
|
||||||
: new UserNotificationListQueryBuilder(query).listNotifications()
|
: new UserNotificationListQueryBuilder(this.sequelize, query).listNotifications()
|
||||||
]).then(([ total, data ]) => ({ total, data }))
|
]).then(([ total, data ]) => ({ total, data }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
import { Sequelize } from 'sequelize'
|
||||||
|
import validator from 'validator'
|
||||||
import { createSafeIn } from '@server/models/utils'
|
import { createSafeIn } from '@server/models/utils'
|
||||||
import { MUserAccountId } from '@server/types/models'
|
import { MUserAccountId } from '@server/types/models'
|
||||||
import { ActorImageType } from '@shared/models'
|
import { ActorImageType } from '@shared/models'
|
||||||
import validator from 'validator'
|
import { AbstractRunQuery } from '../../../../shared/abstract-run-query'
|
||||||
import { AbstractRunQuery } from './abstract-run-query'
|
|
||||||
import { VideoTableAttributes } from './video-table-attributes'
|
import { VideoTableAttributes } from './video-table-attributes'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,8 +20,11 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
|
||||||
|
|
||||||
protected tables: VideoTableAttributes
|
protected tables: VideoTableAttributes
|
||||||
|
|
||||||
constructor (protected readonly mode: 'list' | 'get') {
|
constructor (
|
||||||
super()
|
protected readonly sequelize: Sequelize,
|
||||||
|
protected readonly mode: 'list' | 'get'
|
||||||
|
) {
|
||||||
|
super(sequelize)
|
||||||
|
|
||||||
this.tables = new VideoTableAttributes(this.mode)
|
this.tables = new VideoTableAttributes(this.mode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
|
||||||
protected attributes: { [key: string]: string }
|
protected attributes: { [key: string]: string }
|
||||||
|
|
||||||
constructor (protected readonly sequelize: Sequelize) {
|
constructor (protected readonly sequelize: Sequelize) {
|
||||||
super('get')
|
super(sequelize, 'get')
|
||||||
}
|
}
|
||||||
|
|
||||||
queryWebTorrentVideos (options: BuildVideoGetQueryOptions) {
|
queryWebTorrentVideos (options: BuildVideoGetQueryOptions) {
|
||||||
|
|
|
@ -51,8 +51,8 @@ export class VideoModelBuilder {
|
||||||
private readonly buildOpts = { raw: true, isNewRecord: false }
|
private readonly buildOpts = { raw: true, isNewRecord: false }
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
readonly mode: 'get' | 'list',
|
private readonly mode: 'get' | 'list',
|
||||||
readonly tables: VideoTableAttributes
|
private readonly tables: VideoTableAttributes
|
||||||
) {
|
) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
export class VideoTableAttributes {
|
export class VideoTableAttributes {
|
||||||
|
|
||||||
constructor (readonly mode: 'get' | 'list') {
|
constructor (private readonly mode: 'get' | 'list') {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
|
||||||
])
|
])
|
||||||
|
|
||||||
constructor (protected readonly sequelize: Sequelize) {
|
constructor (protected readonly sequelize: Sequelize) {
|
||||||
super('get')
|
super(sequelize, 'get')
|
||||||
}
|
}
|
||||||
|
|
||||||
queryVideos (options: BuildVideoGetQueryOptions) {
|
queryVideos (options: BuildVideoGetQueryOptions) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { WEBSERVER } from '@server/initializers/constants'
|
||||||
import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
|
import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
|
||||||
import { MUserAccountId, MUserId } from '@server/types/models'
|
import { MUserAccountId, MUserId } from '@server/types/models'
|
||||||
import { VideoInclude, VideoPrivacy, VideoState } from '@shared/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 = ''
|
private offset = ''
|
||||||
|
|
||||||
constructor (protected readonly sequelize: Sequelize) {
|
constructor (protected readonly sequelize: Sequelize) {
|
||||||
super()
|
super(sequelize)
|
||||||
}
|
}
|
||||||
|
|
||||||
queryVideoIds (options: BuildVideosListQueryOptions) {
|
queryVideoIds (options: BuildVideosListQueryOptions) {
|
||||||
|
|
|
@ -19,7 +19,7 @@ export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
|
||||||
private readonly videoModelBuilder: VideoModelBuilder
|
private readonly videoModelBuilder: VideoModelBuilder
|
||||||
|
|
||||||
constructor (protected readonly sequelize: Sequelize) {
|
constructor (protected readonly sequelize: Sequelize) {
|
||||||
super('list')
|
super(sequelize, 'list')
|
||||||
|
|
||||||
this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
|
this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue