We're excited to announce the SurrealDB JavaScript SDK v2, the most significant update to the SDK to date. We've rebuilt the core internals with a focus on ergonomics, flexibility, and developer experience. Key highlights include full support for SurrealDB 3.0, multi-session support, automatic token refreshing, client-side transactions, a redesigned live query API, and a new query builder pattern that makes working with your data more intuitive than ever.
Welcome WASM and Node!
The existing WebAssembly and Node.js SDKs have been rewritten, updated to support the 2.0 JavaScript SDK, and moved into the JavaScript SDK repository.
Going forward, the JS, WASM, and Node.js SDKs will be published together, so embedded SurrealDB stays up to date. The WASM and Node.js SDK versions align their major and minor versions with SurrealDB, so you can tell at a glance which SurrealDB version you're running.
As a bonus, the WASM SDK now supports running inside a Web Worker, so you can offload database work from the main thread and keep your UI responsive.
Wasm
Node.js (and Bun & Deno)
Official event listeners
The original SDK exposed events through the internal surreal.emitter field. The new SDK provides a type-safe surreal.subscribe() function instead. Calling .subscribe() returns a cleanup function that unsubscribes the listener when invoked.
Access internal state
New getters let you read internal state from the Surreal instance:
surreal.namespaceandsurreal.databasefor the selected namespace and databasesurreal.paramsfor connection parameterssurreal.accessTokenandsurreal.refreshTokenfor authentication tokens
Automatic token refreshing
The SDK now restores or renews authentication when your access token expires or the connection reconnects. When refresh tokens are available, they are exchanged for a new token pair. Otherwise the SDK reuses the credentials you provided or fires an auth event for custom handling. Read more on GitHub.
For asynchronous authentication, you can pass a callable to the authentication property:
Multi-session support
You can create multiple isolated sessions on a single connection, each with its own namespace, database, variables, and authentication state. New sessions can be created at any time, or you can fork an existing session and reuse its state.
Simple example
Forking sessions
Await using
Redesigned live query API
Live query methods on the Surreal class have been redesigned to be more intuitive. Live select queries can also restart automatically when the driver reconnects.
The record ID is now passed as the third argument to your handlers, so you can identify which record changed when handling patch updates.
Improved parameter explicitness
Query functions no longer accept plain strings as table names. You must use the Table class, which avoids record IDs being mistaken for table names.
Query builder pattern
A new builder pattern lets you configure RPC calls with optional chaining. All query methods support chainable helpers for filtering, limiting, and fetching.
As a result, update and upsert no longer take content as a second argument. You choose how to change records by calling .content(), .merge(), .replace(), or .patch().
Query method overhaul
The .query() function has been overhauled to support more use cases, including picking response indexes, automatic JSON output, and streaming responses.
SurrealDB does not yet support streaming individual records, but this API is ready for when it does. It works with current SurrealDB versions and is now the only way to obtain query stats.
Expressions API
A new Expressions API works with the .where() function and makes it easier to build dynamic expressions. It integrates with the surql template tag so you can insert parameter-safe expressions anywhere.
You can also serialize expressions to a string with the expr() function:
Value encode/decode visitor API
For advanced use cases where you need to process SurrealDB value types, you can pass encode or decode visitor callbacks in the Surreal constructor. These run for each value sent to or received from the engine, so you can modify or wrap values before they appear in responses.
Diagnostics API
The Diagnostics API lets you wrap engines and inspect protocol-level communication. It is useful for debugging queries, analysing SDK behaviour, measuring timings, and similar tasks.
Because it is implemented as a wrapper engine, there is no extra cost unless you use it. We do not recommend using it in production, as it can affect performance and the events may change between versions.
Each event has a type, key, and phase. The type identifies the operation, key is a stable id across phases, and phase indicates start, progress, or completion. The query diagnostic exposes the internal chunk stream; you are responsible for stitching queries and batches together. The after phase includes duration, success, and result.
Get started with the new SDK
You can visit the JavaScript SDK documentation for more information on the new SDK, which has been fully updated to reflect all new changes. It contains many new concept guides explaining how to use the new SDK, and now includes a comprehensive API reference for detailed information.
Follow the Quickstart guide to get started.
