Migrated internal variables to ~ prefix

This commit is contained in:
Stefan Zermatten
2023-01-31 20:36:26 +02:00
parent 16f5fe91ea
commit 67da641244
13 changed files with 82 additions and 65 deletions

View File

@@ -15,18 +15,21 @@ export default function writeScope(creatureId, computation) {
let $set, $unset;
for (const key in scope){
for (const key in scope) {
// Mongo can't handle keys that start with a dollar sign
if (key[0] === '$' || key[0] === '_') continue;
// Remove large properties that aren't likely to be accessed
delete scope[key].parent;
delete scope[key].ancestors;
// Remove empty keys
for (const subKey in scope[key]) {
if (scope[key][subKey] === undefined) {
delete scope[key][subKey]
}
}
// Only update changed fields
if (!EJSON.equals(variables[key], scope[key])) {
if (!$set) $set = {};
@@ -53,9 +56,9 @@ export default function writeScope(creatureId, computation) {
const update = {};
if ($set) update.$set = $set;
if ($unset) update.$unset = $unset;
CreatureVariables.update({_creatureId: creatureId}, update);
CreatureVariables.update({ _creatureId: creatureId }, update);
}
if (computation.creature?.dirty) {
Creatures.update({_id: creatureId}, {$unset: { dirty: 1 }});
Creatures.update({ _id: creatureId }, { $unset: { dirty: 1 } });
}
}