Function.create()
Create a user-defined Function document.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
data |
Yes |
Object describing the UDF. See data fields. |
data fields
Name | Type | Required | Description |
---|---|---|---|
name |
Yes |
Unique name of the UDF. The name is case insensitive and can’t be a reserved word. |
|
body |
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:
FQL supports the |
|
role |
Role to apply when the UDF
is called to give the UDF the privileges to that role. Built-in roles
include The role can be defined only if you have sufficient privilege, such as
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 |
||
data |
User-defined metadata. |
Return value
Type | Description |
---|---|
New UDF document. See Function document definition. |
Examples
-
Create a UDF:
{ name: "hello", coll: Function, ts: Time("2022-10-25T21:01:21.110Z"), body: "(x) => \"Hello #{x}!\"" }
-
Call the UDF:
"Hello World!"
-
Use heredoc syntax to avoid interpolation until the function executes:
{ name: "hello2", coll: Function, ts: Time("2022-11-03T17:06:02.790Z"), body: "name => \"Hello to you, #{name}!\"\n" }
-
Call the UDF:
"Hello to you, World!"
Create a UDF with the admin
role:
{
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:
{
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!