Querying via SDKs
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:
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
async db.query<T>(query, vars)
Arguments
| Arguments | Description |
|---|
query required | Specifies the SurrealQL statements. |
vars optional | Assigns variables which can be used in the query. |
Example usage
type Person = {
id: string;
name: string;
};
const result = await db.query<[Person[], Person[]]>(
'CREATE person SET name = "John"; SELECT * FROM type::table($tb);',
{ tb: 'person' }
);
const created = result[0].result[0];
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.
Method Syntax
$db->query($query, $vars)
Arguments
| Arguments | Description |
|---|
$query required | Specifies the SurrealQL statements. |
$vars optional | Assigns variables which can be used in the query. |
Example usage
$result = db->query(
'CREATE person SET name = "John"; SELECT * FROM type::table($tb);',
[ "tb" => "person" ]
);
$created = $result[0]->result[0];
$people = $result[1]->result;
Method Syntax
db.query(sql, vars)
Arguments
| Arguments | Description |
|---|
sql required | Specifies the SurrealQL statements. |
vars optional | Assigns variables which can be used in the query. |
Example usage
result = await db.query('CREATE person; SELECT * FROM type::table($tb)', {
'tb': 'person',
})
result[0]['result'][0]
result[1]['result']
Method Syntax
await db.Query(sql)
Arguments
| Arguments | Description |
|---|
sql required | Specifies the SurrealQL statements. |
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
const string table = "person";
var result = await db.Query($"CREATE person; SELECT * FROM type::table({table});");
var created = result.GetValue<Person>(0);
var people = result.GetValue<List<Person>>(1);
.RawQuery() : Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query.
Method Syntax
await db.RawQuery(sql, params)
Arguments
| Arguments | Description |
|---|
sql required | Specifies the SurrealQL statements. |
params optional | Assigns variables which can be used in the query. |
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
var @params = new Dictionary<string, object> { { "table", "person" } };
var result = await db.RawQuery("CREATE person; SELECT * FROM type::table($table);", @params);
var created = result.GetValue<Person>(0);
var people = result.GetValue<List<Person>>(1);
Method Syntax
db.Query(sql, vars)
Arguments
| Arguments | Description |
|---|
sql required | Specifies the SurrealQL statements. |
vars optional | Assigns variables which can be used in the query. |
Example usage
result, err := db.Query("CREATE person; SELECT * FROM type::table($tb);", map[string]string{
"tb": "person"
});
Method Syntax
db.query(sql).bind(vars)
Arguments
| Arguments | Description |
|---|
sql required | Specifies the SurrealQL statements. |
vars optional | Assigns variables which can be used in the query. |
Example usage
let sql = "
CREATE person;
SELECT * FROM type::table($table);
";
let mut result = db
.query(sql)
.bind(("table", "person"))
.await?;
let created: Option<Person> = result.take(0)?;
let people: Vec<Person> = result.take(1)?;
Learn more
Learn more about the SurrealQL query language.