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.

functionDef.update()

Learn: User-defined functions (UDFs)

We recommend you use FSL to create and update user-defined functions (UDFs). See FSL function schema.

Signature

update(data: { *: Any }) => FunctionDef

Description

Updates a UDF, represented as an Function document, with fields from a provided data object.

During the update, fields from the data object are copied to the document, creating new fields or updating existing fields. The operation is similar to a merge.

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

Nested fields

Fields with nested objects in the data object are merged with the identically named nested object in the document.

Remove a field

To remove a document field, set its value in the data object to null.

Metadata fields

You can’t use this method to insert or edit the following metadata fields:

  • coll

  • ts

Staged schema

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

You can’t rename a function while a database has staged schema.

If the database has no staged schema, using this method is equivalent to making an unstaged schema change. Changes are applied immediately to the database’s active schema.

Avoid concurrent schema changes

Concurrent unstaged schema changes can cause contended transactions, even if the changes affect different resources. This includes unstaged changes made using:

A schema change triggers a transaction that validates the entire database schema. To avoid errors, do one of the following instead:

Parameters

Parameter Type Required Description

data

Object

true

Document fields for the Function document.

For supported document fields, see Function collection.

The object can’t include the following metadata fields:

* coll * ts

Return value

Type Description

FunctionDef

The updated Function document.

Examples

Function.byName("validateOrderStatusTransition")?.update({
  name: "validateOrderStatusTransition",
  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,
  role: "server"
})
{
  name: "validateOrderStatusTransition",
  coll: Function,
  ts: Time("2099-10-25T18:16:27.725Z"),
  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,
  role: "server"
}

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!