Compare commits

...

2 Commits

Author SHA1 Message Date
Tom Moor 9fd2f0cb17 restore withoutState scope 2024-12-26 11:25:57 +00:00
Tom Moor f1c8be7bed Exclude state column by default in document queries 2024-12-24 11:29:29 +08:00
5 changed files with 44 additions and 54 deletions
+23 -35
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) => ({
@@ -130,17 +132,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 +1141,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))
+9 -7
View File
@@ -269,13 +269,15 @@ class GroupMembership extends ParanoidModel<
transaction,
});
const document = await Document.unscoped().findOne({
attributes: ["id"],
where: {
id: model.documentId,
},
transaction,
});
const document = await Document.unscoped()
.scope("withoutState")
.findOne({
attributes: ["id"],
where: {
id: model.documentId,
},
transaction,
});
if (!document) {
return;
}
+9 -7
View File
@@ -256,13 +256,15 @@ class UserMembership extends IdModel<
transaction,
});
const document = await Document.unscoped().findOne({
attributes: ["id"],
where: {
id: model.documentId,
},
transaction,
});
const document = await Document.unscoped()
.scope("withoutState")
.findOne({
attributes: ["id"],
where: {
id: model.documentId,
},
transaction,
});
if (!document) {
return;
}
-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,
},