DEFINE FUNCTION statement
The DEFINE FUNCTION statement allows you to define custom functions that can be reused throughout a database. When using the DEFINE FUNCTION statement, you can define a function that takes one or more arguments and returns a value. You can then call this function in other SurrealQL statements.
Functions can be used to encapsulate logic that you want to reuse in multiple queries. They can also be used to simplify complex queries by breaking them down into smaller, more manageable pieces. The are particularly useful when you have a complex query that you need to run multiple times with different arguments.
Requirements
You must be authenticated as a root owner or editor, namespace owner or editor, or database owner or editor before you can use the
DEFINE FUNCTIONstatement.You must select your namespace and database before you can use the
DEFINE FUNCTIONstatement.
Statement syntax
Example usage
Below shows how you can define a custom function using the DEFINE FUNCTION statement, and how to call it.
To showcase a slightly more complex custom function, this will check if a relation between two nodes exists:
Optional arguments
If one or more ending arguments have the option<T> type, they can be omitted when you run the invoke the function.
Adding a return value
Optionally, the return value of a function can be specified.
For a function that is infallible, a return value is mostly for the sake of readability.
For a function that is not infallible, specifying a return value can be used to customise error output.
While both of these return an error, the output of the second function happens only at the point that it attempts to return the combined arguments to the function.
The return value of a function can even be a literal type. The following function returns such a type by either returning an object of a certain structure, or a string. In this case this output is used in case an application prefers to return an error as a simple string instead of throwing an error or returning a NONE value.
Recursive functions
A function is able to call itself, making it a recursive function. One example of a recursive function is the one below which creates a relation between each and every record passed in.
Consider a situation in which seven person records exist. First, person:1 will need to be related to the rest of the person records, after which there are no more relations to create for it. Following this, the relations for person:2 and all the other records except for person:1 will need to be created, and so on.
This can be done in a recursive function by creating all the relations between the first record and the remaining records, after which the function calls itself by passing in all the records except the first. This continues until the function receives less than two records, in which case it ceases calling itself by doing nothing, thereby ending the recursion.
The last query can be viewed graphically inside Surrealist, leading to an output showing a seven-pointed star.

Permissions
You can set the permissions for a custom function using the PERMISSIONS clause. The PERMISSIONS clause is mostly used to restrict who can access a function and what data they can access. It can be set to NONE, FULL, or WHERE @condition.
FULL: When Full permissions are granted record users have access to the function. This is the default permission when not specified.NONE: When this permission is granted, record users have no access to the defined function.WHERE @condition: Permissions are granted to the function based on the specified condition.
Note
Using the FULL permission
The FULL permission grants all users access to the function. The following example defines a function that fetches all products from the product table and grants the function full permissions to access the data to all users.
Using the NONE permission
The NONE permission denies all record users access to the function. The following example defines a function that fetches all products from the product table
Using the WHERE clause
The WHERE clause allows you to specify a condition that determines the permissions granted to the function. The condition must evaluate to a boolean value. If the condition evaluates to true, the function is granted permissions. If the condition evaluates to false, the function is not granted permissions.
Using IF NOT EXISTS clause
The IF NOT EXISTS clause can be used to define a function only if it does not already exist. You should use the IF NOT EXISTS clause when defining a function in SurrealDB if you want to ensure that the function is only created if it does not already exist. If the function already exists, the DEFINE FUNCTION statement will return an error.
It's particularly useful when you want to safely attempt to define a function without manually checking its existence first.
On the other hand, you should not use the IF NOT EXISTS clause when you want to ensure that the function definition is updated regardless of whether it already exists. In such cases, you might prefer using the OVERWRITE clause, which allows you to define a function and overwrite an existing one if it already exists, ensuring that the latest version of the function definition is always in use
Using OVERWRITE clause
The OVERWRITE clause can be used to define a function and overwrite an existing one if it already exists. You should use the OVERWRITE clause when you want to modify an existing user definition. If the user already exists, the DEFINE FUNCTION statement will overwrite the existing definition with the new one.
Functions as custom middleware
A DEFINE FUNCTION statement can be used to define a function for use as custom middleware. For more details on defining a custom function in this manner, see the DEFINE API page.