feat: add goal sync support

This commit is contained in:
zkldi
2022-04-17 04:26:27 +01:00
parent b3f8071e78
commit 120c9cbb52
2 changed files with 30 additions and 3 deletions
+22 -2
View File
@@ -14,12 +14,16 @@ import {
ChartDocument,
FolderDocument,
Game,
GoalDocument,
MilestoneDocument,
MilestoneSetDocument,
SongDocument,
TableDocument,
} from "tachi-common";
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import { UpdateGameSongIDCounter } from "utils/db";
import { InitaliseFolderChartLookup } from "utils/folder";
import { IsSupported } from "utils/misc";
interface SyncInstructions {
pattern: RegExp;
@@ -60,7 +64,8 @@ async function GenericUpsert<T>(
collection: ICollection<T>,
field: keyof T,
logger: KtLogger,
remove = false
remove = false,
update = true
) {
logger.verbose(`Running bulkwrite.`);
@@ -89,7 +94,7 @@ async function GenericUpsert<T>(
// @ts-expect-error Actually, T is assignable to OptionalId<T>.
insertOne: { document },
});
} else if (fjsh.hash(document, "sha256") !== fjsh.hash(exists, "sha256")) {
} else if (update && fjsh.hash(document, "sha256") !== fjsh.hash(exists, "sha256")) {
bwriteOps.push({
replaceOne: {
// @ts-expect-error Known X->Y generic issue.
@@ -243,6 +248,21 @@ const syncInstructions: SyncInstructions[] = [
await GenericUpsert(bmsCourseDocuments, collection, "md5sums", logger);
},
},
{
pattern: /^goals/u,
handler: async (goals: GoalDocument[], collection: ICollection<GoalDocument>, logger) => {
// never remove goals. Never update goals either. Only insert new ones as
// they come in.
await GenericUpsert(
goals.filter((e) => IsSupported(e.game)),
collection,
"goalID",
logger,
false,
false
);
},
},
];
const logger = CreateLogCtx("Database Sync");