Write custom logic with UDFs
Fauna supports user-defined functions (UDFs) for encapsulating business logic as manageable and maintainable resources. UDFs are equivalent to SQL stored procedures.
A UDF is an anonymous function with the same calling syntax as the JavaScript arrow function expression.
See User-defined functions in the migration document for FQL v10 and FQL v4 UDF interoperability. |
Create a UDF and call it
This example UDF calculates the circumference of a circle.
-
Create the UDF by calling the Function
Function.create()
method, passing the name and a body of the UDF:{ name: "getCircumference", coll: Function, ts: Time("2023-06-03T18:17:04.285Z"), body: "radius => 2 * Math.PI * radius" }
-
Call your UDF by its name, passing a parameter:
75.39822368615503
-
To view the UDFs you defined, call
Function.all()
:{ data: [ { name: "getCircumference", coll: Function, ts: Time("2023-06-03T18:19:56.530Z"), body: "radius => 2 * Math.PI * radius" } ] }
Call a UDF from a built-in function
Many built-in methods, such as
where()
, accept a function
as input.
This exercise uses the where()
method to query your CoffeeBean
documents
to get the owner information of all owners growing the Robusta coffee bean
species.
-
First, create a UDF that takes declares a species parameter, searches the documents in your collection, and returns
Country_of_Origin
andOwner
fields of the matching documents:{ name: "getRobustaInfo", coll: Function, ts: Time("2023-06-03T18:40:17.390Z"), body: <<-END species => CoffeeBean.all() .where( .Species == species ) { Country_of_Origin, Owner } END }
Notice that the function body is saved as a heredoc string enclosed by
END
tags. -
Call your UDF with a parameter value of the coffee species you’re interested in:
{ data: [ { Country_of_Origin: "India", Owner: "nishant gurjer" } ] }
It should return only one document with the matching species.
-
Again, call the
Function.all()
method to view your UDFs:{ data: [ { name: "getCircumference", coll: Function, ts: Time("2023-06-03T18:19:56.530Z"), body: "radius => 2 * Math.PI * radius" }, { name: "getRobustaInfo", coll: Function, ts: Time("2023-06-03T18:40:17.390Z"), body: <<-END species => CoffeeBean.all() .where( .Species == species ) { Country_of_Origin, Owner } END } ] }
You should see the UDFs created in this exercise.
Delete a UDF
You can reference a UDF instance by the name you assigned it when you created
it. Use the
Function.byName()
method to get the UDF document and chain the Document
delete()
method to
delete the UDF:
Function.byName("getCircumference") /* not found */
The not found
string returned in the response indicates that the UDF
is successfully deleted, which you can verify by calling the
Function.all()
method.
An error message is returned if the requested UDF doesn’t exist or another
function depends on it.
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!