Publication efficiency with a tree-like data structure


#1

So recently, I found a pretty helpful way to show data in a tree-like structure.
Say I have a user. The user has a one-to-many relationship with orders. The orders have a one-to-many relationship with products, and a one-to-one relationship to charges. A user document looks like this:

{
  "_id": "1234",
  "orders": ["1", "2", "3"]
}

So on each part of my view, I iterate over the relations and publish each result. For example:

if Template.subscriptionsReady
  with user
    +User.profile

    h1.title Orders
    each orders
      +Order.Item _id=this

And my orders template might look like this:

export class Order extends BlazeComponent {
  onCreated() {
    super.onCreated();
    this.autorun(() => this.subscribe('order', this.data()._id));
  }
  order() {
    return Orders.findOne(this.data()._id)
  }
}
Order.register('Order.Item');

My question is really, will subscribing to multiple smaller data sources cause a problem?


#2

Well, did you ever take a look at reywood:publish-composite which in fact does that for you without losing reactivity. It is also mentioned/suggested on the official guide.

That being said, publishing small chunks of data vs publishing a monolith can be a different take for each app. It not only depends on the exact data structure and size, but it also depends on the usage patterns of such publications.

Furthermore, you can leverage a subscription caching solution in either case, again, if it makes sense for the use case.

Sometimes, even overpublishing can help your app performance.

So I’d say try each case - if you have the luxury to - to find out for your app’s own patterns.

But remember, make sure you are testing your app as close to a standard user’s behaviour as possible.