DEV Community

Alex Spinov
Alex Spinov

Posted on

SurrealDB Has a Free API: A Multi-Model Database for Documents, Graphs, and Time Series

SurrealDB is a multi-model database that handles documents, graphs, key-value, and time-series data in a single system with a SQL-like query language called SurrealQL.

Why SurrealDB Matters

Modern apps use MongoDB for documents, Neo4j for graphs, Redis for KV, and TimescaleDB for time series. SurrealDB replaces all four with one database.

What you get for free:

  • Multi-model: document, graph, KV, time-series in one DB
  • SurrealQL: SQL-like but with graph traversals and record links
  • Real-time subscriptions (LIVE SELECT)
  • Built-in authentication and permissions
  • Schema-less or schema-full (your choice)
  • Embedded, single-node, or distributed
  • WebSocket and HTTP APIs
  • Runs in browser via WebAssembly

Quick Start

curl -sSf https://install.surrealdb.com | sh
surreal start --user root --pass root

# Connect
surreal sql --conn http://localhost:8000 --user root --pass root --ns test --db test
Enter fullscreen mode Exit fullscreen mode

SurrealQL

-- Create records (schemaless)
CREATE person:alice SET name = 'Alice', age = 30, skills = ['Rust', 'Go'];
CREATE person:bob SET name = 'Bob', age = 25, skills = ['Python', 'JS'];

-- Record links (graph-like)
RELATE person:alice->knows->person:bob SET since = '2024-01-01';
RELATE person:alice->works_at->company:acme SET role = 'Engineer';

-- Query with graph traversal
SELECT name, ->knows->person.name AS friends FROM person:alice;
-- Returns: { name: 'Alice', friends: ['Bob'] }

-- Deep graph traversal
SELECT name, ->knows->person->works_at->company.name AS friends_companies
FROM person:alice;

-- Aggregation
SELECT skills, count() AS total
FROM person
GROUP BY skills;
Enter fullscreen mode Exit fullscreen mode

JavaScript SDK

import Surreal from "surrealdb.js";

const db = new Surreal();
await db.connect("http://127.0.0.1:8000/rpc");
await db.signin({ user: "root", pass: "root" });
await db.use({ ns: "test", db: "test" });

// Create
const alice = await db.create("person", {
  name: "Alice",
  age: 30,
  skills: ["Rust", "Go"],
});

// Query
const people = await db.query(
  "SELECT * FROM person WHERE age > $min_age",
  { min_age: 20 }
);

// Real-time subscription
await db.live("person", (action, result) => {
  console.log(action, result); // CREATE, UPDATE, DELETE
});
Enter fullscreen mode Exit fullscreen mode

Real-time Subscriptions

-- Live query: get notified on changes
LIVE SELECT * FROM person WHERE age > 20;

-- Any INSERT/UPDATE/DELETE triggers a notification
CREATE person:carol SET name = 'Carol', age = 28;
-- Subscribers instantly receive this new record
Enter fullscreen mode Exit fullscreen mode

Permissions

-- Define access rules
DEFINE TABLE post SCHEMAFUL
  PERMISSIONS
    FOR select FULL
    FOR create WHERE $auth.role = 'admin'
    FOR update WHERE author = $auth.id
    FOR delete WHERE $auth.role = 'admin';
Enter fullscreen mode Exit fullscreen mode

Links


Building multi-model data apps? Check out my developer tools on Apify or email spinov001@gmail.com for custom solutions.

Top comments (0)