使用 publishComposite 加入 Meteor [英] Join in Meteor with publishComposite

查看:31
本文介绍了使用 publishComposite 加入 Meteor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从我的 Meteor 应用程序中删除了 autopublish.现在我正在手动发布我的收藏.我有一些相关的收藏.我想尽可能提高性能.

I have removed autopublish from my Meteor app. Now I'm publishing my collections manually. I have some related collections. I want to increase performance as much as possible.

例如,如果我正在查看一个帖子并想查看与该帖子相关的所有评论,我必须使用 post: Posts.findOne(postId) 查询数据库AND comments: Comments.find({postId: postId}).我正在使用 iron-router 查询数据字段中的两个集合,因此它们出现在我的模板中,但我也在 waitOn 中订阅了出版物.现在我找到了 https://github.com/englue/meteor-publish-composite这让我可以同时发布多个集合.但我不太明白.如果我在 server/publish.js 中使用 Meteor.publishComposite('postAndComments', ...),在 中订阅 postAndComments>waitOn,并像往常一样在 data 中设置 postcomments,然后我会保存对数据库?在我看来,我仍然以相同的次数查询数据库.但是,在发布查询时进行的查询是否仅在查询完成时进行 data 只是一种检索已从数据库中查询的内容的方法?

If I'm, for instance, looking at a post and want to see all the comments related to this post, I have to query the database with both post: Posts.findOne(postId) AND comments: Comments.find({postId: postId}). I am querying the two collections in the data field with iron-router so they are present in my template but I'm also subscribing the publications in waitOn. Now I have found https://github.com/englue/meteor-publish-composite which lets me publish multiple collections at the same time. But I don't quite understand it. If I'm using Meteor.publishComposite('postAndComments', ...) in server/publish.js, subscribing postAndComments in waitOn, and setting both post and comments in data as I normally do, will I then have saved a demand on the database? To me it looks like I still query the database the same number of times. But is the queries done when publishing the only queries made while the queries done i data is only a way to retrieve what has already been queried from the database?

此外,在示例 1 中,展示了如何发布具有所属评论和帖子/评论作者的热门帖子,但在模板中,仅输出帖子.我怎样才能输出评论和作者?我是否误解了 publishComments 的潜力?我把它理解为一种连接.

Besides, in example 1, it is shown how to publish top posts with the belonging comments and post/comment authors, but in the template, only the posts are outputted. How can I also output the comments and authors? Have I misunderstood the potentials of publishComments? I understand it as a kind of a join.

推荐答案

我成功使用了 publishComposite.在下面的示例中,我订阅了与 filter 以及这些单元所属的 Properties 匹配的 Units.

I used publishComposite successfully. In my example below I'm subscribing to Units matching to filter as well as Properties those units belong to.

Meteor.publishComposite('unitsWithProperties', function (filter) {
    var filter = filter || {};
    console.log(filter);
    return {
        find: function () {
            var units;
            units = Units.find(filter);
            return units;
        },
        children: [
            {
                collectionName: 'properties',
                find: function (unit) {
                    return Properties.find({ _id: unit.propertyId });
                }
            }
        ]
    };
});

就您而言,我相信您可以:

In your case I believe you can:

  • 订阅符合 TopPosts 标准的帖子
  • children: 游标数组
  • 中订阅这些帖子的评论和作者
  • subscribe to Posts matching TopPosts criteria
  • subscribe to Comments and Authors of those posts in children: array of cursors

.

希望这会有所帮助.

亚历克斯

这篇关于使用 publishComposite 加入 Meteor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆