Realm rule for tenant ID (multiple users per tenant)

Yes, in a way. As such, every document would belong to a group, rather than a user.

I’ve come from an 8 year background working with MySQL and a couple of years working with GCP, but both MongoDB and Stitch/Realm are new to me.

I initially posted this question when I was just starting to play with Realm, but having now started the setup of our app, I’m starting to get to grips with it. Not as convoluted as it initially appeared to me.

What I’m now looking at … please correct me if you think there is a better way … is the following:

Collection: user (assigned as Realm’s custom user data collection).
Property: tenant_id (string: contains reference to _id of tenant document).
Rule: All users are unable to write to tenant_id.

Collection: tenant (created when first user registers).
Property: _id: ObjectID(xyz)
Rule: All users are able to read the tenant document with _id referenced within their user document.

Trigger: On initial user creation - creates ‘tenant’ document, then updates user’s document (customData) with tenant_id reference. Subsequent users are invited to register via emailed URL, which contains a token relating back to the tenant_id, allowing the same ID to be written to their user document.

Applied to every other collection…
Schema: property: { tenant_id: ‘string’, default: %%user.id }
Rule: All users are able to read/write any document with { tenant_id: %%user.customData.tenant_id }

Does the above make sense? If so, does this seem like a sensible and logical approach, please?

Thank you in advance!