diff --git a/meteor-publish-composite/meteor-publish-composite-tests.ts b/meteor-publish-composite/meteor-publish-composite-tests.ts new file mode 100644 index 000000000..ad84c0f3a --- /dev/null +++ b/meteor-publish-composite/meteor-publish-composite-tests.ts @@ -0,0 +1,60 @@ +/// +/// + +import User = Meteor.User; +interface IPost { _id : string, authorId : string }; +interface IComment { authorId : string }; +var Posts : Mongo.Collection = new Mongo.Collection('Posts'); +var Comments : Mongo.Collection = new Mongo.Collection('Comments'); + +// Server +Meteor.publishComposite('topTenPosts', { + find: function() : Mongo.Cursor { + // Find top ten highest scoring posts + return Posts.find({}, { sort: { score: -1 }, limit: 10 }); + }, + children: [ + { + find: function(post) { + // Find post author. Even though we only want to return + // one record here, we use "find" instead of "findOne" + // since this function should return a cursor. + return Meteor.users.find( + { _id: post.authorId }, + { limit: 1, fields: { profile: 1 } }); + } + }, + { + find: function(post) { + // Find top two comments on post + return Comments.find( + { postId: post._id }, + { sort: { score: -1 }, limit: 2 }); + }, + children: [ + { + find: function(comment, post) { + // Find user that authored comment. + return Meteor.users.find( + { _id: comment.authorId }, + { limit: 1, fields: { profile: 1 } }); + } + } + ] + } + ] +}); + +// Server +Meteor.publishComposite('postsByUser', function(userId, limit) { + return { + find: function() { + // Find posts made by user. Note arguments for callback function + // being used in query. + return Posts.find({ authorId: userId }, { limit: limit }); + }, + children: [ + // This section will be similar to that of the previous example. + ] + } +}); diff --git a/meteor-publish-composite/meteor-publish-composite.d.ts b/meteor-publish-composite/meteor-publish-composite.d.ts new file mode 100644 index 000000000..ddab6ef47 --- /dev/null +++ b/meteor-publish-composite/meteor-publish-composite.d.ts @@ -0,0 +1,65 @@ +// Type definitions for meteor-publish-composite +// Project: https://github.com/englue/meteor-publish-composite +// Definitions by: Robert Van Gorkom +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare interface PublishCompositeConfigN { + children? : PublishCompositeConfigN[]; + find( + ...args : any[] + ) : Mongo.Cursor; +} + +declare interface PublishCompositeConfig4 { + children? : PublishCompositeConfigN[]; + find( + arg4 : InLevel4, + arg3 : InLevel3, + arg2 : InLevel2, + arg1 : InLevel1 + ) : Mongo.Cursor; +} + +declare interface PublishCompositeConfig3 { + children? : PublishCompositeConfig4[]; + find( + arg3 : InLevel3, + arg2 : InLevel2, + arg1 : InLevel1 + ) : Mongo.Cursor; +} + +declare interface PublishCompositeConfig2 { + children? : PublishCompositeConfig3[]; + find( + arg2 : InLevel2, + arg1 : InLevel1 + ) : Mongo.Cursor; +} + +declare interface PublishCompositeConfig1 { + children? : PublishCompositeConfig2[]; + find( + arg1 : InLevel1 + ) : Mongo.Cursor; +} + +declare interface PublishCompositeConfig { + children? : PublishCompositeConfig1[]; + find() : Mongo.Cursor; +} + +declare module Meteor { + function publishComposite( + name : string, + config : PublishCompositeConfig|PublishCompositeConfig[] + ) : void; + + function publishComposite( + name : string, + configFunc : (...args : any[]) => + PublishCompositeConfig|PublishCompositeConfig[] + ) : void; +}