Function.create()

Learn: User-defined functions

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

Create a user-defined Function document.

Signature

Function.create(data: {*: any}): <Function>

Description

The Function.create() method creates a user-defined Function (UDF) with the provided data, which includes a user-defined function name field. The function is subsequently invoked by its name.

To delete a UDF, use the delete() method on the UDF instance:

Function.byName("myFunction")!.delete()

Parameters

Parameter Type Required Description

data

Object

Yes

Object describing the UDF. See data fields.

data fields

Name Type Required Description

name

String

Yes

Unique name of the UDF. The name is case insensitive and can’t be a reserved word.

body

String

Yes

FQL expression of the function.

If the function body includes variable interpolation, use one of the following methods to avoid interpolation until the function executes:

  • Enclose the function body in single quotation marks.

  • Escape the interpolation markers.

  • Use heredoc syntax to declare a multiline literal body.

FQL supports the …​ syntax for variadic parameters. See Variadic functions.

role

String

Role to apply when the UDF is called to give the UDF the privileges to that role. Built-in roles include admin, server, and server-readonly. Setting the role privilege gives the function permission to create, change, and remove documents. A UDF can also change the function privileges of a role.

The role can be defined only if you have sufficient privilege, such as admin, server, or a user-defined role that grants write privilege to functions.

Except as noted, a UDF can’t invoke an action that exceeds the privileges of role.

A caller who is a member of a user-defined role with limited privileges can execute a function that has a role that grants the call action privilege. The call action permits the function to execute even if it results in actions that exceed the limited privileges of the caller. Take care when assigning roles with create and call privileges to functions, such as the server role.

data

Object

User-defined metadata.

Return value

Type Description

Function

New UDF document. See Function document definition.

Examples

  1. Create a UDF:

    Function.create({
      name: 'hello',
      body: '(x) => "Hello #{x}!"',
    })
    {
      name: "hello",
      coll: Function,
      ts: Time("2022-10-25T21:01:21.110Z"),
      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("2022-11-03T17:06:02.790Z"),
      body: "name => \"Hello to you, #{name}!\"\n"
    }
  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: "2023-04-22T13:19:08.080x",
  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("2024-01-08T20:30:50.090Z"),
  data: {
    version: "1.0"
  },
  body: "x => x * x"
}

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!