SurrealSession
The SurrealSession class represents a scoped contextual session attached to a SurrealDB connection. It provides authentication, session configuration, and inherits all query execution methods from SurrealQueryable.
Sessions allow you to maintain isolated contexts with their own namespace, database, variables, and authentication state, while sharing the underlying connection.
Extends: SurrealQueryable
Extended by: Surreal
Source: api/session.ts
Constructor
The constructor is typically not called directly. Use Surreal.newSession() or forkSession() to create new sessions.
Properties
namespace
Returns the currently selected namespace for this session.
Type: string | undefined
Example:
database
Returns the currently selected database for this session.
Type: string | undefined
Example:
accessToken
Returns the current authentication access token for this session.
Type: string | undefined
Example:
parameters
Returns all parameters currently defined on the session.
Type: Record<string, unknown>
Example:
session
Returns the unique session ID. For the default session, undefined is returned.
Type: Uuid | undefined
Example:
isValid
Returns whether the session is valid and can be used. This is always true for the default session, but will be false for sessions that have been disposed via reset() or closeSession().
Type: boolean
Example:
Session Management Methods
.forkSession()
Create a new session by cloning the current session. The new session inherits all properties from the parent session including namespace, database, variables, and authentication state.
Sessions are automatically restored when the connection reconnects. Call reset() on the created session to destroy it.
Returns
Promise<SurrealSession> - A new session instance
Example
.closeSession()
Closes the current session and disposes of it. After this method is called, the session cannot be used again, and isValid will return false.
Returns
Promise<void> - Resolves when the session is closed
Example
Transaction Methods
.beginTransaction()
Create a new transaction scoped to the current session. Transactions allow you to execute multiple queries atomically.
Call commit() on the transaction to apply changes, or cancel() to discard them.
Returns
Promise<SurrealTransaction> - A new SurrealTransaction instance
Example
Session Configuration Methods
.use()
Switch to the specified namespace and/or database for this session.
Leaving the namespace or database undefined will leave the current value unchanged, while passing null will unset the selected namespace or database.
Parameters
| Parameter | Type | Description |
|---|---|---|
what | NamespaceDatabase | null | Object specifying namespace and/or database to switch to. |
Returns
Promise<NamespaceDatabase> - The newly selected namespace and database
Examples
.set()
Define a variable for the current session. Variables can be used in SurrealQL queries with the $ prefix.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable | string | The name of the variable (without the $ prefix). |
value | unknown | The value to assign to the variable. |
Returns
Promise<void> - Resolves when the variable is set
Example
.unset()
Remove a variable from the current session.
Parameters
| Parameter | Type | Description |
|---|---|---|
variable | string | The name of the variable to remove (without the $ prefix). |
Returns
Promise<void> - Resolves when the variable is removed
Example
.reset()
Resets the current session to its initial state, clearing authentication state, variables, and selected namespace/database.
For non-default sessions, this also closes and disposes of the session.
Returns
Promise<void> - Resolves when the session is reset
Example
Authentication Methods
.signup()
Sign up a new record user to the SurrealDB instance.
Note
Parameters
| Parameter | Type | Description |
|---|---|---|
auth | AccessRecordAuth | The authentication details including access method and record data. |
Returns
Promise<Tokens> - The authentication tokens (access and refresh tokens)
Example
.signin()
Sign in to the SurrealDB instance with authentication credentials.
Note
Parameters
| Parameter | Type | Description |
|---|---|---|
auth | AnyAuth | Authentication details (system user, record user, or access method). |
Returns
Promise<Tokens> - The authentication tokens
Examples
.authenticate()
Authenticate the session using an existing access token or access and refresh token combination.
When authenticating with a refresh token, a new refresh token will be issued and returned.
Note
Parameters
| Parameter | Type | Description |
|---|---|---|
token | string | Tokens | The access token string or tokens object with access and refresh tokens. |
Returns
Promise<Tokens> - The authentication tokens (may include new refresh token)
Examples
.invalidate()
Invalidate the authentication for the current session, signing the user out.
Returns
Promise<void> - Resolves when authentication is invalidated
Example
Events
The SurrealSession class emits events that you can subscribe to for tracking session state changes.
auth
Emitted when the authentication state changes for this session.
Payload: [tokens: Tokens | null] - The new authentication tokens, or null if invalidated
Example:
using
Emitted when the namespace or database changes for this session.
Payload: [using: NamespaceDatabase] - Object containing the new namespace and database
Example:
.subscribe()
Subscribe to session events.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | keyof SessionEvents | The event name to subscribe to ("auth" or "using"). |
listener | Function | Callback function invoked when the event is emitted. |
Returns
() => void - An unsubscribe function
Example
Inherited Methods
As SurrealSession extends SurrealQueryable, it 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 functionsapi- Access user-defined APIs
Complete Example
See Also
Surreal - Main connection class
SurrealQueryable - Query execution methods
SurrealTransaction - Transaction support
Authentication Types - Authentication type definitions