perf: Skip the text column when loading document lists (#14138)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-18 19:52:13 -04:00
1 parent 978c05bb17
commit 4bd64340a4
4 files changed
+56 -3

No files matched your search

+37
View File
@@ -793,6 +793,43 @@ describe("#findByPk", () => {
});
});
describe("#withMembershipScope", () => {
it("should not load text for documents with content", async () => {
const user = await buildUser();
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
text: "Hello world",
});
const [loaded] = await Document.withMembershipScope(user.id).findAll({
where: { id: document.id },
});
expect(loaded.content).toBeTruthy();
expect(loaded.text).toBeNull();
});
it("should load text for documents without content or state", async () => {
const user = await buildUser();
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
text: "Hello world",
});
await sequelize.query(
`UPDATE documents SET content = NULL, state = NULL WHERE id = :id`,
{ replacements: { id: document.id } }
);
const [loaded] = await Document.withMembershipScope(user.id).findAll({
where: { id: document.id },
});
expect(loaded.text).toEqual("Hello world");
});
});
describe("findByIds", () => {
test("should return documents by ids", async () => {
const document1 = await buildDocument();
+16 -1
View File
@@ -97,6 +97,15 @@ const stateIfContentEmpty: ProjectionAlias = [
"state",
];
// The markdown text column is only needed to deserialize documents that have
// neither content nor state.
const textIfContentAndStateEmpty: ProjectionAlias = [
Sequelize.literal(
`CASE WHEN document.content IS NULL AND document.state IS NULL THEN document.text ELSE NULL END`
),
"text",
];
type AdditionalFindOptions = {
/** The user ID to load associated permissions for. */
userId?: string;
@@ -166,6 +175,12 @@ const publishedWhere: WhereOptions<Document> = {
include: [stateIfContentEmpty],
},
},
withoutStateOrText: {
attributes: {
exclude: ["state", "text"],
include: [stateIfContentEmpty, textIfContentAndStateEmpty],
},
},
withoutContent: {
attributes: {
exclude: ["state", "content", "text"],
@@ -890,7 +905,7 @@ class Document extends ArchivableModel<
) {
return this.scope([
options?.includeDrafts ? "withDrafts" : "defaultScope",
"withoutState",
"withoutStateOrText",
{
method: ["withViews", userId],
},
+2 -1
View File
@@ -79,7 +79,8 @@ let baseDomain;
@DefaultScope(() => ({
include: [
{
association: "document",
model: Document.scope(["defaultScope", "withoutStateOrText"]),
as: "document",
required: false,
},
{
+1 -1
View File
@@ -624,7 +624,7 @@ router.post(
{
model: Document.scope([
"withDrafts",
"withoutState",
"withoutStateOrText",
{ method: ["withMembership", userId] },
]),
required: true,