Query builder classes are promise-like objects returned by query methods on SurrealQueryable. They provide chainable configuration methods before executing the query.
All query builders implement a promise-like interface, allowing you to await them directly or chain configuration methods before execution.
// Chain configuration before awaiting const users = await db.select(new Table('users')) .where('age > 18') .limit(10) .start(0); // Or await directly const user = await db.select(new RecordId('users', 'john'));
SurrealQueryable.query().collect(), .stream(), .responses(), .json()SelectPromise - Configure SELECT queries with filtering, pagination, and field selection
SurrealQueryable.select().fields(), .value(), .where(), .fetch(), .start(), .limit()CreatePromise - Configure CREATE operations for new records
SurrealQueryable.create().content(), .patch(), .output()InsertPromise - Configure INSERT operations for bulk record insertion
SurrealQueryable.insert().relation(), .ignore(), .output()UpdatePromise - Configure UPDATE operations with various update strategies
SurrealQueryable.update().content(), .merge(), .replace(), .patch(), .where(), .output()UpsertPromise - Configure UPSERT operations (insert or replace)
SurrealQueryable.upsert().content(), .merge(), .patch(), .output()DeletePromise - Configure DELETE operations
SurrealQueryable.delete().output(), .timeout()SurrealQueryable.relate().unique(), .output(), .timeout()LivePromise - Subscribe to live query updates for real-time data changes
ManagedLivePromise, UnmanagedLivePromiseSurrealQueryable.live(), SurrealQueryable.liveOf().diff(), .fields(), .where(), .fetch()RunPromise - Execute SurrealDB functions and SurrealML models
SurrealQueryable.run().json()ApiPromise - Execute user-defined API endpoint calls
SurrealApi.value(), .header(), .json()Most query builders support these common configuration methods:
.json()Return results as JSON strings instead of parsed objects.
const jsonString = await db.select(new Table('users')).json();
.timeout(duration)Set a timeout for the query operation.
await db.select(new Table('users')) .timeout(new Duration('5s'));
.version(timestamp)Query records at a specific version/timestamp (time-travel queries).
await db.select(new Table('users')) .version(new DateTime('2024-01-01T00:00:00Z'));
Query builders use generic type parameters for type safety:
T - The base type of the recordI - The input type for operationsJ - The JSON string type when using .json()R - The result type (for Query class)