Composables
useSurrealSelect
Select records from a table or record ID with SSR support.
useSurrealSelect
Select records from a SurrealDB table, record ID, or record ID range with a builder-pattern API and automatic SSR support. Results are automatically JSON-serialized.
Basic usage
app/pages/index.vue
<script setup lang="ts">
const { data } = await useSurrealSelect(new Table('users'))
</script>
With query builder
The composable accepts an optional builder callback for filtering, pagination, and field selection:
app/pages/index.vue
<script setup lang="ts">
const { data } = await useSurrealSelect(
new Table('users'),
q => q
.where(eq('active', true))
.limit(10)
.start(0),
)
</script>
Expression helpers like
eq, gt, contains, etc. are auto-imported when autoImportExpressions is enabled in your config.Select a single record
<script setup lang="ts">
const { data } = await useSurrealSelect(new RecordId('users', 'john'))
</script>
Reactive inputs
Since the first argument requires upstream SDK classes (Table, RecordId, RecordIdRange), standard ref() wrapping of the class won't make the parameters inside it reactive. Instead, use a getter to reconstruct the class when its parameters change:
app/pages/users.vue
<script setup lang="ts">
const id = ref<string>('tobie')
// Use a getter so that `useSurrealSelect` re-evaluates when `id` changes
const { data } = await useSurrealSelect(
() => new RecordId('users', id.value),
)
</script>
Passing
new RecordId('users', id.value) directly (without the getter) would break reactivity because the class is instantiated once and never re-evaluated when id changes.For non-reactive usage, pass the class directly:
const { data } = await useSurrealSelect(new Table('users'))
Builder methods
| Method | Description |
|---|---|
.fields(...fields) | Select specific fields |
.value(field) | Retrieve the value of a single field |
.where(expr) | Filter records using an expression |
.limit(n) | Limit the number of results |
.start(n) | Skip the first n results |
.fetch(...fields) | Fetch record link contents |
.timeout(duration) | Set query timeout |
.version(dateTime) | Query a specific version (SurrealKV) |
Arguments
| Argument | Type | Description |
|---|---|---|
tableOrRecord | MaybeRefOrGetter<AnyRecordId | Table | RecordIdRange> | The target table, record, or range (reactive) |
select? | (builder) => builder | Builder callback for filtering, pagination, etc. |
asyncDataOptions? | AsyncDataOptions | Options passed to useAsyncData |
Return type
Returns the same shape as useSurrealAsyncData.