SolidJS is a modern JavaScript framework for building responsive and high-performing user interfaces. The SurrealDB SDK for JavaScript can be used in your SolidJS applications to interact with your SurrealDB instance.
This guide walks you through setting up a connection provider and executing queries in a SolidJS project.
Follow the installation guide for more information on how to install the SDK in your project.
Creating the connection provider
We recommend initializing the SDK in a Context Provider so the Surreal client is accessible anywhere in your component tree. The provider below manages the connection lifecycle, tracks connection status via TanStack Query, and cleans up automatically.
The params prop accepts the same options as .connect(), including namespace, database, and authentication.
import { Surreal } from "surrealdb"; import { useContext, createContext, JSX, createEffect, onCleanup, Accessor, onMount } from "solid-js"; import { createMutation } from "@tanstack/solid-query"; import { createStore } from "solid-js/store";
export function useSurreal(): SurrealProviderState { const context = useContext(SurrealContext); if (!context) throw new Error("useSurreal must be used within a SurrealProvider"); return context; }
export function useSurrealClient() { return useSurreal().client; }
Wrapping your application
In your top-level component, wrap the root with QueryClientProvider and SurrealProvider. Pass the endpoint and any connection options through the params prop.
import type { Component } from "solid-js"; import { QueryClient, QueryClientProvider } from "@tanstack/solid-query"; import { SurrealProvider } from "./SurrealProvider"; import App from "./App";
Use the useSurrealClient() hook to access the Surreal instance from any component. All query methods are available on the client, including .query(), .select(), .create(), and more.
import { createResource, For, Show } from "solid-js"; import { Table } from "surrealdb"; import { useSurreal, useSurrealClient } from "./SurrealProvider";
interface User { id: string; name: string; email: string; }
You can build an authentication layer on top of the provider using the SDK's .signin() and .signup() methods. The example below shows a minimal hook for record access authentication.
import { useSurrealClient } from "./SurrealProvider";
export function useAuth() { const client = useSurrealClient();