Function.firstWhere()

Learn: User-defined functions (UDFs)

Get the first user-defined function (UDF) that matches a provided predicate.

Signature

Function.firstWhere(pred: (FunctionDef => Boolean)) => FunctionDef | Null

Description

Gets the first UDF, represented as an Function document, that matches a provided predicate function.

Function documents are FQL versions of a database’s FSL function schema. Function documents have the FunctionDef type. See User-defined functions (UDFs).

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

pred

Predicate function

Yes

Anonymous predicate function that:

The method returns the first Function document for which the predicate returns true.

Return value

One of:

Type Description

FunctionDef

First Function document that matches the predicate.

Null

No Function document matches the predicate.

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
}
\