From b77f5b9fb2633f2cb4e51c90f32100c69147687f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 7 May 2025 08:52:59 -0400 Subject: [PATCH] fix: withoutState scope should include state as fallback --- server/models/Document.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/models/Document.ts b/server/models/Document.ts index b13ff201b3..0b64919ebd 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -15,6 +15,7 @@ import { FindOptions, WhereOptions, EmptyResultError, + Sequelize, } from "sequelize"; import { ForeignKey, @@ -104,10 +105,18 @@ type AdditionalFindOptions = { exclude: ["state"], }, })) +// @ts-expect-error Type 'Literal' is not assignable to type 'string | ProjectionAlias'. @Scopes(() => ({ withoutState: { attributes: { - exclude: ["state"], + include: [ + Sequelize.literal( + // If content (JSON) is null then we still need to return the state column (BINARY) + // as it's used as a fallback for content deserialization for older documents. + // This can be removed if content is 100% backfilled. + `CASE WHEN document.content IS NULL THEN document.state ELSE NULL END AS state` + ), + ], }, }, withCollection: {