-
Let's say I have 3 collections: Roles, Articles, and Users.
// Users collection
{
slug: "users"
....
fields: [
{
name: "roles"
type: "relationship"
relationTo: "user-roles" // User Roles collection
hasMany: true,
}
]
}
// Articles collection
{
slug: "articles"
....
fields: [
{
name: "whoCanView"
type: "relationship"
relationTo: "user-roles"
hasMany: true,
}
]
} Now, if the field is const query = {
whoCanView: {
equals: "user.roles"
}
} However, I was not able to find a sample for a many-to-many comparison. Even one-to-one ( With that said, can someone shed a light on this matter for me? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It just came to my mind that I have not updated the solution on this. When a record hasMany relations, the related objects are referenced by ID. For example, if an
Hence comparison should be based on Each query is considered an object itself, we can construct a dynamic query for many-to-many comparisons. Let's have a sample context here:
// read access function
...
const query = {
or: []
}
for (let role of user.roles) {
query.or.push({
recordReadAccess: {
contains: role.id
}
}
}
// In other words, allow read access if the record contains any of the user roles
// => Contains "Management" or "Front-desk"
return query; |
Beta Was this translation helpful? Give feedback.
It just came to my mind that I have not updated the solution on this.
When a record hasMany relations, the related objects are referenced by ID. For example, if an
article
can be read byfront-desk
ormanagement
, then the database is represented as followed.Hence comparison should be based on
object.id
, notobject.otherValue
.Each query is considered an object itself, we can construct a dynamic query for many-to-many comparisons. Let's have a sample context here: