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 | Read-only | Required | Description |
---|---|---|---|---|
|
true |
Name of the function. Can’t be |
||
|
true |
Collection name: |
||
|
true |
Last time the document was created or updated. |
||
|
Role to use when the function is called. Only included if Can be a built-in role, |
|||
|
true |
FQL function body. |
||
|
Arbitrary user-defined metadata for the document. |
Static methods
You can use the following static methods to manage the Function
collection in FQL.
Method | Description |
---|---|
Call a user-defined function (UDF) by its name. |
|
Get a Set of all user-defined functions (UDFs). |
|
Get a user-defined function (UDF) by its name. |
|
Create a user-defined function (UDF). |
|
Get the first user-defined function (UDF) that matches a provided predicate. |
|
Get |
|
Get a Set of user-defined functions (UDFs) that match a provided predicate. |
Instance properties
Function
documents have the following properties. You
access the property using an existing UDF’s name.
Property | Description |
---|---|
Get or update a user-defined
function (UDF)'s definition, represented as a
|
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 user-defined function (UDF). |
|
Test if a user-defined function (UDF) exists. |
|
Replace a user-defined function (UDF). |
|
Update a user-defined function (UDF). |
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!