The methods below are used to interact with the database and perform CRUD operations. You can also use the query
method to run SurrealQL statements against the database.
Method | Description |
---|---|
db.query() | Runs a set of SurrealQL statements against the database |
db.query_raw() | Runs a set of SurrealQL statements against the database and returns the raw RPC response |
.query()
Runs a set of 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. This contrast to the .query()
method, this will not throw for 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.
The JavaScript SDK also provides easy to use methods for data selection and altering.
Table
vs RecordId
These methods all accept either a Table
vs RecordId
vs StringRecordId
, or a “thing”, as their first argument. Passing a Table
instance, or a string
, will make the method return an array of objects of generic type T
. If you instead pass a RecordId
or StringRecordId
instance, the method will return a single object of generic type T
back.