Started work on migrating model to lazy evaluation

This commit is contained in:
Stefan Zermatten
2018-10-12 09:18:18 +02:00
parent ca8223ccad
commit 189a1d0a16
5 changed files with 75 additions and 44 deletions

View File

@@ -1,7 +1,9 @@
Instances = new Mongo.Collection("instances"); let Instances = new Mongo.Collection("instances");
Schemas.Instance = new SimpleSchema({ let instanceSchema = new SimpleSchema({
//an instance is a single flow of time all parties in an instance are in-sync time wise //an instance is a single flow of time all parties in an instance are in-sync time wise
}); });
Instances.attachSchema(Schemas.Instance); Instances.attachSchema(instanceSchema);
export default Instances;

View File

@@ -1,6 +1,6 @@
Parties = new Mongo.Collection("parties"); let Parties = new Mongo.Collection("parties");
Schemas.Party = new SimpleSchema({ let partySchema = new SimpleSchema({
name: { name: {
type: String, type: String,
defaultValue: "New Party", defaultValue: "New Party",
@@ -40,3 +40,5 @@ Parties.deny({
return _.contains(fields, "owner"); return _.contains(fields, "owner");
} }
}); });
export default Parties;

View File

@@ -1,9 +1,9 @@
Actions = new Mongo.Collection("actions"); let Actions = new Mongo.Collection("actions");
/* /*
* Actions are given to a character by items and features * Actions are given to a character by items and features
*/ */
Schemas.Action = new SimpleSchema({ let actionSchema = new SimpleSchema({
charId: { charId: {
type: String, type: String,
regEx: SimpleSchema.RegEx.Id, regEx: SimpleSchema.RegEx.Id,
@@ -31,10 +31,12 @@ Schemas.Action = new SimpleSchema({
}, },
}); });
Actions.attachSchema(Schemas.Action); Actions.attachSchema(actionSchema);
Actions.attachBehaviour("softRemovable"); Actions.attachBehaviour("softRemovable");
makeChild(Actions); makeChild(Actions);
Actions.allow(CHARACTER_SUBSCHEMA_ALLOW); Actions.allow(CHARACTER_SUBSCHEMA_ALLOW);
Actions.deny(CHARACTER_SUBSCHEMA_DENY); Actions.deny(CHARACTER_SUBSCHEMA_DENY);
export default Actions

View File

@@ -1,9 +1,9 @@
Attacks = new Mongo.Collection("attacks"); let Attacks = new Mongo.Collection("attacks");
/* /*
* Attacks are given to a character by items and features * Attacks are given to a character by items and features
*/ */
Schemas.Attack = new SimpleSchema({ attackSchema = new SimpleSchema({
charId: { charId: {
type: String, type: String,
regEx: SimpleSchema.RegEx.Id, regEx: SimpleSchema.RegEx.Id,
@@ -66,7 +66,7 @@ Schemas.Attack = new SimpleSchema({
}, },
}); });
Attacks.attachSchema(Schemas.Attack); Attacks.attachSchema(attackSchema);
Attacks.attachBehaviour("softRemovable"); Attacks.attachBehaviour("softRemovable");
makeChild(Attacks, ["name", "enabled"]); //children of lots of things makeChild(Attacks, ["name", "enabled"]); //children of lots of things
@@ -85,3 +85,5 @@ Attacks.after.insert(function (userId, attack) {
Attacks.allow(CHARACTER_SUBSCHEMA_ALLOW); Attacks.allow(CHARACTER_SUBSCHEMA_ALLOW);
Attacks.deny(CHARACTER_SUBSCHEMA_DENY); Attacks.deny(CHARACTER_SUBSCHEMA_DENY);
export default Attacks;

View File

@@ -1,4 +1,6 @@
var childSchema = new SimpleSchema({ import { ValidatedMethod } from 'meteor/mdg:validated-method';
let childSchema = new SimpleSchema({
parent: {type: Object}, parent: {type: Object},
"parent.collection": {type: String}, "parent.collection": {type: String},
"parent.id": {type: String, regEx: SimpleSchema.RegEx.Id, index: 1}, "parent.id": {type: String, regEx: SimpleSchema.RegEx.Id, index: 1},
@@ -10,14 +12,14 @@ var childSchema = new SimpleSchema({
}, },
}); });
var joinWithDefaultKeys = function(keys){ let joinWithDefaultKeys = function(keys){
var defaultKeys = [ let defaultKeys = [
"charId", "charId",
]; ];
return _.union(keys, defaultKeys); return _.union(keys, defaultKeys);
}; };
var limitModifierToKeys = function(modifier, keys){ let limitModifierToKeys = function(modifier, keys){
if (!modifier) return; if (!modifier) return;
modifier = _.pick(modifier, ["$set", "$unset"]); modifier = _.pick(modifier, ["$set", "$unset"]);
if (modifier.$set) modifier.$set = _.pick(modifier.$set, keys); if (modifier.$set) modifier.$set = _.pick(modifier.$set, keys);
@@ -27,21 +29,21 @@ var limitModifierToKeys = function(modifier, keys){
return modifier; return modifier;
}; };
var getParent = function(doc){ let getParent = function(doc){
if (!doc || !doc.parent) return; if (!doc || !doc.parent) return;
var parentCol = Meteor.isClient ? let parentCol = Meteor.isClient ?
window[doc.parent.collection] : global[doc.parent.collection]; window[doc.parent.collection] : global[doc.parent.collection];
if (parentCol) if (parentCol)
return parentCol.findOne(doc.parent.id, {removed: true}); return parentCol.findOne(doc.parent.id, {removed: true});
}; };
var inheritParentProperties = function(doc, collection){ let inheritParentProperties = function(doc, collection){
var parent = getParent(doc); let parent = getParent(doc);
if (!parent) throw new Meteor.Error( if (!parent) throw new Meteor.Error(
"Parenting Error", "Parenting Error",
"Document's parent does not exist" "Document's parent does not exist"
); );
var handMeDowns = _.pick(parent, collection.inheritedKeys); let handMeDowns = _.pick(parent, collection.inheritedKeys);
if ( if (
_.contains(collection.inheritedKeys, "charId") && _.contains(collection.inheritedKeys, "charId") &&
doc.parent.collection === "Characters" doc.parent.collection === "Characters"
@@ -52,9 +54,9 @@ var inheritParentProperties = function(doc, collection){
collection.update(doc._id, {$set: handMeDowns}); collection.update(doc._id, {$set: handMeDowns});
}; };
var childCollections = []; let childCollections = [];
makeChild = function(collection, inheritedKeys){ let makeChild = function(collection, inheritedKeys){
inheritedKeys = inheritedKeys || []; inheritedKeys = inheritedKeys || [];
if (inheritedKeys) { if (inheritedKeys) {
collection.inheritedKeys = joinWithDefaultKeys(inheritedKeys); collection.inheritedKeys = joinWithDefaultKeys(inheritedKeys);
@@ -102,9 +104,9 @@ makeChild = function(collection, inheritedKeys){
childCollections.push(collection); childCollections.push(collection);
}; };
makeParent = function(collection, donatedKeys){ let makeParent = function(collection, donatedKeys){
donatedKeys = joinWithDefaultKeys(donatedKeys); donatedKeys = joinWithDefaultKeys(donatedKeys);
var collectionName = collection._collection.name; let collectionName = collection._collection.name;
//after changing, push the changes to all children //after changing, push the changes to all children
collection.after.update(function(userId, doc, fieldNames, modifier, options) { collection.after.update(function(userId, doc, fieldNames, modifier, options) {
modifier = limitModifierToKeys(modifier, donatedKeys); modifier = limitModifierToKeys(modifier, donatedKeys);
@@ -129,8 +131,8 @@ makeParent = function(collection, donatedKeys){
}); });
}; };
var checkPermission = function(userId, charId){ let checkPermission = function(userId, charId){
var char = Characters.findOne(charId, {fields: {owner: 1, writers: 1}}); let char = Characters.findOne(charId, {fields: {owner: 1, writers: 1}});
if (!char) if (!char)
throw new Meteor.Error("Access Denied, no charId", throw new Meteor.Error("Access Denied, no charId",
"Character " + charId + " does not exist"); "Character " + charId + " does not exist");
@@ -143,7 +145,7 @@ var checkPermission = function(userId, charId){
return true; return true;
}; };
var cascadeSoftRemove = function(id, removedWithId){ let cascadeSoftRemove = function(id, removedWithId){
_.each(childCollections, function(treeCollection){ _.each(childCollections, function(treeCollection){
treeCollection.update( treeCollection.update(
{"parent.id": id}, {"parent.id": id},
@@ -159,25 +161,38 @@ var cascadeSoftRemove = function(id, removedWithId){
}); });
}; };
var checkRemovePermission = function(collectionName, id, self){ let checkRemovePermission = function(collectionName, id, self){
check(collectionName, String); check(collectionName, String);
check(id, String); check(id, String);
var collection = Mongo.Collection.get(collectionName); let collection = Mongo.Collection.get(collectionName);
var node = collection.findOne(id); let node = collection.findOne(id);
var charId = node && node.charId; let charId = node && node.charId;
checkPermission(self.userId, charId); checkPermission(self.userId, charId);
}; };
Meteor.methods({ const softRemoveNode = new ValidatedMethod({
softRemoveNode: function(collectionName, id){ name: "parenting.methods.softRemoveNode",
validate: new SimpleSchema({
collectionName: {type: String,},
id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
},
}).validator(), // argument validation
run({collectionName, id}){
checkRemovePermission(collectionName, id, this); checkRemovePermission(collectionName, id, this);
var collection = Mongo.Collection.get(collectionName); let collection = Mongo.Collection.get(collectionName);
collection.softRemove(id); collection.softRemove(id);
cascadeSoftRemove(id, id); cascadeSoftRemove(id, id);
}, },
restoreNode: function(collectionName, id){ });
const restoreNode = new ValidatedMethod({
run(collectionName, id){
checkRemovePermission(collectionName, id, this); checkRemovePermission(collectionName, id, this);
var collection = Mongo.Collection.get(collectionName); let collection = Mongo.Collection.get(collectionName);
collection.restore(id); collection.restore(id);
_.each(childCollections, function(treeCollection){ _.each(childCollections, function(treeCollection){
treeCollection.update( treeCollection.update(
@@ -187,13 +202,16 @@ Meteor.methods({
); );
}); });
}, },
updateChildren: function(parent, modifier, limitToInheritance) { });
const updateChildren = new ValidatedMethod({
run({parent, modifier, limitToInheritance}){
check(parent, {_id: String, charId: String}); check(parent, {_id: String, charId: String});
check(modifier, Object); check(modifier, Object);
checkPermission(this.userId, parent.charId); checkPermission(this.userId, parent.charId);
var selector = {"parent.id": parent._id}; let selector = {"parent.id": parent._id};
_.each(childCollections, function(collection){ _.each(childCollections, function(collection){
var thisModifier; let thisModifier;
if (limitToInheritance){ if (limitToInheritance){
thisModifier = limitModifierToKeys(modifier, collection.inheritedKeys); thisModifier = limitModifierToKeys(modifier, collection.inheritedKeys);
} else { } else {
@@ -203,17 +221,22 @@ Meteor.methods({
collection.update(selector, thisModifier, {multi: true, removed: true}); collection.update(selector, thisModifier, {multi: true, removed: true});
}); });
}, },
cloneChildren: function(objectId, newParent){ });
const cloneChildren = new ValidatedMethod({
run({objectId, newParent}){
check(objectId, String); check(objectId, String);
check(newParent, {id: String, collection: String}); check(newParent, {id: String, collection: String});
_.each(childCollections, function(collection){ _.each(childCollections, function(collection){
var keys = collection.simpleSchema().objectKeys(); let keys = collection.simpleSchema().objectKeys();
collection.find({"parent.id": objectId}).forEach(function(doc){ collection.find({"parent.id": objectId}).forEach(function(doc){
var newDoc = _.pick(doc, keys); let newDoc = _.pick(doc, keys);
newDoc.parent = newParent; newDoc.parent = newParent;
collection.insert(newDoc); collection.insert(newDoc);
}); });
}); });
}, }
}); })
export {makeChild, makeParent, softRemoveNode};