Skip to content

SQL drivers for JavaScript

SQL drivers installation

Install the package for the dialect you use. Each package is ESM only and has no runtime dependencies.

bun add @surrealdb/pg
bun add @surrealdb/mysql

pg is an optional peer dependency of @surrealdb/pg. It is needed only for the Prisma path, and Drizzle's node-postgres driver imports it itself.

RequirementVersion
RuntimeNode.js 20 or later, Bun, Deno, Cloudflare Workers, Vercel Edge, or a browser
SurrealDB serverA version that serves the pgsql and mysql RPC methods

The packages use fetch, WebSocket, AbortController and TextEncoder, and no Node.js built-ins. Node.js versions without a global WebSocket need a constructor passed as the WebSocket option, for example from the ws package. HTTP alone needs nothing extra.

Use ws on a Node.js version without a global WebSocket
import WebSocket from "ws";
import { connect } from "@surrealdb/pg";

const sql = connect({ url: "surrealdb+wss://abc123.surrealdb.earth/prod/app", WebSocket });

Each package is about 11 kB minified and compressed with brotli.

Warning

Do not put database credentials in browser code that real users can read. Pass a token, or a function that returns one, as the auth option.

Call capabilities() to check that an instance serves the method before you rely on it.

const caps = await sql.capabilities();
// { supported: true, version: "...", transport: { ... } }

It reports supported: false with a reason when the method or the dialect route is denied, for example with --deny-rpc pgsql.

Was this page helpful?