Meteor package to transform documents on publish
meteor add maximum:server-transform
Initialize a collection
var Posts = new Mongo.Collection('posts');
add a transform method to an collection object or a cursor (you can add multiple methods, not only one)
// example: persist the author name on the post object (reactive)
Posts.serverTransform(function(doc) {
author = Authors.findOne(doc.authorId);
doc.authorName = author.fullname;
return doc;
});
//transform a cursor (only in a publishTransformed method)
Posts.find().serverTransform(function(doc) {
author = Authors.findOne(doc.authorId);
doc.authorName = author.fullname;
return doc;
});
normally you want to add custom (computed) properties instead of transforming the whole document. You can do this by passing an object to serverTransform
.
Posts.serverTransform({
// example: persist the comments count as a property
// (without publishing any comments and also reactive!)
commentsCount: function(doc) {
return Comments.find({
postId: doc._id
}).count();
}
});
If a computed property returns a new cursor, the cursor will also be transformed and published (this works recursively)
Posts.serverTransform({
allAuthors: function(doc) {
return Authors.find({
postId: doc._id
}, {
reactive: true
});
}
});
make sure that you publish a Posts
cursor with Meteor.publishTransformed
to apply the transformations
Meteor.publishTransformed('posts', function() {
return Posts.find(); // you can also publish multiple cursors by returning an array
});
sometimes you only want local transformations.
Meteor.publishTransformed('posts_2', function() {
return Posts.find().serverTransform({
// we extending the document with the custom property 'commentsCount'
commentsCount: function(doc) {
return Comments.find({
postId: doc._id
}).count();
}
});
});