Merge pull request #3356 from specklesystems/fabians/core-ioc-98

chore(server): core IoC #98 - hasObjectsFactory
This commit is contained in:
Alessandro Magionami
2024-10-22 10:04:25 +02:00
committed by GitHub
4 changed files with 32 additions and 23 deletions
@@ -85,6 +85,11 @@ export type GetObjectChildren = (params: {
cursor: string | null
}>
export type HasObjects = (params: {
streamId: string
objectIds: string[]
}) => Promise<{ [objectId: string]: boolean }>
export type GetObjectChildrenQuery = (params: {
streamId: string
objectId: string
@@ -15,6 +15,7 @@ import {
GetObjectChildrenStream,
GetObjectsStream,
GetStreamObjects,
HasObjects,
StoreClosuresIfNotFound,
StoreObjects,
StoreObjectsIfNotFound,
@@ -184,6 +185,27 @@ export const getObjectsStreamFactory =
return res.stream({ highWaterMark: 500 })
}
export const hasObjectsFactory =
(deps: { db: Knex }): HasObjects =>
async ({ streamId, objectIds }) => {
const dbRes = await tables
.objects(deps.db)
.whereIn('id', objectIds)
.andWhere('streamId', streamId)
.select('id')
const res: Record<string, boolean> = {}
// eslint-disable-next-line @typescript-eslint/no-for-in-array
for (const i in objectIds) {
res[objectIds[i]] = false
}
// eslint-disable-next-line @typescript-eslint/no-for-in-array
for (const i in dbRes) {
res[dbRes[i].id] = true
}
return res
}
export const getObjectChildrenFactory =
(deps: { db: Knex }): GetObjectChildren =>
async ({ streamId, objectId, limit, depth, select, cursor }) => {
@@ -1,11 +1,14 @@
import zlib from 'zlib'
import { corsMiddleware } from '@/modules/core/configs/cors'
import { validatePermissionsWriteStream } from '@/modules/core/rest/authUtils'
import { hasObjects } from '@/modules/core/services/objects'
import { chunk } from 'lodash'
import type { Application } from 'express'
import { hasObjectsFactory } from '@/modules/core/repositories/objects'
import { db } from '@/db/knex'
export default (app: Application) => {
const hasObjects = hasObjectsFactory({ db })
app.options('/api/diff/:streamId', corsMiddleware())
app.post('/api/diff/:streamId', corsMiddleware(), async (req, res) => {
@@ -31,7 +34,7 @@ export default (app: Application) => {
objectListChunks.map((objectListChunk) =>
hasObjects({
streamId: req.params.streamId,
objectIds: objectListChunk
objectIds: objectListChunk as string[]
})
)
)
@@ -1,21 +0,0 @@
const knex = require(`@/db/knex`)
const Objects = () => knex('objects')
module.exports = {
async hasObjects({ streamId, objectIds }) {
const dbRes = await Objects()
.whereIn('id', objectIds)
.andWhere('streamId', streamId)
.select('id')
const res = {}
for (const i in objectIds) {
res[objectIds[i]] = false
}
for (const i in dbRes) {
res[dbRes[i].id] = true
}
return res
}
}