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. |
||
|
Associates a runtime role with the UDF. An
FQL version of the By default, UDFs run with the privileges of the calling query’s authentication
secret. If a The Role can be a user-defined role or one of the following built-in roles:
The Use |
|||
|
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). |