perf: Don't load CRDT state from database by default (#3215)

This commit is contained in:
Tom Moor
2022-03-09 20:07:10 -08:00
committed by GitHub
parent 5efeb90fdd
commit d399e1048a
6 changed files with 27 additions and 8 deletions

View File

@@ -20,7 +20,11 @@ export default class Persistence {
return; return;
} }
const document = await Document.findByPk(documentId); const document = await Document.scope("withState").findOne({
where: {
id: documentId,
},
});
invariant(document, "Document not found"); invariant(document, "Document not found");
if (document.state) { if (document.state) {

View File

@@ -52,7 +52,7 @@ export default async function documentPermanentDeleter(documents: Document[]) {
} }
} }
return Document.scope("withUnpublished").destroy({ return Document.scope("withDrafts").destroy({
where: { where: {
id: documents.map((document) => document.id), id: documents.map((document) => document.id),
}, },

View File

@@ -15,7 +15,7 @@ export default async function documentUpdater({
ydoc: Y.Doc; ydoc: Y.Doc;
userId?: string; userId?: string;
}) { }) {
const document = await Document.findByPk(documentId); const document = await Document.scope("withState").findByPk(documentId);
invariant(document, "document not found"); invariant(document, "document not found");
const state = Y.encodeStateAsUpdate(ydoc); const state = Y.encodeStateAsUpdate(ydoc);
@@ -34,7 +34,7 @@ export default async function documentUpdater({
const existingIds = document.collaboratorIds; const existingIds = document.collaboratorIds;
const collaboratorIds = uniq([...pudIds, ...existingIds]); const collaboratorIds = uniq([...pudIds, ...existingIds]);
await Document.scope("withUnpublished").update( await Document.scope(["withDrafts", "withState"]).update(
{ {
text, text,
state: Buffer.from(state), state: Buffer.from(state),

View File

@@ -69,6 +69,9 @@ const serializer = new MarkdownSerializer();
export const DOCUMENT_VERSION = 2; export const DOCUMENT_VERSION = 2;
@DefaultScope(() => ({ @DefaultScope(() => ({
attributes: {
exclude: ["state"],
},
include: [ include: [
{ {
model: User, model: User,
@@ -112,7 +115,18 @@ export const DOCUMENT_VERSION = 2;
], ],
}; };
}, },
withUnpublished: { withoutState: {
attributes: {
exclude: ["state"],
},
},
withState: {
attributes: {
// resets to include the state column
exclude: [],
},
},
withDrafts: {
include: [ include: [
{ {
model: User, model: User,
@@ -370,7 +384,8 @@ class Document extends ParanoidModel {
// allow default preloading of collection membership if `userId` is passed in find options // allow default preloading of collection membership if `userId` is passed in find options
// almost every endpoint needs the collection membership to determine policy permissions. // almost every endpoint needs the collection membership to determine policy permissions.
const scope = this.scope([ const scope = this.scope([
"withUnpublished", "withoutState",
"withDrafts",
{ {
method: ["withCollection", options.userId, options.paranoid], method: ["withCollection", options.userId, options.paranoid],
}, },

View File

@@ -34,7 +34,7 @@ import Fix from "./decorators/Fix";
return { return {
include: [ include: [
{ {
model: Document.scope("withUnpublished"), model: Document.scope("withDrafts"),
paranoid: true, paranoid: true,
as: "document", as: "document",
include: [ include: [

View File

@@ -20,7 +20,7 @@ router.post("utils.gc", async (ctx) => {
"utils", "utils",
`Permanently destroying upto ${limit} documents older than 30 days…` `Permanently destroying upto ${limit} documents older than 30 days…`
); );
const documents = await Document.scope("withUnpublished").findAll({ const documents = await Document.scope("withDrafts").findAll({
attributes: ["id", "teamId", "text", "deletedAt"], attributes: ["id", "teamId", "text", "deletedAt"],
where: { where: {
deletedAt: { deletedAt: {