Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

Function.byName()

Learn: User-defined functions (UDFs)

Get a user-defined Function document by its name.

Signature

Function.byName(name: string) => NamedRef<FunctionDef>

Description

The Function.byName() method gets the Function object if it exists and is accessible.

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

name

String

Yes

Name of the UDF document to get.

Return value

Type Description

NamedRef<FunctionDef>

Resolved reference to a Function document. Can resolve to an existing document or a NullDoc.

Examples

  1. Fetch a UDF when it exists and is accessible:

    Function.byName("validateOrderStatusTransition")
    {
      name: "validateOrderStatusTransition",
      coll: Function,
      ts: Time("2024-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
    }
  2. When the UDF document doesn’t exist or is inaccessible:

    Function.byName("doesnotexist")
    Function.byName("doesnotexist") /* not found */

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!