SelectPromise<T, I, J>The SelectPromise class provides a chainable interface for configuring SELECT queries before execution. It extends Promise, allowing you to await it directly or chain configuration methods.
Returned by: SurrealQueryable.select()
Source: query/select.ts
T - The result typeI - The input type for field selectionJ - Boolean indicating if result is JSON (default: false).fields()Specify which fields to select from the records.
Method SyntaxselectPromise.fields(...fields)
| Parameter | Type | Description |
|---|---|---|
fields required | Field<I>[] | Field names to select. |
SelectPromise<T, I, J> - Chainable promise
Select Specific Fieldsconst users = await db.select(new Table('users')) .fields('name', 'email', 'age'); // Returns: [{ name, email, age }, ...]
Select Nested Fieldsconst users = await db.select(new Table('users')) .fields('name', 'address.city', 'address.country');
Select with Aggregationsconst stats = await db.select(new Table('orders')) .fields('count()', 'sum(total)', 'avg(items)');
.value()Select only the value of a specific field, unwrapping it from the record structure.
Method SyntaxselectPromise.value(field)
| Parameter | Type | Description |
|---|---|---|
field required | Field<I> | The field name to extract. |
SelectPromise<T, I, J> - Chainable promise
Get Array of Valuesconst names = await db.select(new Table('users')) .value('name'); // Returns: ['John', 'Jane', 'Bob'] // Instead of: [{ name: 'John' }, { name: 'Jane' }, ...]
.where()Add a WHERE clause to filter results.
Method SyntaxselectPromise.where(condition)
| Parameter | Type | Description |
|---|---|---|
condition required | ExprLike | The condition expression (string or Expression object). |
SelectPromise<T, I, J> - Chainable promise
String Conditionconst adults = await db.select(new Table('users')) .where('age >= 18');
With Expression Builderimport { expr } from 'surrealdb'; const activeUsers = await db.select(new Table('users')) .where(expr(({ and, eq, gte, field }) => and( eq(field('status'), 'active'), gte(field('last_login'), new DateTime('2024-01-01')) ) ));
Parameterized Conditionconst users = await db.query( surql`SELECT * FROM users WHERE age >= ${minAge}` ).collect();
.fetch()Specify related fields to fetch (similar to SQL JOIN).
Method SyntaxselectPromise.fetch(...fields)
| Parameter | Type | Description |
|---|---|---|
fields required | string[] | Field names representing relations to fetch. |
SelectPromise<T, I, J> - Chainable promise
Fetch Related Recordsconst posts = await db.select(new Table('posts')) .fetch('author'); // Expands author RecordId to full user object
Fetch Multiple Relationsconst posts = await db.select(new Table('posts')) .fetch('author', 'comments', 'tags');
Fetch Nested Relationsconst posts = await db.select(new Table('posts')) .fetch('author', 'comments.author');
.start()Set the pagination offset (number of records to skip).
Method SyntaxselectPromise.start(offset)
| Parameter | Type | Description |
|---|---|---|
offset required | number | Number of records to skip. |
SelectPromise<T, I, J> - Chainable promise
Paginationconst page = 2; const pageSize = 10; const users = await db.select(new Table('users')) .start((page - 1) * pageSize) .limit(pageSize);
.limit()Limit the number of results returned.
Method SyntaxselectPromise.limit(count)
| Parameter | Type | Description |
|---|---|---|
count required | number | Maximum number of records to return. |
SelectPromise<T, I, J> - Chainable promise
Get Top 10const topUsers = await db.select(new Table('users')) .where('score > 0') .limit(10);
.timeout()Set a timeout for the query operation.
Method SyntaxselectPromise.timeout(duration)
| Parameter | Type | Description |
|---|---|---|
duration required | Duration | Maximum time to wait for query completion. |
SelectPromise<T, I, J> - Chainable promise
const users = await db.select(new Table('users')) .timeout(Duration.parse('5s'));
.version()Select records at a specific version/timestamp (time-travel queries).
Method SyntaxselectPromise.version(timestamp)
| Parameter | Type | Description |
|---|---|---|
timestamp required | DateTime | The timestamp to query at. |
SelectPromise<T, I, J> - Chainable promise
Query Historical Dataconst historicalUsers = await db.select(new Table('users')) .version(DateTime.parse('2024-01-01T00:00:00Z'));
.json()Return results as JSON strings instead of parsed objects.
Method SyntaxselectPromise.json()
SelectPromise<T, I, true> - Promise returning JSON string
const jsonString = await db.select(new Table('users')).json(); console.log(typeof jsonString); // 'string'
.stream()Stream results as they arrive instead of waiting for all results.
Method SyntaxselectPromise.stream()
AsyncIterableIterator - Async iterator for streaming results
for await (const user of db.select(new Table('users')).stream()) { console.log('Received user:', user); }
import { Surreal, Table } from 'surrealdb'; const db = new Surreal(); await db.connect('ws://localhost:8000'); // Select all const allUsers = await db.select(new Table('users')); // Select specific record const user = await db.select(new RecordId('users', 'john'));
const activeAdults = await db.select(new Table('users')) .where('age >= 18 AND status = "active"') .fields('name', 'email', 'age');
function getPage(page: number, pageSize: number) { return db.select(new Table('users')) .start((page - 1) * pageSize) .limit(pageSize); } const page1 = await getPage(1, 20); const page2 = await getPage(2, 20);
const posts = await db.select(new Table('posts')) .where('published = true') .fields('title', 'content', 'author', 'created_at') .fetch('author', 'comments.author') .limit(10); // posts[0].author is now a full User object // posts[0].comments[0].author is also expanded
import { expr, gte } from 'surrealdb'; const stats = await db.select(new Table('orders')) .where(expr(gte('created_at', DateTime.parse('2024-01-01')))) .fields('count() as total_orders', 'sum(amount) as total_revenue', 'avg(amount) as avg_order');
let count = 0; for await (const user of db.select(new Table('users')).stream()) { await processUser(user); count++; if (count % 100 === 0) { console.log(`Processed ${count} users`); } }
All configuration methods return a new SelectPromise, allowing you to chain them in any order:
const result = await db.select(new Table('users')) .where('status = "active"') .fields('name', 'email') .fetch('profile') .start(0) .limit(10) .timeout(Duration.parse('5s'));