Made gave backup and restore the ability to change ids for all docs

This commit is contained in:
Stefan Zermatten
2019-02-11 10:09:18 +02:00
parent 66ee3ff808
commit 0260824c2f

View File

@@ -3,6 +3,7 @@ let characterCollections = [
Attacks,
Buffs,
Classes,
Conditions,
CustomBuffs,
Effects,
Experiences,
@@ -10,21 +11,55 @@ let characterCollections = [
Notes,
Proficiencies,
SpellLists,
Spells,
TemporaryHitPoints,
Items,
Containers,
];
function backupCharacter(charId){
function dumpCharacter(charId){
let characterDump = {};
characterDump.characters = [Characters.findOne(charId)];
characterCollections.map(
c => characterDump[c._name] = c.find({charId}).fetch()
);
characterDump.character = Characters.findOne(charId);
characterCollections.forEach(c => {
characterDump.collections[c._name] = c.find({charId}).fetch();
});
return characterDump;
};
function giveCharacterDumpNewIds(characterDump){
// Give the character a new Id
const newCharId = Random.id();
characterDump.character._id = newCharId;
// Give all documents a new Id, and store the mapping from old to new
let idMap = {}; // {oldId: newId}
for (let colName in characterDump.collections){
for (let doc of characterDump.collections[colName]){
let oldId = doc._id;
let newId = Random.id();
doc._id = newId;
idMap[oldId] = newId;
}
}
// Replace all references to old Ids with new ones
for (let colName in characterDump.collections){
for (let doc of characterDump.collections[colName]){
// Replace the character Id with the new one
doc.charId = newCharId;
// Replace the parent reference id with a new id
if (doc.parent && doc.parent.id){
let newParentId = idMap[doc.parent.id];
if(!newParentId) throw `Can't find the mapping for id ${doc.parent.id}`;
doc.parent.id = newParentId;
}
}
}
}
function restoreCharacter(characterDump){
for (collectionName in characterDump){
Characters.insert(characterDump.character);
for (collectionName in characterDump.collections){
let collection = Meteor.Collection.get(collectionName);
for (doc in characterDump[collectionName]){
collection.insert(doc);