Composables

useSurreal

Access the remote SurrealDB client in your Vue components.

useSurreal

The useSurreal composable returns the main Surreal client instance connected to your remote SurrealDB endpoint. It auto-connects on first use based on your module configuration.

Usage

app/pages/index.vue
<script setup lang="ts">
const client = await useSurreal()

// Use the SDK directly
const results = await client.query('SELECT * FROM users;').json().collect(0)
</script>

Return type

Returns Promise<Surreal> — the connected SurrealDB client instance from the official SDK.

How it works

  1. On first call, the composable reads the endpoint and connection options from runtimeConfig.public.surrealdb
  2. If autoConnect is enabled (default), it connects automatically
  3. During SSR, if preferHttp is enabled (default), WebSocket URLs are converted to HTTP
  4. The surrealdb:connecting and surrealdb:connected hooks are called during the connection lifecycle

Direct SDK access

You can also access the Surreal client directly via the Nuxt app instance:

const { $surreal } = useNuxtApp()

Available properties:

  • $surreal — the remote Surreal client
  • $surrealMemory — the in-memory WASM client (or null)
  • $surrealLocal — the local WASM client (or null)