Besides basic JavaScript utilities and classes for SurrealQL types, there are a handful of utilities built into the embedded scripting functions.
Function | Description |
|---|---|
async fetch(resource, options) | Full fledged fetch implementation closely matching the official specification . |
async query(surql) | Run SurrealQL subqueries from within the embedded scripting functions. |
async value(variable) | Retrieve values for SurrealQL variables from within the embedded scripting functions. |
async fetch(resource, options)
Full fledged fetch implementation closely matching the official specification.
For complete documentation, please refer to the MDN documentation.
Arguments | Description |
|---|---|
resource | Accepts either a url in a string, or a URL or Request object. |
options | Accepts various options related to the request. Refer to MDN docs for a full reference. |
function() {
// List all posts
const posts = fetch('https://jsonplaceholder.typicode.com/posts');
// Update post with ID 1
const updated = fetch('https://jsonplaceholder.typicode.com/posts/1',
{
method: 'PUT',
body: JSON.stringify({
id: 1,
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
});
return { posts, updated };
} async query(surql)
Run SurrealQL subqueries from within the embedded scripting functions.
Only subqueries can be executed with the query() function. This means that only a single query can currently be executed, and that only CRUD operations are allowed.
Arguments | Description |
|---|---|
surql | Accepts a single SurrealQL query, which is limited to a CRUD operation. |
CREATE user:john, user:mary;
RETURN function() {
// Select all users
const users = await surrealdb.query("SELECT * FROM user");
// Prepared query
const query = new surrealdb.Query("SELECT * FROM $id", {
id: new Record('user', 'mary')
});
// Execute prepared query
const mary = (await surrealdb.query(query))[0];
// Assign variables later to prepared query
query.bind('id', new Record('user', 'john'));
// Execute prepared query
const john = (await surrealdb.query(query))[0];
return { john, mary };
} async value(variable)
Retrieve values for SurrealQL variables from within the embedded scripting functions.
Arguments | Description |
|---|---|
variable | Accepts the path to a variable |
LET $something = 123;
LET $obj = {
nested: 456
};
LET $arr = [
{ value: 1 },
{ value: 2 },
{ value: 3 },
{ value: 4 },
{ value: 5 },
{ value: 6 },
];
RETURN function() {
// Get the value for a variable
const something = await surrealdb.value("$something");
// Get the value for a nested property
const nested = await surrealdb.value("$obj.nested");
// Filter properties from an array
const fromArray = await surrealdb.value("$arr[WHERE value >
3].value");
return { something, nested, fromArray };
}{
fromArray: [
4,
5,
6
],
nested: 456,
something: 123
} console
The console object writes to the server log, which is the output of the surreal start process. Each method logs at the matching level, and the arguments are converted to strings and joined with spaces.
| Method | Log level |
|---|---|
console.log(), console.info() | info |
console.warn() | warn |
console.error() | error |
console.debug() | debug |
console.trace() | trace |
The default log level is info, so console.debug() and console.trace() produce output only when the server is started with a more verbose --log level. Nothing is returned to the client, and the output does not appear in the result of the query.
CREATE x SET v = function() {
console.log('hello from js');
console.warn('warned');
console.debug('not shown at the default level');
return 1;
};INFO surrealdb_core::fnc::script::globals::console: hello from js
WARN surrealdb_core::fnc::script::globals::console: warned