Function.firstWhere()

Learn: User-defined functions (UDFs)

Get the first matching accessible user-defined Function document.

Signature

Function.firstWhere(predicate: () => Boolean)) => <Function> | NullFunctionDef

Function.firstWhere(predicate: (val: Function) => Boolean)) => <Function> | NullFunctionDef

Description

Each user-defined function (UDF) document in a Fauna database is represented by a function object. The Function.firstWhere() method returns the first accessible function object that matches the predicate function.

Staged schema

If a database has staged schema, this method interacts with the database’s staged schema, not the active schema.

Parameters

Parameter Type Required Description

predicate

Function

Yes

Anonymous function that compares values in each UDF document and returns true for matches or false for mismatches.

predicate parameters:

Parameter Type Required Description

val

Collection

UDF object to compare. If omitted, use dot notation to access function properties.

Return value

One of:

Type Description

Function

First accessible UDF document matching the predicate function. See Function.

NullFunctionDef

When no UDF documents match the predicate function or all matching UDF documents are inaccessible.

Examples

Get the first UDF when at least one matching UDF document exists and is accessible:

Function.firstWhere(.name.includes('validate'))
{
  name: "validateOrderStatusTransition",
  coll: Function,
  ts: Time("2099-10-25T17:49:28.145Z"),
  body: <<-END
    (oldStatus, newStatus) => {
      if (oldStatus == "cart" && newStatus != "processing") {
        abort("Invalid status transition.")
      } else if (oldStatus == "processing" && newStatus != "shipped") {
        abort("Invalid status transition.")
      } else if (oldStatus == "shipped" && newStatus != "delivered") {
        abort("Invalid status transition.")
      }
    }
  END
}

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!