SurrealDB Docs Logo

Enter a search query

Encoding functions

These functions can be used to encode and decode data into other formats, such as base64 and CBOR (Concise Binary Object Representation). It is particularly used when that data needs to be stored and transferred over media that are designed to deal with text. This encoding and decoding helps to ensure that the data remains intact without modification during transport.

FunctionDescription
encoding::base64::decode()This function is used to decode data.
encoding::base64::encode()This function is used to encode data with optionally padded output.
encoding::base64::decode()This function is used to decode base64 data.
encoding::cbor::decode()This function is used to decode data.
encoding::cbor::encode()This function is used to decode data.

encoding::base64::encode()

The encoding::base64::encode() function encodes a bytes to base64 with optionally padded output.

encoding::base64::encode(bytes) -> string

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

RETURN encoding::base64::encode(<bytes>""); ""
RETURN encoding::base64::encode(<bytes>"2323"); -- 'MjMyMw'
RETURN encoding::base64::encode(<bytes>"hello"); -- 'aGVsbG8'

As of version 2.3.0, you can pass true as the second argument to enable padded base64 outputs:

RETURN encoding::base64::encode(<bytes>"", true); ""
RETURN encoding::base64::encode(<bytes>"2323", true); "MjMyMw=="
RETURN encoding::base64::encode(<bytes>"hello", true); "aGVsbG8="

encoding::base64::decode()

The encoding::base64::decode() function decodes a string into bytes.

API DEFINITION
encoding::base64::decode(string) -> bytes

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

RETURN encoding::base64::decode("MjMyMw"); -- encoding::base64::decode("MjMyMw")

You can also verify that the output of the encoded value matches the original value.

RETURN encoding::base64::decode("aGVsbG8") = <bytes>"hello"; -- true


encoding::cbor::decode()

Available since: v3.0.0-alpha.1

The encoding::cbor::decode() function decodes bytes in valid CBOR format into a SurrealQL value.

API DEFINITION
encoding::cbor::decode(string) -> any
LET $some_bytes = encoding::base64::decode("omRjYm9yaGVuY29kaW5nYmlza3ByZXR0eSBuZWF0");
encoding::cbor::decode($some_bytes);
Output
{ cbor: 'encoding', is: 'pretty neat' }


encoding::cbor::encode()

Available since: v3.0.0-alpha.1

The encoding::cbor::encode() function encodes any SurrealQL value into bytes in CBOR format.

API DEFINITION
encoding::cbor::encode(any) -> bytes
encoding::cbor::encode({ cbor: "encoding", is: "pretty neat" });
Output
encoding::base64::decode("omRjYm9yaGVuY29kaW5nYmlza3ByZXR0eSBuZWF0")