Files
outline/server/scripts/20221029000000-crdt-to-text.ts
Apoorv Mishra 7e61a519f1 Type server models (#6326)
* fix: type server models

* fix: make ParanoidModel generic

* fix: ApiKey

* fix: Attachment

* fix: AuthenticationProvider

* fix: Backlink

* fix: Collection

* fix: Comment

* fix: Document

* fix: FileOperation

* fix: Group

* fix: GroupPermission

* fix: GroupUser

* fix: Integration

* fix: IntegrationAuthentication

* fix: Notification

* fix: Pin

* fix: Revision

* fix: SearchQuery

* fix: Share

* fix: Star

* fix: Subscription

* fix: TypeError

* fix: Imports

* fix: Team

* fix: TeamDomain

* fix: User

* fix: UserAuthentication

* fix: UserPermission

* fix: View

* fix: WebhookDelivery

* fix: WebhookSubscription

* Remove type duplication

---------

Co-authored-by: Tom Moor <tom.moor@gmail.com>
2024-01-12 22:33:05 +05:30

73 lines
1.8 KiB
TypeScript

import "./bootstrap";
import { yDocToProsemirrorJSON } from "@getoutline/y-prosemirror";
import { Node } from "prosemirror-model";
import { Op } from "sequelize";
import * as Y from "yjs";
import { schema, serializer } from "@server/editor";
import { Document } from "@server/models";
const limit = 100;
const page = 0;
const teamId = process.argv[2];
export default async function main(exit = false) {
const work = async (page: number): Promise<void> => {
console.log(`Backfill text from crdt… page ${page}`);
if (!teamId && process.env.DEPLOYMENT === "hosted") {
throw new Error("Team ID is required");
}
// Retrieve all documents within set limit.
const documents = await Document.unscoped().findAll({
attributes: ["id", "urlId", "text", "state"],
limit,
offset: page * limit,
where: {
...(teamId ? { teamId } : {}),
state: {
[Op.ne]: null,
},
},
order: [["createdAt", "ASC"]],
paranoid: false,
});
console.log(documents.length);
for (const document of documents) {
const ydoc = new Y.Doc();
// The where clause above ensures that state is non-null
Y.applyUpdate(ydoc, document.state!);
const node = Node.fromJSON(
schema,
yDocToProsemirrorJSON(ydoc, "default")
);
const text = serializer.serialize(node, undefined);
if (text !== document.text) {
console.log(`Writing text from CRDT for ${document.id}`);
document.text = text;
}
await document.save({
hooks: false,
silent: true,
});
}
return documents.length === limit ? work(page + 1) : undefined;
};
await work(page);
if (exit) {
console.log("Backfill complete");
process.exit(0);
}
}
if (process.env.NODE_ENV !== "test") {
void main(true);
}