feat: Document subscriptions (#3834)
Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
committed by
GitHub
parent
864f585e5b
commit
24c71c38a5
51
server/scripts/20220722000000-backfill-subscriptions.ts
Normal file
51
server/scripts/20220722000000-backfill-subscriptions.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user