Skip to content

Instantly share code, notes, and snippets.

@heschong
Created September 14, 2015 06:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heschong/b0d22ef472b02a9c0e97 to your computer and use it in GitHub Desktop.
Save heschong/b0d22ef472b02a9c0e97 to your computer and use it in GitHub Desktop.
Sample Meteor pattern for flexible publications
/*
* This is a simple pattern for a flexible publication mechanism, for feedback purposes
*/
// ... on client and server
MyCollection = new Mongo.Collection('mycollection');
// ... on the server
/*
* This function allows us to check to see if a MongoDB query object is
* relatively safe from NoSQL injection attempts
*
* Usage via a check function:
*
* check(arg, Match.Where(safeQuery));
*/
safeQuery = function(value) {
return !EJSON.stringify(value).match(/\"\$(where|inc|mul|rename|setOnInsert|set|unset|currentDate)\"/g);
}
// Publish a document set with the client's dynamic specifiers
Meteor.publish('mycollection', function(where) {
check(where, Match.Where(safeQuery));
return MyCollection.find(where);
});
// ... On the client
// Subscribe to all the documents matching { some: 'criteria' }
Meteor.subscribe('mycollection', { some: 'criteria' });
var stuff = MyCollection.find({ someMore: 'criteria' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment