• Start

Functions

/

Database functions

Record

These functions can be used to retrieve specific metadata from a SurrealDB Record ID.

These functions can be used to retrieve specific metadata from a SurrealDB Record ID.

Function

Description

record::exists()

Checks to see if a SurrealDB Record ID exists

record::id()

Extracts and returns the identifier from a SurrealDB Record ID

record::tb()

Extracts and returns the table name from a SurrealDB Record ID

record::refs()

Extracts and returns the record IDs of any records that have a record link along with a REFERENCES clause

record::is_edge()

Identifies whether the value passed in is a graph edge

The record::exists function checks to see if a given record exists.

API DEFINITION
record::exists(record) -> bool

A simple example showing the output of this function when a record does not exist and when it does:

/**[test]

[[test.results]]
value = "false"

[[test.results]]
value = "[{ id: person:tobie }]"

[[test.results]]
value = "true"

*/

RETURN record::exists(r"person:tobie");
-- false

CREATE person:tobie;
RETURN record::exists(r"person:tobie");
-- true

A longer example of record::exists using method syntax:

/**[test]

[[test.results]]
value = "false"

*/

FOR $person IN ["Haakon_VII", "Ferdinand_I", "Manuel_II", "Wilhelm_II", "George_I", "Albert_I", "Alfonso_XIII", "George_V", "Frederick_VIII"] {
    LET $record_name = type::record("person", $person.lowercase());
    IF !$record_name.exists() {
        CREATE $record_name;
    }
}

The record::id function extracts and returns the identifier from a SurrealDB Record ID.

API DEFINITION
record::id(record) -> value

The following example shows this function, and its output, when used in a RETURN statement:

/**[test]

[[test.results]]
value = "'tobie'"

*/

RETURN record::id(person:tobie);

'tobie'

The record::tb function extracts and returns the table name from a SurrealDB Record ID.

API DEFINITION
record::tb(record) -> string

The following example shows this function, and its output, when used in a RETURN statement:

/**[test]

[[test.results]]
value = "'person'"

*/

RETURN record::tb(person:tobie);

-- 'person'



Available since: v3.0.0

The record::is_edge function checks to see if the value passed in is a graph edge.

API DEFINITION
record::is_edge(record | string) -> bool
/**[test]

[[test.results]]
value = "[{ id: likes:first_like, in: person:one, out: person:two }]"

[[test.results]]
value = "true"

[[test.results]]
value = "true"

*/

RELATE person:one->likes:first_like->person:two;

-- Both return true
record::is_edge(likes:first_like);
record::is_edge("likes:first_like");

Method chaining allows functions to be called using the . dot operator on a value of a certain type instead of the full path of the function followed by the value.

/**[test]

[[test.results]]
value = "'aeon'"

[[test.results]]
value = "'aeon'"

*/

-- Traditional syntax
record::id(r"person:aeon");

-- Method chaining syntax
r"person:aeon".id();
Response
'aeon'

This is particularly useful for readability when a function is called multiple times.

/**[test]

[[test.results]]
value = "'person'"

[[test.results]]
value = "'person'"

*/

-- Traditional syntax
record::table(array::max([r"person:aeon", r"person:landevin"]));

-- Method chaining syntax
[r"person:aeon", r"person:landevin"].max().table();
Response
'person'

Was this page helpful?