The SDK provides comprehensive TypeScript type definitions for type-safe development. This page documents the key types and interfaces used throughout the SDK.
ConnectionStatusRepresents the current connection state.
type ConnectionStatus = "disconnected" | "connecting" | "reconnecting" | "connected"
Example:
if (db.status === "connected") { console.log('Ready to execute queries'); }
DriverOptionsConfiguration options for the Surreal driver.
interface DriverOptions { engines?: Engines; codecs?: Codecs; codecOptions?: CodecOptions; websocketImpl?: typeof WebSocket; fetchImpl?: typeof fetch; }
Properties:
engines - Custom engine factories for different protocolscodecs - Custom codec factories for encoding/decodingcodecOptions - Options for codec behaviorwebsocketImpl - Custom WebSocket implementationfetchImpl - Custom fetch implementationExample:
const db = new Surreal({ codecOptions: { useNativeDates: true } });
ConnectOptionsOptions for establishing a connection.
interface ConnectOptions { namespace?: string; database?: string; authentication?: AuthProvider; versionCheck?: boolean; invalidateOnExpiry?: boolean; reconnect?: boolean | Partial<ReconnectOptions>; }
Properties:
namespace - Namespace to usedatabase - Database to useauthentication - Authentication details or provider functionversionCheck - Enable version compatibility checking (default: true)invalidateOnExpiry - Invalidate session on token expiry (default: false)reconnect - Reconnection behavior configuration (default: true)Example:
await db.connect('ws://localhost:8000', { namespace: 'my_namespace', database: 'my_database', authentication: { username: 'root', password: 'root' }, reconnect: { attempts: 10, retryDelay: 1000 } });
ReconnectOptionsConfiguration for automatic reconnection behavior.
interface ReconnectOptions { enabled: boolean; attempts: number; retryDelay: number; retryDelayMax: number; retryDelayMultiplier: number; retryDelayJitter: number; catch?: (error: Error) => boolean; }
Properties:
enabled - Enable automatic reconnectionattempts - Maximum reconnection attempts (-1 for unlimited)retryDelay - Initial delay before reconnecting (ms)retryDelayMax - Maximum delay between attempts (ms)retryDelayMultiplier - Multiply delay after each failed attemptretryDelayJitter - Random offset percentage for delayscatch - Custom error handler for reconnection errorsVersionInfoSurrealDB version information.
interface VersionInfo { version: string; }
Example:
const info = await db.version(); console.log(info.version); // "surrealdb-2.1.0"
AnyAuthUnion type for all authentication methods.
type AnyAuth = | SystemAuth | NamespaceAuth | DatabaseAuth | AccessRecordAuth
SystemAuthSystem user authentication (root, namespace, or database level).
interface SystemAuth { username: string; password: string; }
Example:
await db.signin({ username: 'root', password: 'root' });
AccessRecordAuthRecord user authentication via access methods.
interface AccessRecordAuth { namespace: string; database: string; access: string; variables?: Record<string, unknown>; }
Example:
await db.signup({ namespace: 'my_namespace', database: 'my_database', access: 'user_access', variables: { email: 'user@example.com', password: 'password123' } });
TokensAuthentication token pair.
interface Tokens { access: string; refresh?: string; }
Example:
const tokens = await db.signin(credentials); console.log(tokens.access); // JWT access token console.log(tokens.refresh); // Optional refresh token
AuthProviderFunction or static value for providing authentication.
type AuthProvider = | AnyAuth | (() => AnyAuth | Promise<AnyAuth>)
Example:
await db.connect('ws://localhost:8000', { authentication: async () => ({ username: await getUsername(), password: await getPassword() }) });
SessionSession identifier type.
type Session = Uuid | undefined
NamespaceDatabaseNamespace and database pair.
interface NamespaceDatabase { namespace?: string; database?: string; }
Example:
await db.use({ namespace: 'production', database: 'main' });
SessionEventsEvents emitted by sessions.
type SessionEvents = { auth: [Tokens | null]; using: [NamespaceDatabase]; }
SurrealEventsEvents emitted by Surreal instances.
type SurrealEvents = SessionEvents & { connecting: []; connected: [string]; reconnecting: []; disconnected: []; error: [Error]; }
RecordResult<T>Ensures records have an id field of type RecordId.
type RecordResult<T> = T extends object ? { id: RecordId } & T : { id: RecordId }
Example:
interface User { name: string; email: string; } const user: RecordResult<User> = await db.select(new RecordId('users', 'john')); console.log(user.id); // RecordId console.log(user.name); // string
QueryResponse<T>Response from a query execution.
type QueryResponse<T = unknown> = | QueryResponseSuccess<T> | QueryResponseFailure interface QueryResponseSuccess<T> { success: true; stats?: QueryStats; type: "live" | "kill" | "other"; result: T; } interface QueryResponseFailure { success: false; stats?: QueryStats; error: { code: number; message: string; }; }
Example:
const responses = await db.query('SELECT * FROM users').responses(); for (const response of responses) { if (response.success) { console.log('Result:', response.result); } else { console.error('Error:', response.error.message); } }
QueryStatsQuery execution statistics.
interface QueryStats { recordsReceived: number; bytesReceived: number; recordsScanned: number; bytesScanned: number; duration: Duration; }
LiveResourceResources that can be subscribed to with live queries.
type LiveResource = Table | RecordId | RecordIdRange
LiveMessageMessage received from a live query subscription.
interface LiveMessage<T = unknown> { action: "CREATE" | "UPDATE" | "DELETE"; result: T; diff?: unknown; }
Example:
for await (const message of subscription) { console.log(`${message.action}:`, message.result); }
RecordIdValueValid types for record ID components.
type RecordIdValue = | string | number | Uuid | bigint | unknown[] | Record<string, unknown>
Values<T>Extract values from a type, excluding id field.
type Values<T> = Omit<T, 'id'>
Example:
interface User { id: RecordId; name: string; email: string; } const userData: Values<User> = { name: 'John', email: 'john@example.com' // id is excluded };
Nullable<T>Make properties nullable.
type Nullable<T> = { [K in keyof T]: T[K] | null; }
CodecOptionsOptions for value encoding/decoding.
interface CodecOptions { useNativeDates?: boolean; valueEncodeVisitor?: (value: unknown) => unknown; valueDecodeVisitor?: (value: unknown) => unknown; }
Properties:
useNativeDates - Use native Date objects instead of DateTime (loses nanosecond precision)valueEncodeVisitor - Custom function to transform values before encodingvalueDecodeVisitor - Custom function to transform values after decodingExample:
const db = new Surreal({ codecOptions: { useNativeDates: true, valueDecodeVisitor: (value) => { // Custom transformation return value; } } });
SqlExportOptionsOptions for database export.
interface SqlExportOptions { users: boolean; accesses: boolean; params: boolean; functions: boolean; analyzers: boolean; tables: boolean | string[]; versions: boolean; records: boolean; sequences: boolean; v3: boolean; }
The v3 option controls whether to include v3-specific export content.
Example:
const sql = await db.export({ tables: ['users', 'posts'], records: true, functions: false });
Prettify<T>Expand type for better IDE display.
type Prettify<T> = { [K in keyof T]: T[K] } & {}
EventPublisher<T>Interface for event subscription.
interface EventPublisher<T extends Record<string, unknown[]>> { subscribe<K extends keyof T>( event: K, listener: (...payload: T[K]) => void ): () => void; }
ApiRequest<T>Request options for user-defined API endpoints.
interface ApiRequest<T = unknown> { body?: T; method?: string; headers?: Record<string, string>; query?: Record<string, string>; }
Properties:
body - Request body to sendmethod - HTTP method (default: "get")headers - Additional headers for the requestquery - Query parameters to append to the URLExample:
const api = db.api(); const result = await api.invoke('/custom', { method: 'post', body: { data: 'value' }, headers: { 'X-Custom': 'header' }, query: { filter: 'active' } });
Leverage generics for type-safe operations:
interface User { name: string; email: string; } // Type-safe selection const users = await db.select<User>(new Table('users')); users[0].name; // TypeScript knows this is a string
Create types for your data models:
interface Post { title: string; content: string; author: RecordId<'users'>; created_at: DateTime; } const posts = await db.select<Post>(new Table('posts'));
Implement type guards for runtime type checking:
function isUser(value: unknown): value is User { return ( typeof value === 'object' && value !== null && 'name' in value && 'email' in value ); } if (isUser(data)) { console.log(data.email); // Type-safe }
Properly handle discriminated unions:
const response = await db.query('SELECT * FROM users').responses(); for (const r of response) { if (r.success) { console.log(r.result); // Success case } else { console.error(r.error); // Failure case } }
Source: types/