Make sync-database refresh folders and tables properly.

This commit is contained in:
zkldi
2021-12-08 20:59:44 +00:00
parent c3557e74df
commit c7966b3b92
+4 -70
View File
@@ -157,83 +157,17 @@ const syncInstructions: SyncInstructions[] = [
collection: ICollection<FolderDocument>,
logger
) => {
const bwriteOps: BulkWriteOperation<FolderDocument>[] = [];
const r = await GenericUpsert(folders, collection, "folderID", logger, true);
for (const folder of folders) {
if (!TachiConfig.GAMES.includes(folder.game)) {
continue; // Skip things for games we don't care about.
}
const exists = await collection.findOne({
folderID: folder.folderID,
});
if (!exists) {
bwriteOps.push({
// @ts-expect-error faulty monk types
insertOne: { document: folder },
});
} else if (fjsh.hash(folder, "sha256") !== fjsh.hash(exists, "sha256")) {
bwriteOps.push({
updateOne: {
filter: {
folderID: folder.folderID,
},
update: {
$set: folder,
},
},
});
}
}
if (bwriteOps.length) {
await collection.bulkWrite(bwriteOps);
if (r) {
await InitaliseFolderChartLookup();
}
},
},
{
pattern: /^tables$/u,
handler: async (
tables: TableDocument[],
collection: ICollection<TableDocument>,
logger
) => {
const bwriteOps: BulkWriteOperation<TableDocument>[] = [];
for (const table of tables) {
if (!TachiConfig.GAMES.includes(table.game)) {
continue; // Skip things for games we don't care about.
}
const exists = await collection.findOne({
tableID: table.tableID,
});
if (!exists) {
bwriteOps.push({
// @ts-expect-error faulty monk types
insertOne: { document: table },
});
} else if (fjsh.hash(table, "sha256") !== fjsh.hash(exists, "sha256")) {
bwriteOps.push({
updateOne: {
filter: {
tableID: table.tableID,
},
update: {
$set: table,
},
},
});
}
}
if (bwriteOps.length) {
await collection.bulkWrite(bwriteOps);
}
},
handler: (tables: TableDocument[], collection: ICollection<TableDocument>, logger) =>
GenericUpsert(tables, collection, "tableID", logger, true),
},
{
pattern: /^bms-course-lookup$/u,