diff --git a/src/lib/data/attachments.ts b/src/lib/data/attachments.ts index ef8785f5..0524de9d 100644 --- a/src/lib/data/attachments.ts +++ b/src/lib/data/attachments.ts @@ -107,6 +107,7 @@ export const getAllAttachments = async ({ planEntities, governingEntities, log, + skipValidation = false, }: { database: Database; planId: PlanId; @@ -127,6 +128,13 @@ export const getAllAttachments = async ({ */ governingEntities: MapOfGoverningEntities; log: SharedLogContext; + /** + * Skip validation when fetching data from tables which have + * JSON columns, as those are expensive to verify. + * + * Also, skip type-checking attachment values against their types. + */ + skipValidation?: boolean; }): Promise => { const attachmentPrototypesById = await findAndOrganizeObjectsByUniqueProperty( database.attachmentPrototype, @@ -138,6 +146,7 @@ export const getAllAttachments = async ({ [Op.IN]: types, }, }, + skipValidation, }), 'id' ); @@ -165,6 +174,7 @@ export const getAllAttachments = async ({ ? { latestVersion: true } : { currentVersion: true }), }, + skipValidation, }), 'attachmentId' ); @@ -206,11 +216,22 @@ export const getAllAttachments = async ({ }) ); } - const data = typeCheckAttachmentData({ - attachment, - attachmentVersion, - log, - }); + + let data: AttachmentData; + if (!skipValidation) { + data = typeCheckAttachmentData({ + attachment, + attachmentVersion, + log, + }); + } else { + // Here, we sacrifice type-safety for speed, if `skipValidation` is enabled + data = { + type: attachment.type, + value: attachmentVersion.value, + } as unknown as AttachmentData; + } + result.set(attachment.id, { id: attachment.id, customRef, diff --git a/src/lib/data/governingEntities.ts b/src/lib/data/governingEntities.ts index bb3657fc..b5bd073c 100644 --- a/src/lib/data/governingEntities.ts +++ b/src/lib/data/governingEntities.ts @@ -27,6 +27,7 @@ export const getAllGoverningEntitiesForPlan = async ({ planId, version, prototypes, + skipValidation = false, }: { database: Database; planId: PlanId; @@ -41,6 +42,11 @@ export const getAllGoverningEntitiesForPlan = async ({ EntityPrototypeId, InstanceDataOfModel >; + /** + * Skip validation when fetching data from tables which have + * JSON columns, as those are expensive to verify + */ + skipValidation?: boolean; }): Promise => { const ges = await database.governingEntity.find({ where: { @@ -63,6 +69,7 @@ export const getAllGoverningEntitiesForPlan = async ({ ? { latestVersion: true } : { currentVersion: true }), }, + skipValidation, }), 'governingEntityId' ); diff --git a/src/lib/data/planEntities.ts b/src/lib/data/planEntities.ts index ecf122e5..4c92e794 100644 --- a/src/lib/data/planEntities.ts +++ b/src/lib/data/planEntities.ts @@ -44,6 +44,7 @@ export const getAndValidateAllPlanEntities = async ({ version, prototypes, allowMissingPlanEntities, + skipValidation = false, }: { database: Database; planId: PlanId; @@ -71,6 +72,11 @@ export const getAndValidateAllPlanEntities = async ({ * This can happen for example when another entity is soft-deleted. */ allowMissingPlanEntities?: boolean; + /** + * Skip validation when fetching data from tables which have + * JSON columns, as those are expensive to verify + */ + skipValidation?: boolean; }): Promise => { const planEntities = await database.planEntity.find({ where: { @@ -94,6 +100,7 @@ export const getAndValidateAllPlanEntities = async ({ ? { latestVersion: true } : { currentVersion: true }), }, + skipValidation, }), 'planEntityId' ); diff --git a/src/lib/data/projects.ts b/src/lib/data/projects.ts index 348306e6..ee957bed 100644 --- a/src/lib/data/projects.ts +++ b/src/lib/data/projects.ts @@ -258,6 +258,7 @@ export const getProjectBudgetsByOrgAndCluster = async < projects, log, ignoreInconsistentBudgets, + skipValidation = false, }: { database: Database; projects: Map; @@ -273,6 +274,11 @@ export const getProjectBudgetsByOrgAndCluster = async < * to for example calculate a plan's overall requirements. */ ignoreInconsistentBudgets?: true; + /** + * Skip validation when fetching data from tables which have + * JSON columns, as those are expensive to verify + */ + skipValidation?: boolean; }): Promise> => { const projectVersionIds = [...projects.values()].map( (p) => p.projectVersion.id @@ -298,6 +304,7 @@ export const getProjectBudgetsByOrgAndCluster = async < [Op.IN]: segments.map((s) => s.id), }, }, + skipValidation, }); const breakdownsBySegment = groupObjectsByProperty(