Skip to content

Commit

Permalink
fix: Repeated Prisma calls in extended Prisma clients
Browse files Browse the repository at this point in the history
  • Loading branch information
zermelo-wisen committed Apr 8, 2024
1 parent 2a8fa2c commit e684a54
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/hooks/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ function getFunctionInfo(model: string, action: string, moduleId: string) {
return functionInfos.get(key)!;
}

interface PrismaCallDetails {
model: string;
action: string;
argsValue: string;
}
let lastPrismaCallDetails: PrismaCallDetails | undefined;

function createPrismaClientMethodProxy<T extends (...args: unknown[]) => unknown>(
prismaClientMethod: T,
moduleId: string,
Expand All @@ -110,11 +117,32 @@ function createPrismaClientMethodProxy<T extends (...args: unknown[]) => unknown
if (argArray?.length > 0) {
const requestParams = argArray[0] as PrismaRequestParams;
if (requestParams.action && requestParams.model) {
prismaCall = recording.functionCall(
getFunctionInfo(requestParams.model, requestParams.action, moduleId),
requestParams.model,
[requestParams.args?.data, requestParams.args?.include, requestParams?.args?.where],
);
// In extended prisma clients, we receive repeated calls
// with the same action, module and args. It can be seen
// with the example project cal.com. Looks like it's related
// to the Prisma client extension mechanism. To prevent filling
// appmaps with repeated calls we don't record them if they are
// exactly the same with the previous one.

const funInfo = getFunctionInfo(requestParams.model, requestParams.action, moduleId);
// We use JSON.stringify to stringify details deeper in a compact string than
// event.ts:resolveParameters() will do.
const argsValue = JSON.stringify(requestParams.args);

const prismaCallDetails = {
model: requestParams.model,
action: requestParams.action,
argsValue,
};
// Don't record if it's repeated.
if (
prismaCallDetails.action != lastPrismaCallDetails?.action ||
prismaCallDetails.model != lastPrismaCallDetails?.model ||
prismaCallDetails.argsValue != lastPrismaCallDetails?.argsValue
) {
lastPrismaCallDetails = prismaCallDetails;
prismaCall = recording.functionCall(funInfo, requestParams.model, [argsValue]);
}
}
}

Expand Down

0 comments on commit e684a54

Please sign in to comment.