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 Description

name

Name of the function.

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

coll

Document collection name.

ts

Most recent document update timestamp.

role

(Optional) 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

FQL function body.

data

(Optional) Object with metadata fields.

Static methods

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

Method Description

Dynamically get the Function by name.

Get the Set of all accessible user-defined Function documents.

Get a user-defined Function document by its name.

Create a user-defined Function document.

Get the first matching accessible user-defined Function document.

Get the name of the Function collection as a string.

Get the Set of matching accessible user-defined Function documents.

Instance properties

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

Property Description

Get or update a UDF’s definition, represented as a Function collection 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

Delete a function.

Verify that a function exists.

Replace function fields.

Update function fields.

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!