Skip to content

Commit

Permalink
Spans
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Dec 28, 2024
1 parent befeb20 commit 0d3870a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
58 changes: 33 additions & 25 deletions apps/backend/src/prisma-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,40 @@ export async function rawQueryAll<Q extends Record<string, undefined | RawQuery<
}

async function rawQueryArray<Q extends RawQuery<any>[]>(queries: Q): Promise<[] & { [K in keyof Q]: Awaited<ReturnType<Q[K]["postProcess"]>> }> {
if (queries.length === 0) return [] as any;
return await traceSpan({
description: "raw SQL query",
attributes: {
"stack.raw-queries.length": queries.length,
...Object.fromEntries(queries.map((q, index) => [`stack.raw-queries.${index}`, q.sql.text])),
},
}, async () => {
if (queries.length === 0) return [] as any;

const query = Prisma.sql`
WITH ${Prisma.join(queries.map((q, index) => {
return Prisma.sql`${Prisma.raw("q" + index)} AS (
${q.sql}
)`;
}), ",\n")}
const query = Prisma.sql`
WITH ${Prisma.join(queries.map((q, index) => {
return Prisma.sql`${Prisma.raw("q" + index)} AS (
${q.sql}
)`;
}), ",\n")}
${Prisma.join(queries.map((q, index) => {
return Prisma.sql`
SELECT
${"q" + index} AS type,
row_to_json(c) AS json
FROM (SELECT * FROM ${Prisma.raw("q" + index)}) c
`;
}), "\nUNION ALL\n")}
`;
const rawResult = await prismaClient.$queryRaw(query) as { type: string, json: any }[];
const unprocessed = new Array(queries.length).fill(null).map(() => [] as any[]);
for (const row of rawResult) {
const type = row.type;
const index = +type.slice(1);
unprocessed[index].push(row.json);
}
const postProcessed = queries.map((q, index) => q.postProcess(unprocessed[index]));
return postProcessed as any;
${Prisma.join(queries.map((q, index) => {
return Prisma.sql`
SELECT
${"q" + index} AS type,
row_to_json(c) AS json
FROM (SELECT * FROM ${Prisma.raw("q" + index)}) c
`;
}), "\nUNION ALL\n")}
`;
const rawResult = await prismaClient.$queryRaw(query) as { type: string, json: any }[];
const unprocessed = new Array(queries.length).fill(null).map(() => [] as any[]);
for (const row of rawResult) {
const type = row.type;
const index = +type.slice(1);
unprocessed[index].push(row.json);
}
const postProcessed = queries.map((q, index) => q.postProcess(unprocessed[index]));
return postProcessed as any;
});
}

2 changes: 1 addition & 1 deletion apps/backend/src/route-handlers/smart-request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const parseAuth = withTraceSpan('smart request parseAuth', async (req: NextReque
if (!result) throw new StatusError(401, "Invalid development key override");
} else if (adminAccessToken) {
// TODO put this into the bundled queries above (not so important because this path is quite rare)
const internalUser = await extractUserFromAdminAccessToken({ token: adminAccessToken, projectId });
await extractUserFromAdminAccessToken({ token: adminAccessToken, projectId });
if (!await queries.project) {
// this happens if the project is still in the user's managedProjectIds, but has since been deleted
throw new KnownErrors.InvalidProjectForAdminAccessToken();
Expand Down

0 comments on commit 0d3870a

Please sign in to comment.