grapher-helpers
Provides common cultofcoders:grapher querires out of the box, as well as creates helper methods for links providing a more OOP syntax.
To install:
meteor add wesleyfsmith:grapher-helpers
Creating helpers from links
Now, when you define your links you can automatically create associated helpers. Let's say you have a Mongo.Collection called Practices.
1Practices.addLinksWithHelpers({ 2 employees: { 3 collection: Meteor.users, 4 type: 'many', 5 field: 'employeeIds' 6 } 7});
And then you can call:
1let practice = Practices.findOne(); 2practice.employees() // returns all employees
Which is the equivalent of:
1let practice = Practices.findOne(); 2Practices.findLink(practice._id, 'employees').fetch();
Note that if you haven't subscribed to the appropriate documents these helpers won't return anything.
Common queries
Most often you want to query an object and just get all the fields by default. With you package you can do this by running:
1let query = Practices.findQuery()
This creates a query that will grab all the fields off the collections' simple schema.
If you want to only query a single document you can run:
1let query = Practices.findOneQuery('id')
This creates a query that will grab all the fields off the collections' simple schema, and then only query for that one document.
Created by query
If you create your collection like this:
1 2import {createdByLink} from 'meteor/wesleyfsmith:grapher-helpers'; 3 4Practices = new Mongo.Collection('practices'); 5 6Practices.attachSchema(new SimpleSchema({ 7 name: { 8 type: String 9 }, 10 employeeIds: { 11 type: [String], 12 optional: true, 13 autoform: { 14 omit: true 15 } 16 }, 17 createdAt: { 18 type: Date, 19 defaultValue: new Date(), 20 autoform: { 21 omit: true 22 } 23 }, 24 createdById: createdByLink(Practices) 25}
The createdByLink function will set the following on your simple schema:
1createdById: { 2 type: String, 3 denyUpdate: true, // for a createdBy, shouldn't be able to update. 4 autoValue: function() { 5 if (this.isInsert) { 6 return this.userId; 7 } 8 }, 9 allowedValues: function () { 10 return this.userId; 11 }, 12 autoform: { 13 omit: true 14 } 15 }
AND it will create a link as well as a helper. Now you can call:
1let practice = Practices.findOne(); 2practice.createdBy(); // returns the creator, assuming you've subscribed to that document
OR you can still get the link manually:
1let practice = Practices.findOne(); 2Practices.findLink(practice._id, 'createdBy').fetchOne();
Finally, you can also create a default query for documents that were created by a specific user:
1Practices.findCreatedByQuery();
If you specify an id, it will query for documents created with that id, otherwise it will default to Meteor.userId();