mirror of
https://gitea.tendokyu.moe/ppc/amnet.git
synced 2026-09-25 07:38:17 +03:00
43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import {Realm} from "realm";
|
|
import {createRealmContext} from "@realm/react";
|
|
|
|
import {AMCard} from "./AMCard";
|
|
import {AMServer} from "./AMServer";
|
|
import {AMCardRecentUse} from "./AMCardRecentUse";
|
|
import {AMPhysicalCardData} from "./AMPhysicalCardData";
|
|
|
|
const migrationFunction = (oldRealm: Realm, newRealm: Realm) => {
|
|
if (oldRealm.schemaVersion < 5) {
|
|
const oldCards = oldRealm.objects("card");
|
|
const gameIdMap: Map<string, { card: any, lastUsed: Date }> = new Map();
|
|
|
|
// get last used card for each game
|
|
oldCards.forEach((card: any) => {
|
|
if (card.lastUsedGameId && card.lastUsed) {
|
|
const existing = gameIdMap.get(card.lastUsedGameId);
|
|
if (!existing || card.lastUsed > existing.lastUsed) {
|
|
gameIdMap.set(card.lastUsedGameId, {card, lastUsed: card.lastUsed});
|
|
}
|
|
}
|
|
});
|
|
|
|
// write new history records
|
|
const newCards = newRealm.objects("card");
|
|
gameIdMap.forEach((value, gameId) => {
|
|
const newCard = newCards.filtered(`_id == $0`, value.card._id)[0];
|
|
newRealm.create("cardUsageHistory", {
|
|
gameId: gameId,
|
|
card: newCard
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|
|
// @ts-ignore
|
|
export default createRealmContext({
|
|
schemaVersion: 5,
|
|
schema: [AMPhysicalCardData, AMCardRecentUse, AMCard, AMServer],
|
|
onMigration: migrationFunction,
|
|
deleteRealmIfMigrationNeeded: false,
|
|
});
|