adjusting for stringified json objs

This commit is contained in:
Kristaps Fabians Geikins
2024-10-17 16:32:00 +03:00
parent f33dcb0849
commit 486a3857b0
6 changed files with 42 additions and 14 deletions
@@ -1,5 +1,6 @@
import { Logger } from '@/logging/logging'
import {
InsertableSpeckleObject,
RawSpeckleObject,
SpeckleObject,
SpeckleObjectClosureEntry
@@ -30,7 +31,9 @@ export type StoreObjects = (
}>
) => Promise<number[]>
export type StoreSingleObjectIfNotFound = (object: SpeckleObject) => Promise<void>
export type StoreSingleObjectIfNotFound = (
object: SpeckleObject | InsertableSpeckleObject
) => Promise<void>
export type StoreClosuresIfNotFound = (
closures: SpeckleObjectClosureEntry[]
@@ -1,10 +1,22 @@
import { ObjectChildrenClosureRecord, ObjectRecord } from '@/modules/core/helpers/types'
import { Nullable } from '@speckle/shared'
import { OverrideProperties, SetOptional } from 'type-fest'
export type SpeckleObjectClosureEntry = ObjectChildrenClosureRecord
export type SpeckleObject = ObjectRecord
/**
* We preemptively serialize a couple of fields (usually knex does it)
*/
export type InsertableSpeckleObject = OverrideProperties<
SetOptional<SpeckleObject, 'createdAt'>,
{
data: string
totalChildrenCountByDepth: string
}
>
export type RawSpeckleObject = Record<string, unknown> & {
id: string
speckle_type: string
@@ -14,6 +14,7 @@ import {
StoreObjects,
StoreSingleObjectIfNotFound
} from '@/modules/core/domain/objects/operations'
import { SpeckleObject } from '@/modules/core/domain/objects/types'
const ObjectChildrenClosure = buildTableHelper('object_children_closure', [
'parent',
@@ -74,7 +75,14 @@ export const insertObjectsFactory =
export const storeSingleObjectIfNotFoundFactory =
(deps: { db: Knex }): StoreSingleObjectIfNotFound =>
async (insertionObject) => {
await tables.objects(deps.db).insert(insertionObject).onConflict().ignore()
await tables
.objects(deps.db)
.insert(
// knex is bothered by string being inserted into jsonb, which is actually fine
insertionObject as SpeckleObject
)
.onConflict()
.ignore()
}
export const storeClosuresIfNotFoundFactory =
@@ -1,5 +1,8 @@
import crypto from 'crypto'
import { RawSpeckleObject, SpeckleObject } from '@/modules/core/domain/objects/types'
import {
InsertableSpeckleObject,
RawSpeckleObject
} from '@/modules/core/domain/objects/types'
import { getMaximumObjectSizeMB } from '@/modules/shared/helpers/envHelper'
import { estimateStringMegabyteSize } from '@/modules/core/utils/chunking'
import { ObjectHandlingError } from '@/modules/core/errors/object'
@@ -19,7 +22,7 @@ export const prepInsertionObject = (
streamId: string,
obj: RawSpeckleObject
): Omit<
SpeckleObject,
InsertableSpeckleObject,
'totalChildrenCount' | 'totalChildrenCountByDepth' | 'createdAt'
> => {
const MAX_OBJECT_SIZE_MB = getMaximumObjectSizeMB()
@@ -38,8 +41,7 @@ export const prepInsertionObject = (
}
return {
// Not sure why we stringify it, cause knex will do that, but leaving it as is for now
data: stringifiedObj as unknown as Record<string, unknown>,
data: stringifiedObj,
streamId,
id: obj.id,
// YEAH, this has been broken forever...
@@ -79,14 +81,10 @@ export const createObjectFactory =
}
}
const finalInsertionObject: SpeckleObject = {
const finalInsertionObject: InsertableSpeckleObject = {
...insertionObject,
totalChildrenCount: closures.length,
// Not sure why we stringify it, cause knex will do that, but leaving it as is for now
totalChildrenCountByDepth: JSON.stringify(
totalChildrenCountByDepth
) as unknown as Record<string, unknown>,
createdAt: new Date()
totalChildrenCountByDepth: JSON.stringify(totalChildrenCountByDepth)
}
await deps.storeSingleObjectIfNotFoundFactory(finalInsertionObject)
+1 -1
View File
@@ -189,7 +189,7 @@
"supertest": "^4.0.2",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.0.0",
"type-fest": "^2.19.0",
"type-fest": "^4.26.1",
"typescript": "^4.6.4",
"typescript-eslint": "^7.12.0",
"ws": "^8.17.1",
+8 -1
View File
@@ -16724,7 +16724,7 @@ __metadata:
supertest: "npm:^4.0.2"
ts-node: "npm:^10.9.1"
tsconfig-paths: "npm:^4.0.0"
type-fest: "npm:^2.19.0"
type-fest: "npm:^4.26.1"
typescript: "npm:^4.6.4"
typescript-eslint: "npm:^7.12.0"
ua-parser-js: "npm:^1.0.38"
@@ -50008,6 +50008,13 @@ __metadata:
languageName: node
linkType: hard
"type-fest@npm:^4.26.1":
version: 4.26.1
resolution: "type-fest@npm:4.26.1"
checksum: 10/b82676194f80af228cb852e320d2ea8381c89d667d2e4d9f2bdfc8f254bccc039c7741a90c53617a4de0c9fdca8265ed18eb0888cd628f391c5c381c33a9f94b
languageName: node
linkType: hard
"type-is@npm:^1.6.16, type-is@npm:^1.6.18, type-is@npm:~1.6.18":
version: 1.6.18
resolution: "type-is@npm:1.6.18"