Each SurrealDB 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. The supported SDKs are:
For other languages and runtimes, see the community SDKs, which are maintained outside SurrealDB.
Writing SurrealQL queries in 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
asyncdb.query<T>(query,vars)
Arguments
Arguments
Description
query
Specifies the SurrealQL statements.
vars
Assigns variables which can be used in the query.
Example usage
typePerson={id:string;name:string;};// Assign the variable on the connectionconstresult=awaitdb.query<[Person[],Person[]]>('CREATE person SET name = "John"; SELECT * FROM type::table($tb);',{tb:'person'});// Get the first result from the first queryconstcreated=result[0].result[0];// Get all of the results from the second queryconstpeople=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.
Method Syntax
$db->query($query,$vars)
Arguments
Arguments
Description
$query
Specifies the SurrealQL statements.
$vars
Assigns variables which can be used in the query.
Example usage
// Assign the variable on the connection$result=db->query('CREATE person SET name = "John"; SELECT * FROM type::table($tb);',["tb"=>"person"]);// Get the first result from the first query$created=$result[0]->result[0];// Get all of the results from the second query$people=$result[1]->result;
Method Syntax
db.query(sql,vars)
Arguments
Arguments
Description
sql
Specifies the SurrealQL statements.
vars
Assigns variables which can be used in the query.
Example usage
# Assign the variable on the connectionresult=awaitdb.query('CREATE person; SELECT * FROM type::table($tb)',{'tb': 'person',})# Get the first result from the first queryresult[0]['result'][0]# Get all of the results from the second queryresult[1]['result']
Method Syntax
awaitdb.Query(sql)
Arguments
Arguments
Description
sql
Specifies the SurrealQL statements.
cancellationToken
The cancellationToken enables graceful cancellation of asynchronous operations.
Example usage
// Execute query with paramsconststringtable="person";varresult=awaitdb.Query($"CREATE person; SELECT * FROM type::table({table});");// Get the first result from the first queryvarcreated=result.GetValue<Person>(0);// Get all of the results from the second queryvarpeople=result.GetValue<List<Person>>(1);
.RawQuery() : Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query.
Method Syntax
awaitdb.RawQuery(sql, params)
Arguments
Arguments
Description
sql
Specifies the SurrealQL statements.
params
Assigns variables which can be used in the query.
cancellationToken
The cancellationToken enables graceful cancellation of asynchronous operations.
Example usage
// Assign the variable on the connectionvar @params =newDictionary<string, object> { { "table", "person" } };varresult=awaitdb.RawQuery("CREATE person; SELECT * FROM type::table($table);", @params);// Get the first result from the first queryvarcreated=result.GetValue<Person>(0);// Get all of the results from the second queryvarpeople=result.GetValue<List<Person>>(1);
Method syntax
surrealdb.Query[T](ctx,db,sql,vars)
Arguments
Arguments
Description
T
The Go type of each statement's result.
sql
Specifies the SurrealQL statements.
vars
Assigns variables which can be used in the query. Pass
nil
for none.
Example usage
results,err:=surrealdb.Query[[]any](ctx,db,"CREATE person; SELECT * FROM type::table($tb);",map[string]any{"tb":"person",})iferr!=nil{log.Fatal(err)}// Get all of the results from the second querypeople:=(*results)[1].Result
Method Syntax
db.query(sql).bind(vars)
Arguments
Arguments
Description
sql
Specifies the SurrealQL statements.
vars
Assigns variables which can be used in the query.
Example usage
// Run some queriesletsql=" CREATE person; SELECT * FROM type::table($table);";letmutresult=db .query(sql) .bind(("table","person")) .await?;// Get the first result from the first queryletcreated: Option<Person> =result.take(0)?;// Get all of the results from the second queryletpeople: Vec<Person> =result.take(1)?;