Surreal
The Surreal class is the primary interface for connecting to a SurrealDB instance, managing connections, executing queries, and handling database sessions. It extends SurrealSession and inherits all session management and query execution capabilities.
By default, a Surreal instance operates with a default session scope, but you can create additional isolated sessions using the session management methods.
Extends: SurrealSession → SurrealQueryable
Source: api/surreal.ts
Constructor
Syntax
Parameters
| Parameter | Type | Description |
|---|---|---|
options | DriverOptions | Driver-wide configuration options for customizing engines, codecs, and implementations. |
Examples
Properties
status
Returns the current connection status.
Type: ConnectionStatus
Values: "disconnected" | "connecting" | "reconnecting" | "connected"
Example:
isConnected
Returns whether the connection is currently established. This is equivalent to checking if status === "connected".
Type: boolean
Example:
ready
A promise that resolves when the connection is established and ready, or rejects if a connection error occurs.
Type: Promise<void>
Example:
Inherited Properties
The Surreal class inherits all properties from SurrealSession, including:
namespace- Current namespacedatabase- Current databaseaccessToken- Current access tokenparameters- Session parameterssession- Session IDisValid- Session validity status
Connection Methods
.connect()
Connect to a local or remote SurrealDB instance.
Warning
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | URL | The endpoint URL to connect to (e.g., "ws://localhost:8000", "http://localhost:8000/rpc"). |
opts | ConnectOptions | Connection-specific options such as namespace, database, and authentication. |
Returns
Promise<true> - Resolves to true when connection is successful
Examples
.close()
Disconnect from the active SurrealDB instance.
Returns
Promise<true> - Resolves to true when disconnection is successful
Example
.health()
Check the health status of the connected SurrealDB instance.
Returns
Promise<void> - Resolves if the instance is healthy, rejects otherwise
Example
.version()
Retrieve version information from the connected SurrealDB instance.
Returns
Promise<VersionInfo> - An object containing version information
Example
.isFeatureSupported()
Check whether a specific feature is available in the current connection.
Parameters
| Parameter | Type | Description |
|---|---|---|
feature | Feature | A feature from the Features object (e.g. Features.LiveQueries, Features.Api). |
Returns
boolean - true if the feature is supported, false otherwise
Example
Session Management Methods
.sessions()
List all active sessions on the current connection.
Returns
Promise<Uuid[]> - An array of session IDs
Example
.newSession()
Create a new isolated session on the current connection. The new session will have its own namespace, database, variables, and authentication state, but will share the same connection.
Sessions are automatically restored when the connection reconnects. Call reset() on the returned session to destroy it.
Returns
Promise<SurrealSession> - A new SurrealSession instance
Example
.closeSession()
Close the primary session. This is equivalent to calling close() on the connection.
Returns
Promise<void> - Resolves when the session is closed
Example
Data Management Methods
.export()
Export the database contents as a SQL string.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | SqlExportOptions | Options to customize what gets exported. |
Returns
Promise<string> - The exported database as a SQL string
Examples
.import()
Import database contents from a SQL string.
Parameters
| Parameter | Type | Description |
|---|---|---|
input | string | The SQL string to import into the database. |
Returns
Promise<void> - Resolves when the import is complete
Example
Events
The Surreal class implements the EventPublisher interface and emits various events during the connection lifecycle. Subscribe to events using the subscribe() method.
connecting
Emitted when the connection attempt starts.
Payload: None
Example:
connected
Emitted when the connection is successfully established.
Payload: [version: string] - The SurrealDB version string
Example:
reconnecting
Emitted when the connection is attempting to reconnect after being disconnected.
Payload: None
Example:
disconnected
Emitted when the connection is closed.
Payload: None
Example:
error
Emitted when a connection error occurs.
Payload: [error: Error] - The error object
Example:
Inherited Events
The Surreal class also inherits and re-emits events from SurrealSession:
auth- Emitted when authentication state changesusing- Emitted when namespace/database changes
.subscribe()
Subscribe to connection and session events.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | keyof SurrealEvents | The event name to subscribe to. |
listener | Function | Callback function invoked when the event is emitted. |
Returns
() => void - An unsubscribe function to remove the event listener
Example
Inherited Methods
As Surreal extends SurrealSession, it inherits all authentication and query methods:
Authentication Methods
signup()- Sign up a new usersignin()- Sign in with credentialsauthenticate()- Authenticate with a tokeninvalidate()- Invalidate the session
Session Configuration Methods
use()- Set namespace and databaseset()- Set a session parameterunset()- Remove a session parameterreset()- Reset the session
Query Methods
As Surreal extends SurrealQueryable (via SurrealSession), it also inherits all query execution methods:
query()- Execute raw SurrealQLselect()- Select recordscreate()- Create recordsinsert()- Insert recordsupdate()- Update recordsupsert()- Upsert recordsdelete()- Delete recordsrelate()- Create graph relationshipslive()- Subscribe to live queriesrun()- Execute functions
Transaction Method
beginTransaction()- Start a transaction
Type Parameters
This class does not use generic type parameters.
Complete Example
See Also
SurrealSession - Session management and authentication
SurrealQueryable - Query execution methods
SurrealTransaction - Transaction support
Connection Engines - Engine-specific documentation
Data Types - Working with data types