Exclude state column by default in document queries

This commit is contained in:
Tom Moor committed 2024-12-24 11:29:29 +08:00
1 parent ce55719626
commit f1c8be7bed
4 files changed
+38 -59

No files matched your search

+12 -14
View File
@@ -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;
+23 -40
View File
@@ -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))
-2
View File
@@ -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],
@@ -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,
},