function
Define a user-defined Function (UDF) schema.
Syntax
[@role(<roleName>)]
[@alias(<aliasId>]
function <functionName> (<parameter>: <parameterType>): <returnType> {
<functionBody>
}
Annotations
This is the role to use when the UDF is called. This is typically used for privilege escalation when current privileges would otherwise be too restrictive. A function must declare a built-in role 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.
Use role carefully. Setting the role privilege gives the function permission to create, change, and remove documents when invoked by calling the function. A UDF can change a role to change function privileges.
Examples
Simple function schema definition:
function MyFunction(x: Number): Number {
x + 2
}
Function schema definition with @role
annotation:
@role(server)
function submitOrder(customer, productsRequested) {
if (productsRequested.length == 0) {
abort("Cart can't be empty")
}
Order.create({
customer: customer,
cart: shoppingCart,
status: "processing",
creationDate: Time.now(),
shipDate: null,
deliveryAddress: customer.address,
creditCard: customer.creditCard
}
)
}
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!