Skip to content
Jan Dvorak edited this page Mar 8, 2016 · 2 revisions

Publications

The following are some examples of publishing joined data for posts.

var publicationOptionsSchema = new SimpleSchema({
  limit:{
    type: Number,
    optional: true
  },
  skip:{
    type: Number,
    optional: true
  },
  sort:{
    type: Number,
    optional: true
  }
});

Meteor.publish('posts', function(options) {
    var friendMap;

    if(!this.userId){
        return this.ready();
    }

    friendMap = Meteor.friends.find({userId:this.userId}, {fields:{friendId:true}}).map(function(friend){
        return friend.friendId;
    });

    friendMap.push(this.userId);

    options = options || {};

    check(options, publicationOptionsSchema)

    //only allow the limit and skip options
    options = _.pick(options, "limit", "skip", "sort");

    Meteor.publishWithRelations({
        handle: this,
        collection: Meteor.posts,
        filter: {$or:[{userId:{$in:friendMap}}, {posterId:{$in:friendMap}}]},
        options:options,
        mappings: [{
            key: 'userId',
            collection: Meteor.users,
            options:{fields:{username:true, avatarId:true}}
        }, {
            reverse: true,
            key: 'linkedObjectId',
            collection: Meteor.comments,
            options:{sort:{date:-1}, limit:3},
            mappings: [{
                key: 'userId',
                collection: Meteor.users,
                options:{fields:{username:true, avatarId:true}}
            }]
        }]
    });

});

Clone this wiki locally