Executing queries
The JavaScript SDK provides two ways to execute queries against SurrealDB: raw SurrealQL using the .query() method, and structured query builder methods like .select(), .create(), .update(), and .delete(). Both approaches support type-safe generics, chainable configuration, and multiple result formats.
API References
| Method | Description |
|---|---|
db.query(query, bindings?) | Executes raw SurrealQL statements |
db.select(target) | Selects records from the database |
db.create(target) | Creates new records |
db.insert(target, data) | Inserts one or multiple records |
db.update(target) | Updates existing records |
db.upsert(target) | Inserts or replaces records |
db.delete(target) | Deletes records from the database |
db.relate(from, edge, to) | Creates graph relationships between records |
db.run(name, args?) | Executes a SurrealDB function or SurrealML model |
db.set(key, value) | Defines a parameter on the session |
db.unset(key) | Removes a parameter from the session |
Running raw SurrealQL
The .query() method executes raw SurrealQL statements against the database. You can pass bindings as a second argument to safely inject variables into the query.
When executing multiple statements, each result maps to a position in the generic type parameter.
You can also pass a bound query for automatic parameterization using the surql template tag.
Selecting records
The .select() method reads records from the database. You can pass a Table to select all records, a RecordId to select a specific record, or a RecordIdRange to select a range.
Query builder methods return chainable promises, allowing you to configure the query before it executes.
In the above example, we use the gt() function to create a greater than condition. This function is part of the expression utilities that are available in the SDK.
If you prefer to write raw condition, you can use the raw() function to insert the condition directly into the query.
Creating records
The .create() method creates new records. Use .content() to set the record data. When passed a Table, SurrealDB generates a random ID. When passed a RecordId, the record is created with that specific ID.
Inserting records
The .insert() method inserts one or multiple records at once. This is more efficient than calling .create() in a loop when working with bulk data.
Updating records
The .update() and .upsert() methods modify existing records. Instead of passing content as a second argument, you choose an update strategy by chaining .content(), .merge(), .replace(), or .patch().
Replace the entire record with new data. Any existing fields not included in the new data will be removed.
You can also filter which records to update using .where().
Deleting records
The .delete() method removes records from the database. Like other query methods, it accepts a Table, RecordId, or RecordIdRange.
Creating graph relationships
The .relate() method creates edges between records in SurrealDB's graph model. You specify the source record(s), the edge table, and the target record(s).
Running functions
The .run() method executes SurrealDB functions or SurrealML models by name. You can pass arguments and optionally specify a model version.
Setting session parameters
You can define parameters on the current session using .set() and remove them with .unset(). Session parameters are available in all subsequent queries as $name variables and persist for the lifetime of the session.
Collecting specific results
When running multi-statement queries, you can use .collect() to pick specific result indexes rather than receiving all results.
Streaming query responses
The .stream() method returns an async iterable of response frames, allowing you to process results incrementally. Each frame is either a value, a completion signal with query stats, or an error.
Note
Serializing results
The .json() method converts SurrealDB value types in query results to their JSON representations. This is useful when you need to serialize results for APIs or tools that don't understand SurrealDB's custom types.
Learn more about the jsonify utility in the Utilities concept page.
Accessing response metadata
The .responses() method returns the full response objects including success status, query stats, and error information for each statement.
Learn more
Bound queries for parameterized query building with
surqlandBoundQueryLive queries for real-time subscriptions
Transactions for atomic multi-query operations
Query Builders API reference for detailed method signatures
SurrealQL statements for the query language reference