chore(server): fileuploads IoC 4 - getPendingUploadsBaseQueryFactory

This commit is contained in:
Kristaps Fabians Geikins
2024-09-11 15:04:40 +03:00
parent 675bec46a0
commit e1f4266b63
3 changed files with 65 additions and 53 deletions
+3 -1
View File
@@ -51,7 +51,7 @@ import {
import { CommentRecord } from '@/modules/comments/helpers/types'
import { metaHelpers } from '@/modules/core/helpers/meta'
import { Users } from '@/modules/core/dbSchema'
import { getStreamPendingModels } from '@/modules/fileuploads/repositories/fileUploads'
import { getStreamPendingModelsFactory } from '@/modules/fileuploads/repositories/fileUploads'
import { FileUploadRecord } from '@/modules/fileuploads/helpers/types'
import { getAppScopes } from '@/modules/auth/repositories'
import {
@@ -88,6 +88,8 @@ import { graphDataloadersBuilders } from '@/modules'
const simpleTupleCacheKey = (key: [string, string]) => `${key[0]}:${key[1]}`
const getStreamPendingModels = getStreamPendingModelsFactory({ db })
/**
* TODO: Lazy load DataLoaders to reduce memory usage
* - Instead of keeping them request scoped, cache them identified by request (user ID) with a TTL,
@@ -1,10 +1,10 @@
import { Roles } from '@speckle/shared'
import { Resolvers } from '@/modules/core/graph/generated/graphql'
import {
getStreamPendingModels,
getBranchPendingVersions,
getBranchPendingVersionsFactory,
getFileInfoFactory,
getStreamFileUploadsFactory
getStreamFileUploadsFactory,
getStreamPendingModelsFactory
} from '@/modules/fileuploads/repositories/fileUploads'
import { authorizeResolver } from '@/modules/shared'
import {
@@ -15,6 +15,8 @@ import { db } from '@/db/knex'
const getFileInfo = getFileInfoFactory({ db })
const getStreamFileUploads = getStreamFileUploadsFactory({ db })
const getStreamPendingModels = getStreamPendingModelsFactory({ db })
const getBranchPendingVersions = getBranchPendingVersionsFactory({ db })
export = {
Stream: {
@@ -75,60 +75,68 @@ export const saveUploadFileFactory =
return newRecord as FileUploadRecord
}
const getPendingUploadsBaseQuery = (
streamId: string,
options?: Partial<{ ignoreOld: boolean; limit: number }>
) => {
const { ignoreOld = true, limit } = options || {}
const getPendingUploadsBaseQueryFactory =
(deps: { db: Knex }) =>
(streamId: string, options?: Partial<{ ignoreOld: boolean; limit: number }>) => {
const { ignoreOld = true, limit } = options || {}
const q = FileUploads.knex<FileUploadRecord[]>()
.where(FileUploads.col.streamId, streamId)
.whereIn(FileUploads.col.convertedStatus, [
FileUploadConvertedStatus.Queued,
FileUploadConvertedStatus.Converting
])
.orderBy(FileUploads.col.uploadDate, 'desc')
const q = tables
.fileUploads(deps.db)
.where(FileUploads.col.streamId, streamId)
.whereIn(FileUploads.col.convertedStatus, [
FileUploadConvertedStatus.Queued,
FileUploadConvertedStatus.Converting
])
.orderBy(FileUploads.col.uploadDate, 'desc')
if (ignoreOld) {
q.andWhere(FileUploads.col.uploadDate, '>=', knex.raw(`now()-'1 day'::interval`))
if (ignoreOld) {
q.andWhere(FileUploads.col.uploadDate, '>=', knex.raw(`now()-'1 day'::interval`))
}
if (limit) {
q.limit(limit)
}
return q
}
if (limit) {
q.limit(limit)
}
return q
}
export async function getStreamPendingModels(
streamId: string,
options?: Partial<{ limit: number; branchNamePattern: string }>
) {
const q = getPendingUploadsBaseQuery(streamId, { limit: options?.limit }).whereNotIn(
FileUploads.col.branchName,
Branches.knex().select(Branches.col.name).where(Branches.col.streamId, streamId)
)
if (options?.branchNamePattern) {
q.whereRaw(
knex.raw(`?? ~* ?`, [FileUploads.col.branchName, options.branchNamePattern])
)
}
return await q
}
export async function getBranchPendingVersions(
streamId: string,
branchName: string,
options?: Partial<{ limit: number }>
) {
const q = getPendingUploadsBaseQuery(streamId, { limit: options?.limit })
.where(FileUploads.col.branchName, branchName)
.whereIn(
export const getStreamPendingModelsFactory =
(deps: { db: Knex }) =>
async (
streamId: string,
options?: Partial<{ limit: number; branchNamePattern: string }>
) => {
const q = getPendingUploadsBaseQueryFactory(deps)(streamId, {
limit: options?.limit
}).whereNotIn(
FileUploads.col.branchName,
Branches.knex().select(Branches.col.name).where(Branches.col.streamId, streamId)
)
return await q
}
if (options?.branchNamePattern) {
q.whereRaw(
knex.raw(`?? ~* ?`, [FileUploads.col.branchName, options.branchNamePattern])
)
}
return await q
}
export const getBranchPendingVersionsFactory =
(deps: { db: Knex }) =>
async (
streamId: string,
branchName: string,
options?: Partial<{ limit: number }>
) => {
const q = getPendingUploadsBaseQueryFactory(deps)(streamId, {
limit: options?.limit
})
.where(FileUploads.col.branchName, branchName)
.whereIn(
FileUploads.col.branchName,
Branches.knex().select(Branches.col.name).where(Branches.col.streamId, streamId)
)
return await q
}