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

Learn: User-defined functions (UDFs)

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

A user-defined function (UDF) is a set of one or more FQL statements stored as a reusable resource in a Fauna database. Like a stored procedure in SQL, a UDF can accept parameters, perform operations, and return results.

Function collection

Fauna stores UDFs as documents in the Function system collection. These documents have the FunctionDef type and are an FQL version of the FSL function schema.

Function documents have the following FQL structure:

{
  name: "getOrCreateCart",
  coll: Function,
  ts: Time("2099-09-25T21:53:08.780Z"),
  role: "server",
  body: <<-END
    (id) => {
      let customer = Customer.byId(id)!
      if (customer!.cart == null) {
        Order.create({
          status: "cart",
          customer: Customer.byId(id),
          createdAt: Time.now(),
          payment: {
          }
        })
      } else {
        customer!.cart
      }
    }
  END
}
Field Type Read-only Required Description

name

true

Name of the function.

Can’t be documents, events, self, sets, or a single underscore _, and can’t include a space character.

coll

true

Collection name: Function.

ts

true

Last time the document was created or updated.

role

Role to use when the function is called. Only included if role is provided when the UDF is created.

Can be a built-in role, admin, server, or server-readonly, or a user-defined role that grants write privilege for Functions.

body

true

FQL function body.

data

Arbitrary user-defined metadata for the document.

Static methods

You can use the following static methods to manage the Function collection in FQL.

Method Description

Call a user-defined function (UDF) by its name.

Get a Set of all user-defined functions (UDFs).

Get a user-defined function (UDF) by its name.

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

Get "Function" as a String.

Get a Set of user-defined functions (UDFs) that match a provided predicate.

Instance properties

Function documents have the following properties. You access the property using an existing UDF’s name.

Property Description

Get or update a user-defined function (UDF)'s definition, represented as a Function document.

Instance methods

You can use the following instance methods to manage function definitions, represented as Function documents, in FQL. You call the methods on a FunctionDef.

Method Description

Test if a user-defined function (UDF) exists.

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!