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.create()

Learn: User-defined functions (UDFs)

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

Signature

Function.create(data: { name: String, alias: String | Null, signature: String | Null, role: String | Null, body: String, data: { *: Any } | Null }) => FunctionDef

Description

Creates an UDF with the provided document fields. Fauna stores UDFs as documents in the Function system collection.

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 adds a function to the staged schema, not the active 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

Document fields for the new Function document.

For supported document fields, see Function collection.

Return value

Type Description

FunctionDef

The new Function document.

Examples

  1. Create a UDF:

    Function.create({
      name: 'hello',
      body: '(x) => "Hello #{x}!"'
    })
    {
      name: "hello",
      coll: Function,
      ts: Time("2099-10-25T17:56:30.150Z"),
      body: "(x) => \"Hello \#{x}!\""
    }
  2. Call the UDF:

    hello("World")
    "Hello World!"

 

  1. Use heredoc syntax to avoid interpolation until the function executes:

    Function.create({
      name: "hello2",
      body: <<-EOB
    name => "Hello to you, #{name}!"
    EOB
    })
    {
      name: "hello2",
      coll: Function,
      ts: Time("2099-11-03T17:06:02.790Z"),
      body: <<-END
        name => "Hello to you, #{name}!"
        END
    }
  2. Call the UDF:

    hello2("World")
    "Hello to you, World!"

 

Create a UDF with the admin role:

Function.create({
  name: 'Doublex',
  body: '(x) => x + x',
  role: 'admin',
})
{
  name: "Doublex",
  coll: Function,
  ts: Time("2099-06-25T15:03:14.060Z"),
  body: "(x) => x + x",
  role: "admin"
}

 

Create a UDF with a data metadata field:

Function.create({
  name: "square",
  body: "x => x * x",
  data: {
    version: "1.0"
  }
})
{
  name: "square",
  coll: Function,
  ts: Time("2099-01-08T20:30:50.090Z"),
  body: "x => x * x",
  data: {
    version: "1.0"
  }
}

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!