Real-world example for Data Rules
Let’s looks at a real-world scenario as a use case for data rules in your app.
As a reminder, data rules let you define (1) which data is synced to users’ devices, and (2) what read and write access users have to data (via DB
and OnlineDB
).
The scenario: You want sensitive app settings such as pricing data to be readable and writable by global admins, read-only by regional admins and not at all accessible (no read or write access) by normal users. A constraint is that the volume of this pricing data is too large for offline access for global admins, so they will only be able to access this data while online.
Data model and data rules file
We’ll define the following data model:
Note the following about the data model:
Normal users, regional admins and global admins are distinguished by the
role
field on auser
Normal users and regional admins belong to a
region
. This relationship will form the root of their data buckets (we’ll talk about this more below).We have several explicit
has-many
relationships in theregion
model. These are also necessary for defining the data buckets as we’ll show below.We have a
client
model thatbelongs-to
aregion
. Client data is not considered sensitive: normal users, and regional admins will have access to all clients in their region, and global admins will have access to all clients across all regions. We’ll show this in the data rules below.The pricing models, i.e.
pricing_template
andpricing_item
, contain sensitive data. This data will be readable and writable by global admins, read-only by regional admins (for their region) and not at all accessible (no read or write access) by regular users. Additionally, admins will only be able to access these models usingOnlineDB
, since it’s too much data to sync to their devices.
Given the above, the data rules for this app could be implemented as follows:
Let’s dive into these data rules.
Define the data buckets
Note that we have defined two object-specific buckets and one global bucket.
The two object-specific data buckets are for normal users and regional admins respectively, since we need to group their data by the region
these users belong to. This can be seen in the via
attribute:
Global admins have access to all data across all regions, so we can define a global bucket for these users:
Define what data is synced for each user role
For normal users and for regional admins the following data is synced: the region
object that the user belongs to (as the bucket root), and all clients
belonging to this region
(per the <has-many name="clients" />
relationship). These object groups sync because of the implicit read="any"
rule on both the bucket (which includes its root object) and the <has-many name="clients" />
relationship. read="any"
is the default that is applied if no read
attribute is specified.
For regional admins, the pricing data belonging to their region
is also synced. This can be seen by the explicit read="any"
attribute on these relationships:
For global admins, all clients and regions are synced:
For global admins, pricing data is not synced, but is accessible via OnlineDB
. This is configured with the read="online"
attribute on these models:
Define what read & write access each user role has
Normal users and regional admins can read and write to the region
they belong to, as well as all clients belonging to that region. This is because of the implicit default rules on the bucket root (read="any"
and write="update,delete"
) and the implicit read="any"
and write="any"
rules on the <has-many name="clients" />
relationship.
Normal users cannot read any pricing data - as shown by the read="none"
attribute on these relationships:
Regional admins can read the pricing data belonging to their region, but cannot write to it (write="none"
):
Global admins can read and write to all data: all regions, clients and pricing data. However, they can only query pricing data via OnlineDB
, as is shown by the read="online"
rule:
For more info on the read
/ write
syntax, see the data ACL syntax reference here.
Last updated