Function.create()

Create a user-defined Function document.

Signature

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

Description

Function.create() creates a user-defined Function (UDF) from the attributes provided in the data parameter.

After it is created, the UDF is invoked with the provided name.

When the function body includes variable interpolation, use one of the following strategies to avoid immediate interpolation:

  • Enclose the function body in single quotation marks.

  • Escape the interpolation markers.

  • Use heredoc syntax to declare a multiline literal body.

A role can be assigned to the function to give the function the privileges of that role.

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

Function.byName("Doublex")!.delete()
{
  "name": "Doublex",
  "coll": "Function"
}

Parameters

Parameter Type Required Description

data

Object

Yes

Object describing the UDF document to create.

data fields

Name Type Required Description

name

String

Yes

Name of the UDF document to create where name can’t be a reserved word, case insensitive.

body

String

Yes

FQL expression for the UDF.

role

String

Role that should be used when the UDF is called. This is typically be used for privilege escalation when current privileges would otherwise be too restrictive.
To use a built-in role, use admin, server, or server-readonly. A function must declare one of these built-in roles to be able to view logs.
The role can be set only by users with a privileged role, such as admin, server, or a user-defined role that grants write privilege for Functions.

IMPORTANT: Use role carefully. Setting the role privilege gives the function permission to create, change, and remove documents when invoked by calling the function. Users that can write functions can adjust role to change function privileges.

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}!\""
    }

    Call the UDF:

    hello("World")
    "Hello World!"
  2. Use heredoc syntax to avoid interpolation until function execution:

    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"
    }

    Call the UDF:

    hello2("World")
    "Hello to you, World!"
  3. 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"
    }

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!