From a37bb1395632f0a260f1ac42ff3483f7cdf3806d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 29 May 2026 22:46:43 -0400 Subject: [PATCH] perf: Avoid redundant import lookup when presenting documents (#12529) The FileOperation import association was fetched for every non-public document but only used when sourceMetadata is present. Move the lookup inside that branch to eliminate an N+1 query for documents that are not imports, benefiting every endpoint that presents documents. Co-authored-by: Claude Opus 4.8 --- server/presenters/document.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/server/presenters/document.ts b/server/presenters/document.ts index 9ea9f1bafa..7daf3e63e9 100644 --- a/server/presenters/document.ts +++ b/server/presenters/document.ts @@ -103,8 +103,6 @@ async function presentDocument( } if (!options.isPublic) { - const source = document.import ?? (await document.$get("import")); - res.tasks = document.tasks; res.isCollectionDeleted = await document.isCollectionDeleted(); res.collectionId = document.collectionId; @@ -118,15 +116,16 @@ async function presentDocument( if (options.includeCommentCount) { res.commentCount = await document.commentCount; } - res.sourceMetadata = document.sourceMetadata - ? { - importedAt: source?.createdAt ?? document.createdAt, - importType: source?.format, - createdByName: document.sourceMetadata.createdByName, - fileName: document.sourceMetadata?.fileName, - originalDocumentId: document.sourceMetadata?.originalDocumentId, - } - : undefined; + if (document.sourceMetadata) { + const source = document.import ?? (await document.$get("import")); + res.sourceMetadata = { + importedAt: source?.createdAt ?? document.createdAt, + importType: source?.format, + createdByName: document.sourceMetadata.createdByName, + fileName: document.sourceMetadata?.fileName, + originalDocumentId: document.sourceMetadata?.originalDocumentId, + }; + } } return res;