Any value from SurrealDB is converted into a JavaScript type automatically, and the return value from the JavaScript function is converted to a SurrealQL value. Boolean values, Integers, Floats, Strings, Arrays, Objects, and Date objects are all converted automatically to and from SurrealQL values.
CREATE user:test SET created_at = function() {
return new Date();
};[
{
created_at: d'2026-04-02T01:13:18.408Z',
id: user:test
}
]In addition, a number of special classes are included within the JavaScript functions for the additional types which are not built into JavaScript. These enable the creation of duration values, record ids, and UUID values from within JavaScript.
Any values of these types passed into embedded scripting functions are also represented with these special classes.
CREATE user:test SET
session_timeout = function() {
return new Duration('1w');
},
best_friend = function() {
return new Record('user', 'joanna');
},
identifier = function() {
return new Uuid('03412258-988f-47cd-82db-549902cdaffe');
}
;[
{
best_friend: user:joanna,
id: user:test1,
identifier: u'03412258-988f-47cd-82db-549902cdaffe',
session_timeout: 1w
}
]From SurrealDB 4.0, a range is passed into a function as an instance of the Range class, where earlier versions passed undefined. Its start and end properties hold the bounds, or undefined for a range with no bound on that side, and startInclusive and endInclusive say whether each bound is included. A Range returned from the function is converted back to a SurrealQL range, so returning the range that was passed in gives the same range, such as 1..3. new Range(start, end) creates a range that includes its start and excludes its end, and new Range(start, end, startInclusive, endInclusive) sets both. JavaScript has only one kind of number, so a bound read into JavaScript is a float: the bounds in the example below are 1f and 5f, and new Range(1, 5) returns 1f..5f.
RETURN function(1..=5) {
const [r] = arguments;
return [r.start, r.end, r.startInclusive, r.endInclusive];
};[1f, 5f, true, true]