chore(server): core IoC #92 - getFormattedObjectFactory

This commit is contained in:
Kristaps Fabians Geikins
2024-10-18 16:09:14 +03:00
parent 9c1892b4e8
commit 4cf520be06
7 changed files with 39 additions and 24 deletions
@@ -6,7 +6,7 @@ import {
SpeckleObjectClosureEntry
} from '@/modules/core/domain/objects/types'
import { BatchedSelectOptions } from '@/modules/shared/helpers/dbHelper'
import { Optional } from '@speckle/shared'
import { Nullable, Optional } from '@speckle/shared'
import { Knex } from 'knex'
export type GetStreamObjects = (
@@ -19,6 +19,11 @@ export type GetObject = (
streamId: string
) => Promise<Optional<SpeckleObject>>
export type GetFormattedObject = (params: {
streamId: string
objectId: string
}) => Promise<Nullable<Omit<SpeckleObject, 'streamId'>>>
export type GetBatchedStreamObjects = (
streamId: string,
options?: Partial<BatchedSelectOptions>
@@ -8,6 +8,7 @@ import {
import { Knex } from 'knex'
import {
GetBatchedStreamObjects,
GetFormattedObject,
GetObject,
GetStreamObjects,
StoreClosuresIfNotFound,
@@ -16,6 +17,7 @@ import {
StoreSingleObjectIfNotFound
} from '@/modules/core/domain/objects/operations'
import { SpeckleObject } from '@/modules/core/domain/objects/types'
import { SetOptional } from 'type-fest'
const ObjectChildrenClosure = buildTableHelper('object_children_closure', [
'parent',
@@ -53,6 +55,24 @@ export const getObjectFactory =
.first()
}
export const getFormattedObjectFactory =
(deps: { db: Knex }): GetFormattedObject =>
async ({ streamId, objectId }) => {
const res = await tables
.objects(deps.db)
.where({ streamId, id: objectId })
.select('*')
.first()
if (!res) return null
// TODO: Why tho? A lot if not most of places already just use getObjectFactory,
const finalRes: SetOptional<typeof res, 'streamId'> = res
if (finalRes.data) finalRes.data.totalChildrenCount = res.totalChildrenCount // move this back
delete finalRes.streamId // backwards compatibility
return finalRes
}
export const getBatchedStreamObjectsFactory =
(deps: { db: Knex }): GetBatchedStreamObjects =>
(streamId: string, options?: Partial<BatchedSelectOptions>) => {
@@ -4,10 +4,15 @@ const { corsMiddleware } = require('@/modules/core/configs/cors')
const { validatePermissionsReadStream } = require('./authUtils')
const { getObject, getObjectChildrenStream } = require('../services/objects')
const { getObjectChildrenStream } = require('../services/objects')
const { SpeckleObjectsStream } = require('./speckleObjectsStream')
const { pipeline, PassThrough } = require('stream')
const { logger } = require('@/logging/logging')
const { getFormattedObjectFactory } = require('@/modules/core/repositories/objects')
const { db } = require('@/db/knex')
const getObject = getFormattedObjectFactory({ db })
module.exports = (app) => {
app.options('/objects/:streamId/:objectId', corsMiddleware())
@@ -5,17 +5,6 @@ const knex = require(`@/db/knex`)
const Objects = () => knex('objects')
module.exports = {
/**
* @returns {Promise<Omit<import('@/modules/core/helpers/types').ObjectRecord, 'streamId'>>}
*/
async getObject({ streamId, objectId }) {
const res = await Objects().where({ streamId, id: objectId }).select('*').first()
if (!res) return null
res.data.totalChildrenCount = res.totalChildrenCount // move this back
delete res.streamId // backwards compatibility
return res
},
async getObjectChildrenStream({ streamId, objectId }) {
const q = knex.with(
'object_children_closure',
@@ -427,10 +416,5 @@ module.exports = {
res[dbRes[i].id] = true
}
return res
},
// NOTE: Derive Object
async updateObject() {
throw new Error('Updating object is not implemented')
}
}
@@ -8,7 +8,6 @@ const { beforeEachContext } = require('@/test/hooks')
const { getAnIdForThisOnePlease } = require('@/test/helpers')
const {
getObject,
getObjects,
getObjectChildren,
getObjectChildrenQuery,
@@ -86,7 +85,8 @@ const {
const {
storeSingleObjectIfNotFoundFactory,
storeClosuresIfNotFoundFactory,
storeObjectsIfNotFoundFactory
storeObjectsIfNotFoundFactory,
getFormattedObjectFactory
} = require('@/modules/core/repositories/objects')
const sampleCommit = JSON.parse(`{
@@ -188,6 +188,7 @@ const createObjects = createObjectsFactory({
storeObjectsIfNotFoundFactory: storeObjectsIfNotFoundFactory({ db }),
storeClosuresIfNotFound: storeClosuresIfNotFoundFactory({ db })
})
const getObject = getFormattedObjectFactory({ db })
describe('Objects @core-objects', () => {
const userOne = {
+2 -2
View File
@@ -12,7 +12,6 @@ import {
sendObjectPreviewFactory,
checkStreamPermissionsFactory
} from '@/modules/previews/services/management'
import { getObject } from '@/modules/core/services/objects'
import {
getObjectPreviewInfoFactory,
createObjectPreviewFactory,
@@ -29,6 +28,7 @@ import { SpeckleModule } from '@/modules/shared/helpers/typeHelper'
import { getStreamFactory } from '@/modules/core/repositories/streams'
import { getPaginatedBranchCommitsItemsByNameFactory } from '@/modules/core/services/commit/retrieval'
import { getStreamBranchByNameFactory } from '@/modules/core/repositories/branches'
import { getFormattedObjectFactory } from '@/modules/core/repositories/objects'
const httpErrorImage = (httpErrorCode: number) =>
require.resolve(`#/assets/previews/images/preview_${httpErrorCode}.png`)
@@ -45,7 +45,7 @@ export const init: SpeckleModule['init'] = (app, isInitial) => {
const getCommitsByStreamId = legacyGetPaginatedStreamCommitsPageFactory({ db })
const getStream = getStreamFactory({ db })
const getObjectPreviewBufferOrFilepath = getObjectPreviewBufferOrFilepathFactory({
getObject,
getObject: getFormattedObjectFactory({ db }),
getObjectPreviewInfo: getObjectPreviewInfoFactory({ db }),
createObjectPreview: createObjectPreviewFactory({ db }),
getPreviewImage: getPreviewImageFactory({ db })
@@ -1,6 +1,6 @@
import { logger } from '@/logging/logging'
import { GetFormattedObject } from '@/modules/core/domain/objects/operations'
import { GetStream } from '@/modules/core/domain/streams/operations'
import { getObject } from '@/modules/core/services/objects'
import {
CheckStreamPermissions,
CreateObjectPreview,
@@ -19,7 +19,7 @@ const defaultAngle = '0'
export const getObjectPreviewBufferOrFilepathFactory =
(deps: {
getObject: typeof getObject
getObject: GetFormattedObject
getObjectPreviewInfo: GetObjectPreviewInfo
createObjectPreview: CreateObjectPreview
getPreviewImage: GetPreviewImage