npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

surreal-tools

v1.0.0

Published

**surreal-tools** is a utility package designed to simplify interactions with [SurrealDB](https://surrealdb.com/). It currently provides:

Readme

surreal-tools

surreal-tools is a utility package designed to simplify interactions with SurrealDB. It currently provides:

  • A tagged template function surql for executing typed queries with variable interpolation.
  • A comprehensive migration tool to manage and apply schema changes in a structured, automated way.

Installation

npm install surreal-tools

Note: If you are using an embedded database protocol (mem://, rocksdb://, surrealkv://), you also need to install @surrealdb/node:

npm install @surrealdb/node

Usage

surql: Tagged Template Function for Queries

Basic Setup

import { Surreal } from "surrealdb";
import { createSurql } from "surreal-tools";

const surreal = new Surreal();
await surreal.connect("ws://localhost:8000");
const surql = createSurql(surreal);

Executing a Simple Query

const responses = await surql`INFO FOR ROOT`;

Executing a Typed Query

const responses = await surql`INFO FOR ROOT`.$type<
  { namespaces: Record<string, string> }
>();

Using Variables in Queries

const namespace = "test";

// Inline template variable
const responses = await surql`USE NS ${namespace}`;

// Or using named variables
const responses = await surql`USE NS $namespace`.vars({ namespace: "test" });

Transaction Behavior

Queries containing multiple statements (separated by ;) are automatically wrapped in a transaction:

// This is automatically wrapped in BEGIN TRANSACTION ... COMMIT TRANSACTION
const results = await surql`
  CREATE user SET name = 'Alice';
  CREATE user SET name = 'Bob';
`;

To opt out of automatic transaction wrapping, use .options({ transaction: false }):

const results = await surql`
  CREATE user SET name = 'Alice';
  CREATE user SET name = 'Bob';
`.options({ transaction: false });

Database Migrations

Surreal Tools includes a CLI and programmatic API to help manage schema migrations for SurrealDB.

Initialization

Create a surreal.config.ts configuration file manually or initialize it using the CLI:

npx surreal-tools migration init

This will:

  • Prepare the migration namespace and database (default: migrations/migrations)
  • Create a migrations folder (default: .surreal-migrations)

Creating a Migration

After making schema changes (e.g., adding tables, fields, or namespaces), create a migration:

npx surreal-tools migration create

This generates a new migration file inside the .surreal-migrations directory.

Optional parameters:

  • --name <filename>: Specify a custom filename for the migration.
  • --custom: Create a custom migration script.

Applying Migrations

via CLI

npx surreal-tools migration apply

(Requires a valid surreal.config.ts file.)

Programmatically

import { Surreal } from "surrealdb";
import { applyMigrations } from "surreal-tools";

const surreal = new Surreal();
await surreal.connect("ws://localhost:8000");

// Pass a Surreal or Surql instance
const applied = await applyMigrations(surreal);

// Optionally pass migration options as second argument
// await applyMigrations(surreal, {
//   baseDir: '.surreal-migrations',
//   migrationNamespace: 'migrations',
//   migrationDatabase: 'migrations',
// });

License

MIT