Role.firstWhere()

Get the first matching accessible role.

Signature

Role.firstWhere(predicate: () => Boolean)): Role | NullRole

Role.firstWhere(predicate: (val: Role) => Boolean)): Role | NullRole

Description

Each role in a Fauna is represented by a Role document. This method returns a Role for the first accessible role that matches the predicate function.

Parameters

Parameter Type Required Description

predicate

Function

Yes

An anonymous function that compares values in each Role and returns true for matches or false for mismatches.

predicate parameters:

Parameter Type Required Description

val

Role

An object to compare. If omitted, use dot notation to access role properties.

Return value

One of:

Type Description

Role

The Role for the first accessible role matching the predicate function.

NullRole

When no roles match the predicate function or all matching role are inaccessible. See NullDoc.

Examples

Role.firstWhere(.name.includes("manager"))
{
  name: "manager",
  coll: Role,
  ts: Time("2023-07-27T22:40:32.735Z"),
  privileges: [
    {
      resource: "Store",
      actions: {
        create: true,
        read: true,
        write: true,
        delete: true
      }
    },
    ...
    {
      resource: "Manager",
      actions: {
        read: "(ref) => Query.identity() == ref"
      }
    }
  ],
  membership: [
    {
      resource: "Manager"
    }
  ]
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!