I am trying to set up v2 sync rules based on the relationships defined in my data model. For this example there are 3 pertinent entities:
- Channel
- Product
- Product Monitor
A user has a simple belongs-to relationship to a channel, and a channel has 2 "has many" relationships to products and product monitors. Here is a trimmed-down version of the data model:
<data-model>
<model name="user" label="User">
<field name="name" label="Name" type="text:name"/>
<belongs-to model="channel" name="Channel"/>
<display>{name}</display>
</model>
<model name="channel" label="Channel">
<field name="tsf_code" label="TSF Code" type="text:name"/>
<field name="name" label="Name" type="text:name"/>
<has-many model="channel_product" name="Sells" />
<has-many model="product_monitor" name="Monitors" />
<display>{name}</display>
</model>
<!-- Link to enable many to many relationship between channels and products -->
<model name="channel_product" label="ChannelProduct">
<belongs-to model="channel"/>
<belongs-to model="product"/>
<display>{channel}{product}</display>
</model>
<model name="product" label="Product">
<field name="name" label="Name" type="text:name"/>
<field name="code" label="Code" type="text"/>
<belongs-to model="channel_product" name="Soldby"/>
<display>{name}</display>
</model>
<model name="product_monitor" label="Product Monitor">
<field name="kind" label="Kind" type="single-choice">
<option key="mtd">Month To Date</option>
<option key="ytd">Year To Date</option>
<option key="tdy">Today</option>
<option key="all">All time</option>
</field>
<belongs-to model="channel" name="Channel"/>
<belongs-to model="product" name="Product"/>
<display>{Channel}:{Product} ({kind})</display>
</model>
</data-model>
My aim is to set up sync rules to only push down the product and product monitor objects linked to the user via the channel - which seems consistent with the example at https://docs.journeyapps.com/reference/build/data-model-configuration/other-topics-data-model/sync-rules-v2
The only difference from the example I see is the additional link to channel_product, so I am not 100% sure how to incorporate this?
Finally, here is the sync rule I defined for the above:
<bucket via="self/Channel">
<!-- The root itself (channel in this case) is automatically included in the bucket -->
<!-- sync all products linked to this channel via the has-many relationship -->
<has-many name="Sells" />
<!-- sync all product monitors linked to this channel via the has-many relationship -->
<has-many name="Monitors" />
</bucket>
Assuming I have set up my data corrcetly, I would expect to get the approriate objects whenever I do a DB.model.all()?
Here is a little test method to do that:
function fetchMonitors() {
var monitors =
DB.product_monitor.all().toArray();
console.log('Monitors:',monitors);
var product =
DB.product.all().toArray();
console.log('Products:',product);
var channel =
DB.channel.all().toArray();
console.log('Channels:',channel);
notification.success('Counts: Channel[' + channel.length +'] Product ['+product.length+'] Monitor ['+monitors.length+']');
}
At the moment the only object that does return is the Channel (so the root of my sync rule).
Am I missing something? Should I set up additional sync bucket for the many-to-many link "channel_product"?