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:
While you can use the other methods provided by the SDKs to perform data queries, you can use the query
method to run SurrealQL statements against the database.
Method Syntaxasync db.query<T>(query, vars)
Arguments | Description | ||
---|---|---|---|
query required | Specifies the SurrealQL statements. | ||
vars optional | 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.
Method Syntax$db->query($query, $vars)
Arguments | Description | ||
---|---|---|---|
$query required | Specifies the SurrealQL statements. | ||
$vars optional | Assigns variables which can be used in the query. |
// 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 Syntaxdb.query(sql, vars)
Arguments | Description | ||
---|---|---|---|
sql required | Specifies the SurrealQL statements. | ||
vars optional | Assigns variables which can be used in the query. |
# Assign the variable on the connection result = await db.query('CREATE person; SELECT * FROM type::table($tb)', { 'tb': 'person', }) # Get the first result from the first query result[0]['result'][0] # Get all of the results from the second query result[1]['result']
Method Syntaxawait db.Query(sql)
Arguments | Description | ||
---|---|---|---|
sql required | Specifies the SurrealQL statements. | ||
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
// Execute query with params const string table = "person"; var result = await db.Query($"CREATE person; SELECT * FROM type::table({table});"); // Get the first result from the first query var created = result.GetValue<Person>(0); // Get all of the results from the second query var people = result.GetValue<List<Person>>(1);
.RawQuery()
: Runs a set of SurrealQL statements against the database, based on a raw SurrealQL query.
Method Syntaxawait db.RawQuery(sql, params)
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. |
// Assign the variable on the connection var @params = new Dictionary<string, object> { { "table", "person" } }; var result = await db.RawQuery("CREATE person; SELECT * FROM type::table($table);", @params); // Get the first result from the first query var created = result.GetValue<Person>(0); // Get all of the results from the second query var people = result.GetValue<List<Person>>(1);
Method Syntaxdb.Query(sql, vars)
Arguments | Description | ||
---|---|---|---|
sql required | Specifies the SurrealQL statements. | ||
vars optional | Assigns variables which can be used in the query. |
// Assign the variable on the connection result, err := db.Query("CREATE person; SELECT * FROM type::table($tb);", map[string]string{ "tb": "person" });
Method Syntaxdb.query(sql).bind(vars)
Arguments | Description | ||
---|---|---|---|
sql required | Specifies the SurrealQL statements. | ||
vars optional | Assigns variables which can be used in the query. |
// Run some queries let sql = " CREATE person; SELECT * FROM type::table($table); "; let mut result = db .query(sql) .bind(("table", "person")) .await?; // Get the first result from the first query let created: Option<Person> = result.take(0)?; // Get all of the results from the second query let people: Vec<Person> = result.take(1)?;
Learn more about the SurrealQL query language.