Skip to content

Commit

Permalink
Allow to skip validation when querying tables with JSON columns
Browse files Browse the repository at this point in the history
This will allow to significantly speed up
requests which fetch tons of data
  • Loading branch information
Pl217 committed Feb 19, 2024
1 parent 43e3cea commit 7ae2a54
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/lib/data/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const getAllAttachments = async ({
planEntities,
governingEntities,
log,
skipValidation = false,
}: {
database: Database;
planId: PlanId;
Expand All @@ -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<AttachmentResults> => {
const attachmentPrototypesById = await findAndOrganizeObjectsByUniqueProperty(
database.attachmentPrototype,
Expand All @@ -138,6 +146,7 @@ export const getAllAttachments = async ({
[Op.IN]: types,
},
},
skipValidation,
}),
'id'
);
Expand Down Expand Up @@ -165,6 +174,7 @@ export const getAllAttachments = async ({
? { latestVersion: true }
: { currentVersion: true }),
},
skipValidation,
}),
'attachmentId'
);
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/data/governingEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const getAllGoverningEntitiesForPlan = async ({
planId,
version,
prototypes,
skipValidation = false,
}: {
database: Database;
planId: PlanId;
Expand All @@ -41,6 +42,11 @@ export const getAllGoverningEntitiesForPlan = async ({
EntityPrototypeId,
InstanceDataOfModel<Database['entityPrototype']>
>;
/**
* Skip validation when fetching data from tables which have
* JSON columns, as those are expensive to verify
*/
skipValidation?: boolean;
}): Promise<MapOfGoverningEntities> => {
const ges = await database.governingEntity.find({
where: {
Expand All @@ -63,6 +69,7 @@ export const getAllGoverningEntitiesForPlan = async ({
? { latestVersion: true }
: { currentVersion: true }),
},
skipValidation,
}),
'governingEntityId'
);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/data/planEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const getAndValidateAllPlanEntities = async ({
version,
prototypes,
allowMissingPlanEntities,
skipValidation = false,
}: {
database: Database;
planId: PlanId;
Expand Down Expand Up @@ -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<ValidatedPlanEntities> => {
const planEntities = await database.planEntity.find({
where: {
Expand All @@ -94,6 +100,7 @@ export const getAndValidateAllPlanEntities = async ({
? { latestVersion: true }
: { currentVersion: true }),
},
skipValidation,
}),
'planEntityId'
);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/data/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export const getProjectBudgetsByOrgAndCluster = async <
projects,
log,
ignoreInconsistentBudgets,
skipValidation = false,
}: {
database: Database;
projects: Map<ProjectId, Data>;
Expand All @@ -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<Map<ProjectId, ProjectBudgetSegmentBreakdown[]>> => {
const projectVersionIds = [...projects.values()].map(
(p) => p.projectVersion.id
Expand All @@ -298,6 +304,7 @@ export const getProjectBudgetsByOrgAndCluster = async <
[Op.IN]: segments.map((s) => s.id),
},
},
skipValidation,
});

const breakdownsBySegment = groupObjectsByProperty(
Expand Down

0 comments on commit 7ae2a54

Please sign in to comment.