From f1c8be7bed58e8205543e70a006a145bda2e0676 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 24 Dec 2024 11:29:29 +0800 Subject: [PATCH] Exclude state column by default in document queries --- .../commands/documentCollaborativeUpdater.ts | 26 ++++---- server/models/Document.ts | 63 +++++++------------ server/models/helpers/SearchHelper.ts | 2 - .../queues/processors/WebsocketsProcessor.ts | 6 +- 4 files changed, 38 insertions(+), 59 deletions(-) diff --git a/server/commands/documentCollaborativeUpdater.ts b/server/commands/documentCollaborativeUpdater.ts index f10d9f2537..9f1aff1fa3 100644 --- a/server/commands/documentCollaborativeUpdater.ts +++ b/server/commands/documentCollaborativeUpdater.ts @@ -28,20 +28,18 @@ export default async function documentCollaborativeUpdater({ isLastConnection, }: Props) { return sequelize.transaction(async (transaction) => { - const document = await Document.unscoped() - .scope("withoutState") - .findOne({ - where: { - id: documentId, - }, - transaction, - lock: { - of: Document, - level: transaction.LOCK.UPDATE, - }, - rejectOnEmpty: true, - paranoid: false, - }); + const document = await Document.unscoped().findOne({ + where: { + id: documentId, + }, + transaction, + lock: { + of: Document, + level: transaction.LOCK.UPDATE, + }, + rejectOnEmpty: true, + paranoid: false, + }); const state = Y.encodeStateAsUpdate(ydoc); const content = yDocToProsemirrorJSON(ydoc, "default") as ProsemirrorData; diff --git a/server/models/Document.ts b/server/models/Document.ts index ef6df7c71a..53f2edae9b 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -10,7 +10,6 @@ import type { SaveOptions, } from "sequelize"; import { - Sequelize, Transaction, Op, FindOptions, @@ -101,6 +100,9 @@ type AdditionalFindOptions = { }, }, }, + attributes: { + exclude: ["state"], + }, })) @Scopes(() => ({ withCollectionPermissions: (userId: string, paranoid = true) => ({ @@ -117,11 +119,6 @@ type AdditionalFindOptions = { }, ], }), - withoutState: { - attributes: { - exclude: ["state"], - }, - }, withCollection: { include: [ { @@ -130,17 +127,6 @@ type AdditionalFindOptions = { }, ], }, - withStateIsEmpty: { - attributes: { - exclude: ["state"], - include: [ - [ - Sequelize.literal(`CASE WHEN state IS NULL THEN true ELSE false END`), - "stateIsEmpty", - ], - ], - }, - }, withState: { attributes: { // resets to include the state column @@ -1150,30 +1136,27 @@ class Document extends ArchivableModel< // Checking if the record is new is a performance optimization – new docs cannot have children const childDocuments = this.isNewRecord ? [] - : await (this.constructor as typeof Document) - .unscoped() - .scope("withoutState") - .findAll({ - where: options?.includeArchived - ? { - teamId: this.teamId, - parentDocumentId: this.id, - publishedAt: { - [Op.ne]: null, - }, - } - : { - teamId: this.teamId, - parentDocumentId: this.id, - publishedAt: { - [Op.ne]: null, - }, - archivedAt: { - [Op.is]: null, - }, + : await (this.constructor as typeof Document).unscoped().findAll({ + where: options?.includeArchived + ? { + teamId: this.teamId, + parentDocumentId: this.id, + publishedAt: { + [Op.ne]: null, }, - transaction: options?.transaction, - }); + } + : { + teamId: this.teamId, + parentDocumentId: this.id, + publishedAt: { + [Op.ne]: null, + }, + archivedAt: { + [Op.is]: null, + }, + }, + transaction: options?.transaction, + }); const children = await Promise.all( childDocuments.map((child) => child.toNavigationNode(options)) diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index 2c3474d9f7..c63e966f6c 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -183,7 +183,6 @@ export default class SearchHelper { ]; return Document.scope([ - "withoutState", "withDrafts", { method: ["withViews", user.id], @@ -246,7 +245,6 @@ export default class SearchHelper { // Final query to get associated document data const [documents, count] = await Promise.all([ Document.scope([ - "withState", "withDrafts", { method: ["withViews", user.id], diff --git a/server/queues/processors/WebsocketsProcessor.ts b/server/queues/processors/WebsocketsProcessor.ts index e426890549..a3d261674b 100644 --- a/server/queues/processors/WebsocketsProcessor.ts +++ b/server/queues/processors/WebsocketsProcessor.ts @@ -464,7 +464,7 @@ export default class WebsocketsProcessor { const comment = await Comment.findByPk(event.modelId, { include: [ { - model: Document.scope(["withoutState", "withDrafts"]), + model: Document.scope("withDrafts"), as: "document", required: true, }, @@ -486,7 +486,7 @@ export default class WebsocketsProcessor { paranoid: false, include: [ { - model: Document.scope(["withoutState", "withDrafts"]), + model: Document.scope("withDrafts"), as: "document", required: true, }, @@ -510,7 +510,7 @@ export default class WebsocketsProcessor { const comment = await Comment.findByPk(event.modelId, { include: [ { - model: Document.scope(["withoutState", "withDrafts"]), + model: Document.scope("withDrafts"), as: "document", required: true, },