• Start

SurrealQL

/

Executing queries

Via SDKs

Query SurrealDB programmatically using one of the official SDKs available for Rust, JavaScript, Python, Go, Java, .NET, and PHP.

SurrealDB supports a number of methods for connecting to the database and performing data queries. Each SDK has its own set of methods for connecting to the database and performing data queries.

In each SDK, you can connect to the database using a local or remote connection. Once you are connected, you can start performing data queries. Here is a list of all the Supported SDKs:

In addition to the variety of methods provided by the SDKs to perform data queries, the query method works as a catch-all way to run SurrealQL statements against the database.

Method Syntax

async db.query<T>(query, vars)
ArgumentsDescription
query Specifies the SurrealQL statements.
vars Assigns variables which can be used in the query.
type Person = {
id: string;
name: string;
};

// Assign the variable on the connection
const result = await db.query<[Person[], Person[]]>(
'CREATE person SET name = "John"; SELECT * FROM type::table($tb);',
{ tb: 'person' }
);

// Get the first result from the first query
const created = result[0].result[0];

// Get all of the results from the second query
const people = result[1].result;

.query_raw()

With .query_raw(), you will get back the raw RPC response. In contrast to the .query() method, this will not throw errors that occur in individual queries, but will rather give those back as a string, and this will include the time it took to execute the individual queries.

Learn more about the SurrealQL query language.

Was this page helpful?