From 189a1d0a1670822e06e90d96c29e29f2e971b0dc Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Fri, 12 Oct 2018 09:18:18 +0200 Subject: [PATCH] Started work on migrating model to lazy evaluation --- app/imports/Model/Campaign/Instance.js | 8 +- app/imports/Model/Campaign/Party.js | 6 +- app/imports/Model/Creature/Actions.js | 8 +- app/imports/Model/Creature/Attacks.js | 8 +- .../functions => imports/Model}/parenting.js | 89 ++++++++++++------- 5 files changed, 75 insertions(+), 44 deletions(-) rename app/{lib/functions => imports/Model}/parenting.js (74%) diff --git a/app/imports/Model/Campaign/Instance.js b/app/imports/Model/Campaign/Instance.js index b672b457..c47e2dd3 100644 --- a/app/imports/Model/Campaign/Instance.js +++ b/app/imports/Model/Campaign/Instance.js @@ -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 }); -Instances.attachSchema(Schemas.Instance); +Instances.attachSchema(instanceSchema); + +export default Instances; diff --git a/app/imports/Model/Campaign/Party.js b/app/imports/Model/Campaign/Party.js index d48dbb97..6c8c4ae8 100644 --- a/app/imports/Model/Campaign/Party.js +++ b/app/imports/Model/Campaign/Party.js @@ -1,6 +1,6 @@ -Parties = new Mongo.Collection("parties"); +let Parties = new Mongo.Collection("parties"); -Schemas.Party = new SimpleSchema({ +let partySchema = new SimpleSchema({ name: { type: String, defaultValue: "New Party", @@ -40,3 +40,5 @@ Parties.deny({ return _.contains(fields, "owner"); } }); + +export default Parties; diff --git a/app/imports/Model/Creature/Actions.js b/app/imports/Model/Creature/Actions.js index 31caa805..1318bb76 100644 --- a/app/imports/Model/Creature/Actions.js +++ b/app/imports/Model/Creature/Actions.js @@ -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 */ -Schemas.Action = new SimpleSchema({ +let actionSchema = new SimpleSchema({ charId: { type: String, regEx: SimpleSchema.RegEx.Id, @@ -31,10 +31,12 @@ Schemas.Action = new SimpleSchema({ }, }); -Actions.attachSchema(Schemas.Action); +Actions.attachSchema(actionSchema); Actions.attachBehaviour("softRemovable"); makeChild(Actions); Actions.allow(CHARACTER_SUBSCHEMA_ALLOW); Actions.deny(CHARACTER_SUBSCHEMA_DENY); + +export default Actions diff --git a/app/imports/Model/Creature/Attacks.js b/app/imports/Model/Creature/Attacks.js index 4a2dcc72..eeb8b2f6 100644 --- a/app/imports/Model/Creature/Attacks.js +++ b/app/imports/Model/Creature/Attacks.js @@ -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 */ -Schemas.Attack = new SimpleSchema({ +attackSchema = new SimpleSchema({ charId: { type: String, regEx: SimpleSchema.RegEx.Id, @@ -66,7 +66,7 @@ Schemas.Attack = new SimpleSchema({ }, }); -Attacks.attachSchema(Schemas.Attack); +Attacks.attachSchema(attackSchema); Attacks.attachBehaviour("softRemovable"); 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.deny(CHARACTER_SUBSCHEMA_DENY); + +export default Attacks; diff --git a/app/lib/functions/parenting.js b/app/imports/Model/parenting.js similarity index 74% rename from app/lib/functions/parenting.js rename to app/imports/Model/parenting.js index eb3e01f2..21edb341 100644 --- a/app/lib/functions/parenting.js +++ b/app/imports/Model/parenting.js @@ -1,4 +1,6 @@ -var childSchema = new SimpleSchema({ +import { ValidatedMethod } from 'meteor/mdg:validated-method'; + +let childSchema = new SimpleSchema({ parent: {type: Object}, "parent.collection": {type: String}, "parent.id": {type: String, regEx: SimpleSchema.RegEx.Id, index: 1}, @@ -10,14 +12,14 @@ var childSchema = new SimpleSchema({ }, }); -var joinWithDefaultKeys = function(keys){ - var defaultKeys = [ +let joinWithDefaultKeys = function(keys){ + let defaultKeys = [ "charId", ]; return _.union(keys, defaultKeys); }; -var limitModifierToKeys = function(modifier, keys){ +let limitModifierToKeys = function(modifier, keys){ if (!modifier) return; modifier = _.pick(modifier, ["$set", "$unset"]); if (modifier.$set) modifier.$set = _.pick(modifier.$set, keys); @@ -27,21 +29,21 @@ var limitModifierToKeys = function(modifier, keys){ return modifier; }; -var getParent = function(doc){ +let getParent = function(doc){ if (!doc || !doc.parent) return; - var parentCol = Meteor.isClient ? + let parentCol = Meteor.isClient ? window[doc.parent.collection] : global[doc.parent.collection]; if (parentCol) return parentCol.findOne(doc.parent.id, {removed: true}); }; -var inheritParentProperties = function(doc, collection){ - var parent = getParent(doc); +let inheritParentProperties = function(doc, collection){ + let parent = getParent(doc); if (!parent) throw new Meteor.Error( "Parenting Error", "Document's parent does not exist" ); - var handMeDowns = _.pick(parent, collection.inheritedKeys); + let handMeDowns = _.pick(parent, collection.inheritedKeys); if ( _.contains(collection.inheritedKeys, "charId") && doc.parent.collection === "Characters" @@ -52,9 +54,9 @@ var inheritParentProperties = function(doc, collection){ collection.update(doc._id, {$set: handMeDowns}); }; -var childCollections = []; +let childCollections = []; -makeChild = function(collection, inheritedKeys){ +let makeChild = function(collection, inheritedKeys){ inheritedKeys = inheritedKeys || []; if (inheritedKeys) { collection.inheritedKeys = joinWithDefaultKeys(inheritedKeys); @@ -102,9 +104,9 @@ makeChild = function(collection, inheritedKeys){ childCollections.push(collection); }; -makeParent = function(collection, donatedKeys){ +let makeParent = function(collection, donatedKeys){ donatedKeys = joinWithDefaultKeys(donatedKeys); - var collectionName = collection._collection.name; + let collectionName = collection._collection.name; //after changing, push the changes to all children collection.after.update(function(userId, doc, fieldNames, modifier, options) { modifier = limitModifierToKeys(modifier, donatedKeys); @@ -129,8 +131,8 @@ makeParent = function(collection, donatedKeys){ }); }; -var checkPermission = function(userId, charId){ - var char = Characters.findOne(charId, {fields: {owner: 1, writers: 1}}); +let checkPermission = function(userId, charId){ + let char = Characters.findOne(charId, {fields: {owner: 1, writers: 1}}); if (!char) throw new Meteor.Error("Access Denied, no charId", "Character " + charId + " does not exist"); @@ -143,7 +145,7 @@ var checkPermission = function(userId, charId){ return true; }; -var cascadeSoftRemove = function(id, removedWithId){ +let cascadeSoftRemove = function(id, removedWithId){ _.each(childCollections, function(treeCollection){ treeCollection.update( {"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(id, String); - var collection = Mongo.Collection.get(collectionName); - var node = collection.findOne(id); - var charId = node && node.charId; + let collection = Mongo.Collection.get(collectionName); + let node = collection.findOne(id); + let charId = node && node.charId; checkPermission(self.userId, charId); }; -Meteor.methods({ - softRemoveNode: function(collectionName, id){ +const softRemoveNode = new ValidatedMethod({ + 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); - var collection = Mongo.Collection.get(collectionName); + let collection = Mongo.Collection.get(collectionName); collection.softRemove(id); cascadeSoftRemove(id, id); }, - restoreNode: function(collectionName, id){ +}); + +const restoreNode = new ValidatedMethod({ + run(collectionName, id){ checkRemovePermission(collectionName, id, this); - var collection = Mongo.Collection.get(collectionName); + let collection = Mongo.Collection.get(collectionName); collection.restore(id); _.each(childCollections, function(treeCollection){ 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(modifier, Object); checkPermission(this.userId, parent.charId); - var selector = {"parent.id": parent._id}; + let selector = {"parent.id": parent._id}; _.each(childCollections, function(collection){ - var thisModifier; + let thisModifier; if (limitToInheritance){ thisModifier = limitModifierToKeys(modifier, collection.inheritedKeys); } else { @@ -203,17 +221,22 @@ Meteor.methods({ collection.update(selector, thisModifier, {multi: true, removed: true}); }); }, - cloneChildren: function(objectId, newParent){ +}); + +const cloneChildren = new ValidatedMethod({ + run({objectId, newParent}){ check(objectId, String); check(newParent, {id: String, collection: String}); _.each(childCollections, function(collection){ - var keys = collection.simpleSchema().objectKeys(); + let keys = collection.simpleSchema().objectKeys(); collection.find({"parent.id": objectId}).forEach(function(doc){ - var newDoc = _.pick(doc, keys); + let newDoc = _.pick(doc, keys); newDoc.parent = newParent; collection.insert(newDoc); }); }); - }, -}); + } +}) + +export {makeChild, makeParent, softRemoveNode};