Skip to content

Instantly share code, notes, and snippets.

@heschong
Created September 14, 2015 06:26
Show Gist options
  • Select an option

  • Save heschong/b0d22ef472b02a9c0e97 to your computer and use it in GitHub Desktop.

Select an option

Save heschong/b0d22ef472b02a9c0e97 to your computer and use it in GitHub Desktop.

Revisions

  1. heschong revised this gist Sep 14, 2015. No changes.
  2. heschong created this gist Sep 14, 2015.
    41 changes: 41 additions & 0 deletions flexible_publications.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    /*
    * 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' });