The official SurrealDB SDK for JavaScript.
surrealdb.js
The official SurrealDB SDK for JavaScript.
Documentation
View the SDK documentation here.
How to install
Import it with:
import Surreal from "@surrealdb/surrealdb";
For best results, set a version in the url:
Install it with:
npm i surrealdb.js
pnpm i surrealdb.js
yarn add surrealdb.js
Next, just import it with:
const { Surreal } = require("surrealdb.js");
or when you use modules:
import Surreal from "surrealdb.js";
Install for the browser
For usage in a browser environment, when using a bundler (e.g. Rollup, Vite, or webpack) you can install it with:
npm i surrealdb.js
pnpm i surrealdb.js
yarn add surrealdb.js
Next, just import it with:
import Surreal from "surrealdb.js";
or when you use CommonJS:
const { Surreal } = require("surrealdb.js");
Install for the browser with a CDN
For fast prototyping we provide a browser-ready bundle. You can import it with:
import Surreal from "https://unpkg.com/surrealdb.js";
import Surreal from "https://cdn.jsdelivr.net/npm/surrealdb.js";
NOTE: this bundle is not optimized for production! So don't use it in production!
Getting started
In the example below you can see how to connect to a remote instance of SurrealDB, authenticating with the database, and issuing queries for creating, updating, and selecting data from records.
This example requires SurrealDB to be installed and running on port 8000.
This example makes use of top level await, available in modern browsers, Deno and Node.js >= 14.8.
import { Surreal, RecordId, Table } from "surrealdb.js";
const db = new Surreal();
await db.connect("http://127.0.0.1:8000/rpc");
await db.use({
namespace: "test",
database: "test"
});
await db.signin({
username: "root",
password: "root",
});
let created = await db.create("person", {
title: "Founder & CEO",
name: {
first: "Tobie",
last: "Morgan Hitchcock",
},
marketing: true,
});
let updated = await db.merge(new RecordId('person', 'jaime'), {
marketing: true,
});
let people = await db.select("person");
let groups = await db.query(
"SELECT marketing, count() FROM $tb GROUP BY marketing",
{
tb: new Table("person"),
},
);
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)
Build for all supported environments
For Deno, no build is needed. For all other environments run
bun run build
.
Code Quality Fixes
bun quality:apply
Code Quality unsafe fixes
bun quality:apply:unsafe
Run tests for WS
bun test
Run tests for HTTP
SURREAL_PROTOCOL=http bun test
PRs
Before you commit, please format and lint your code accordingly to check for
errors, and ensure all tests still pass
Local setup
For local development the
Bun extension and Biome extension
for VSCode are helpful.
Directory structure
./biome.json
include settings for code quality../scripts
include the build scripts for NPM and JSR../src
includes all source code. ./src/index.ts
is the main entrypoint../dist
is build by ./scripts/build.ts
and includes the compiled and minified bundles for ESM, CJS and bundled ESM targets../tests
includes all test files.