From 486a3857b09e63c7db7b43749c112e1b60f3ba16 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Thu, 17 Oct 2024 16:32:00 +0300 Subject: [PATCH] adjusting for stringified json objs --- .../modules/core/domain/objects/operations.ts | 5 ++++- .../modules/core/domain/objects/types.ts | 12 ++++++++++++ .../modules/core/repositories/objects.ts | 10 +++++++++- .../core/services/objects/management.ts | 18 ++++++++---------- packages/server/package.json | 2 +- yarn.lock | 9 ++++++++- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/packages/server/modules/core/domain/objects/operations.ts b/packages/server/modules/core/domain/objects/operations.ts index dc9ca8a21..f45eb5f69 100644 --- a/packages/server/modules/core/domain/objects/operations.ts +++ b/packages/server/modules/core/domain/objects/operations.ts @@ -1,5 +1,6 @@ import { Logger } from '@/logging/logging' import { + InsertableSpeckleObject, RawSpeckleObject, SpeckleObject, SpeckleObjectClosureEntry @@ -30,7 +31,9 @@ export type StoreObjects = ( }> ) => Promise -export type StoreSingleObjectIfNotFound = (object: SpeckleObject) => Promise +export type StoreSingleObjectIfNotFound = ( + object: SpeckleObject | InsertableSpeckleObject +) => Promise export type StoreClosuresIfNotFound = ( closures: SpeckleObjectClosureEntry[] diff --git a/packages/server/modules/core/domain/objects/types.ts b/packages/server/modules/core/domain/objects/types.ts index e22816982..6fde54b41 100644 --- a/packages/server/modules/core/domain/objects/types.ts +++ b/packages/server/modules/core/domain/objects/types.ts @@ -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, + { + data: string + totalChildrenCountByDepth: string + } +> + export type RawSpeckleObject = Record & { id: string speckle_type: string diff --git a/packages/server/modules/core/repositories/objects.ts b/packages/server/modules/core/repositories/objects.ts index d93498404..76f1a8408 100644 --- a/packages/server/modules/core/repositories/objects.ts +++ b/packages/server/modules/core/repositories/objects.ts @@ -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 = diff --git a/packages/server/modules/core/services/objects/management.ts b/packages/server/modules/core/services/objects/management.ts index 3dc689e4b..6f5428553 100644 --- a/packages/server/modules/core/services/objects/management.ts +++ b/packages/server/modules/core/services/objects/management.ts @@ -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, + 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, - createdAt: new Date() + totalChildrenCountByDepth: JSON.stringify(totalChildrenCountByDepth) } await deps.storeSingleObjectIfNotFoundFactory(finalInsertionObject) diff --git a/packages/server/package.json b/packages/server/package.json index f77c5fd25..dd3dfb233 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index dc56a1500..bda6d6f4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"