Made gave backup and restore the ability to change ids for all docs
This commit is contained in:
@@ -3,6 +3,7 @@ let characterCollections = [
|
|||||||
Attacks,
|
Attacks,
|
||||||
Buffs,
|
Buffs,
|
||||||
Classes,
|
Classes,
|
||||||
|
Conditions,
|
||||||
CustomBuffs,
|
CustomBuffs,
|
||||||
Effects,
|
Effects,
|
||||||
Experiences,
|
Experiences,
|
||||||
@@ -10,21 +11,55 @@ let characterCollections = [
|
|||||||
Notes,
|
Notes,
|
||||||
Proficiencies,
|
Proficiencies,
|
||||||
SpellLists,
|
SpellLists,
|
||||||
|
Spells,
|
||||||
|
TemporaryHitPoints,
|
||||||
Items,
|
Items,
|
||||||
Containers,
|
Containers,
|
||||||
];
|
];
|
||||||
|
|
||||||
function backupCharacter(charId){
|
function dumpCharacter(charId){
|
||||||
let characterDump = {};
|
let characterDump = {};
|
||||||
characterDump.characters = [Characters.findOne(charId)];
|
characterDump.character = Characters.findOne(charId);
|
||||||
characterCollections.map(
|
characterCollections.forEach(c => {
|
||||||
c => characterDump[c._name] = c.find({charId}).fetch()
|
characterDump.collections[c._name] = c.find({charId}).fetch();
|
||||||
);
|
});
|
||||||
return characterDump;
|
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){
|
function restoreCharacter(characterDump){
|
||||||
for (collectionName in characterDump){
|
Characters.insert(characterDump.character);
|
||||||
|
for (collectionName in characterDump.collections){
|
||||||
let collection = Meteor.Collection.get(collectionName);
|
let collection = Meteor.Collection.get(collectionName);
|
||||||
for (doc in characterDump[collectionName]){
|
for (doc in characterDump[collectionName]){
|
||||||
collection.insert(doc);
|
collection.insert(doc);
|
||||||
|
|||||||
Reference in New Issue
Block a user