feat: Document subscriptions (#3834)

Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
CuriousCorrelation
2022-08-26 12:17:13 +05:30
committed by GitHub
parent 864f585e5b
commit 24c71c38a5
36 changed files with 2594 additions and 165 deletions

View File

@@ -0,0 +1,51 @@
import "./bootstrap";
import { Subscription, Document } from "@server/models";
const limit = 100;
let page = parseInt(process.argv[2], 10);
page = Number.isNaN(page) ? 0 : page;
export default async function main(exit = false) {
const work = async (page: number) => {
console.log(`Backfill subscription… page ${page}`);
// Retrieve all documents within set limit.
const documents = await Document.findAll({
attributes: ["collaboratorIds", "id"],
limit,
offset: page * limit,
order: [["createdAt", "ASC"]],
});
for (const document of documents) {
try {
await Promise.all(
document.collaboratorIds.map((collaboratorId) =>
Subscription.findOrCreate({
where: {
userId: collaboratorId,
documentId: document.id,
event: "documents.update",
},
})
)
);
} catch (err) {
console.error(`Failed at ${document.id}:`, err);
continue;
}
}
};
await work(page);
if (exit) {
console.log("Backfill complete");
process.exit(0);
}
}
if (process.env.NODE_ENV !== "test") {
main(true);
}