Database functions are SurrealQL's built-in, namespaced helpers (string::split(), math::mean(), time::now(), and so on). They run inside the database engine and are the usual choice for everyday querying and data shaping.
SurrealQL also supports other kinds of callable logic:
JavaScript functions — embedded scripts when the JavaScript runtime is enabled; see the scripting docs for definitions, context, and limits.
SurrealML functions — helpers used with SurrealML.
Several function families transform values across representation boundaries (JSON, CBOR, tokens, runtime query strings). See Representations and codecs for when to use each.
The table below lists all of SurrealDB's function modules, grouped by purpose and data type, with short examples and links to detailed documentation.
Function | Description and Example |
|---|---|
API | These functions can be used to add middleware to a defined API endpoint. Example: api::timeout(1s) |
Array | These functions can be used when working with, and Example: array::len([1,2,3]) |
Bytes | These functions can be used when working with bytes in Example: bytes::len("SurrealDB".to_bytes()); |
Count | This function can be used when counting field values and Example: count([1,2,3]) |
Crypto | These functions can be used when hashing data, encrypting Example: crypto::argon2::generate("MyPaSSw0RD") |
Duration | Funcions and constants for converting between numeric values Example: duration::days(90h30m) |
Encoding | Encode and decode values as JSON, CBOR, or Base64. encoding::cbor::encode({'foo': 'bar'}) |
Eval | Evaluate a SurrealQL or ISO GQL query string at runtime inside the eval::surql("RETURN 1 + 1") |
Files | These functions can be used to work with files. f"my_bucket:/my_book.txt".get() |
Geo | These functions can be used when working with and analysing Example:{' '} geo::distance((-0.04, 51.55), (30.46, -17.86)) |
HTTP | These functions can be used when opening and submitting Example: |
Math | Functions and constants for Example:{' '}
math::max([ 26.164, 13.746189, 23, 16.4, 41.42 ])
|
Not | This function reverses the truthiness of a value. Example: not(true) |
Object | These functions can be used when working with, and Example:{' '}
object::from_entries([[ "a", 1 ],[ "b", true ]])
|
Parse | These functions can be used when parsing email addresses and Example:{' '}
parse::url::domain("http://127.0.0.1/index.html")
|
Rand | These functions can be used when generating random data Example:{' '}
rand::enum('one', 'two', 3, 4.15385, 'five', true)
|
Record | These functions can be used to retrieve specific metadata Example: record::id(person:tobie) |
Search | These functions are used in conjunction with the{' '} @@operator (the 'matches' operator) to either Example:{' '}
SELECT search::score(1) AS score FROM book WHERE title
@1@ 'rust web'
|
Sequence | These functions can be used to work with a defined sequence. Example: sequence::nextval('mySeq2') |
Session | These functions return information about the current Example: session::db() |
Set | These functions can be used when working with, and Example: `set::len({1,2,3})` |
Sleep | This function can be used to introduce a delay or pause in Example: sleep(900ms) |
String | These functions can be used when working with and Example:{' '} string::reverse('emosewa si 0.2 BDlaerruS') |
Time | Functions and constants for Example: time::timezone() |
Type | These functions can be used for generating and coercing data Example: type::is_number(500) |
Value | This module contains several miscellaneous functions that Example:{' '} value::diff([true, false], [true, true]) |
Vector | A collection of essential vector operations that provide Example: vector::add([1, 2, 3], [1, 2, 3]) |
How to use database functions
Classic syntax
Functions in SurrealDB can always be called using their full path names beginning with the package names indicated above, followed by the function arguments.
string::split("SurrealDB 3.0 is now here!", " ");
array::len([1,2,3]);
type::is_number(10);
type::record("cat", "mr_meow");-------- Query --------
[
'SurrealDB',
'3.0',
'is',
'now',
'here!'
]
-------- Query --------
3
-------- Query --------
true
-------- Query --------
cat:mr_meowMethod syntax
Functions that are called on an existing value can be called using method syntax, using the . (dot) operator.
The following functions will produce the same output as the classic syntax above. type::record() cannot be called with method syntax because it is used to outright create a record ID from nothing, rather than being called on an existing value.
"SurrealDB 3.1 is now here!".split(" ");
[1,2,3].len();
10.is_number();The method syntax is particularly useful when calling a number of functions inside a single query.
array::len(array::windows(array::distinct(array::flatten([[1,2,3],[1,4,6],[4,2,4]])), 2));Without method chaining, a query of this type is often written across multiple nested lines:
array::len(
array::clump(
array::distinct(
array::flatten([[1,2,3],[1,4,6],[4,2,4]])
)
, 2)
);However, method chaining syntax allows queries of this type to be read from left to right in a functional manner. This is known as method chaining. As each of the methods below except the last return an array, further array methods can thus be called by using the . operator. The final method then returns an integer.
[[1,2,3],[1,4,6],[4,2,4],2].flatten().distinct().windows(2).len();This can be made even more readable by splitting over multiple lines.
[[1,2,3],[1,4,6],[4,2,4]]
.flatten()
.distinct()
.windows(2)
.len(); Conversion from :: (double colon) to _ (underscore) syntax
Full function paths in SurrealDB were converted to match the method syntax detailed above.
-- Old syntax
type::is::record(person:one);
-- Method syntax
person:one.is_record();
-- New syntax now matches method syntax
type::is_record(person:one);Built-in constants
Some modules expose constants (fixed values) as well as functions. Consts use the same module::name path syntax as for functions, but omit parentheses because they access direct values instead of a function to be called.
Math — numeric constants (π, e, τ, infinities, and related values).
Time —
time::epoch,time::minimum, andtime::maximum.Duration —
duration::max.
RETURN [math::pi, math::tau, math::e];[
3.141592653589793f,
6.283185307179586f,
2.718281828459045f
]Aggregate functions
A few functions can be used not just on their own but with a GROUP BY clause including as part of a pre-computed table view.
These functions are:
Anonymous functions
SurrealDB also allows for the creation of anonymous functions (also known as closures) that do not need to be defined on the database. See the page on closures for more details.
Extensions
You can also write your own functions in Rust that can be compiled to WASM modules, linked to, and called from the database. For more on how extensions are built and run, see Extensions.