Profile
Back to NewsBack
GitHub Trending 5 min
Reader Mode
surrealdb/surrealdb.js: SurrealDB SDK for JavaScript

surrealdb/surrealdb.js: SurrealDB SDK for JavaScript

7 hours ago


SurrealDB JavaScript SDK


Connect to remote and embedded SurrealDB instances


   


       

     

Documentation

View the SDK documentation here.

Learn SurrealDB

  • A Tour of SurrealDB: https://surrealdb.com/learn/tour
  • Aeon's Surreal Renaissance (Interactive book): https://surrealdb.com/learn/book
  • Documentation: https://surrealdb.com/docs

Packages

This monorepo publishes several npm packages. Most applications only need surrealdb - install engine or utility packages when your use case requires them.

| Package | npm | Description | Readme | | --- | --- | --- | --- | | surrealdb | surrealdb | Official database client - connect, query, live subscriptions, sessions | Read more | | @surrealdb/sqon | @surrealdb/sqon | Value types, CBOR/JSON codecs, and core utilities (re-exported by surrealdb) | Read more | | @surrealdb/wasm | @surrealdb/wasm | Embedded SurrealDB engine for browsers (mem://, indxdb://) | Read more | | @surrealdb/node | @surrealdb/node | Embedded SurrealDB engine for Node.js, Bun, and Deno | Read more | | @surrealdb/memory | @surrealdb/memory | Typed HTTP client for the Agent Memory API | Read more |

Quick start

bun add surrealdb
import { Surreal } from "surrealdb";

const db = new Surreal(); await db.connect("wss://my-instance.aws-euw1.surreal.cloud");

For installation options, connection setup, query examples, live queries, embedded engines, and TypeScript notes, see the surrealdb package readme.

Choosing the embedded engine version

@surrealdb/node and @surrealdb/wasm don't bundle SurrealDB. The engine ships separately as @surrealdb/node-native and @surrealdb/wasm-native, declared as peer dependencies, so you choose which SurrealDB release runs in your process. It is independent of the version of the driver package.

// package.json
{
    "dependencies": {
        "surrealdb": "^2.1.1",
        "@surrealdb/node": "^3.1.0",
        // Pin exactly for reproducible builds, or use a range to follow new releases
        "@surrealdb/node-native": "3.3.1"
    }
}

If you don't install the engine yourself, your package manager picks the newest release inside the peer range. See the @surrealdb/node and @surrealdb/wasm readmes for details.

Contributing

Local setup

This is a Bun project, not Node.js. It works across all major runtimes, however.

Supported environments

Requirements

  • Bun
  • SurrealDB (for testing)

TypeScript

This SDK supports both TypeScript 5 and TypeScript 6. If you are using TypeScript 6, note that the default value for the types compiler option changed from auto-discovering all @types/* packages to []. You may need to explicitly add the types you depend on in your tsconfig.json:

{
    "compilerOptions": {
        "types": ["node"]
    }
}

Build for all supported environments

For Deno, no build is needed. For all other environments run:

bun run build

The embedded engines

@surrealdb/node and @surrealdb/wasm are the SDK-facing half of the embedded engines. The database itself is built in the SurrealDB repository and published as @surrealdb/node-native and @surrealdb/wasm-native, so nothing here needs a Rust toolchain.

To develop against an unpublished engine, build the two packages in that repository and register them, then link each into the package that consumes it:

cd <surrealdb>/surrealdb/node && bun run build && bun link
cd ../wasm && bun run build && bun link
cd <surrealdb.js>/packages/node && bun link --no-save @surrealdb/node-native
cd ../wasm && bun link --no-save @surrealdb/wasm-native

The committed manifests pin a published version, so bun install on its own installs from the registry; the bun link in the consuming package is what overrides it. Keep --no-save — without it bun link rewrites the manifest to a link: specifier, which resolves to nothing on a clean checkout and takes every CI job down at bun install. To go back to the published engine, reinstall the consuming package with bun install --force.

One consequence of linking rather than installing: a linked package sits outside this project, and Vite's dev server refuses to serve files from there, so demo:wasm answers 403 for the WebAssembly module until the engine repository is added to server.fs.allow. A published dependency is inside node_modules and needs nothing.

Code quality

bun run qa - apply formatting and safe fixes

bun run qau - apply formatting and unsafe fixes

bun run qc - check code quality

bun run qts - TypeScript type check

Run tests

bun run test - WebSocket protocol

SURREAL_DEFAULT_PROTOCOL=http bun test - HTTP protocol

PRs

Before you commit, please format and lint your code accordingly to check for errors, and ensure all tests still pass.

Editor extensions

For local development the Bun extension and Biome extension for VSCode are helpful.

Directory structure

  • ./biome.json - code quality settings
  • ./scripts - build and publish scripts
  • ./packages/sdk - JavaScript SDK (surrealdb on npm)
  • ./packages/sqon - SQON value types and codecs
  • ./packages/node - embedded Node.js engine
  • ./packages/wasm - embedded WebAssembly engine
  • ./packages/memory - Agent Memory HTTP client
  • ./packages/tests - test suite (surrealdb/ for the SDK, memory/ for Agent Memory)
  • ./demo/wasm - WebAssembly demo
  • ./demo/node - Node.js demo
  • ./demo/expo - Expo / React Native demo
Chat with me