DEV Community

Alex Spinov
Alex Spinov

Posted on

SurrealDB Has a Free Multi-Model API That Combines SQL, Graph, and Document

SurrealDB is a multi-model database that speaks SQL but handles documents, graphs, and time series in one engine. One query language for all your data patterns.

Setup

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

SurrealQL

-- Create records (schemaless by default)
CREATE user:alice SET name = 'Alice', email = 'alice@example.com', age = 30;
CREATE user:bob SET name = 'Bob', email = 'bob@example.com', age = 25;

-- Graph relations (RELATE)
RELATE user:alice->follows->user:bob SET since = time::now();
RELATE user:alice->likes->post:1 SET at = time::now();

-- Query graph traversals
SELECT name, ->follows->user.name AS following FROM user:alice;
-- Returns: { name: 'Alice', following: ['Bob'] }

-- Document queries
SELECT * FROM user WHERE age > 25 ORDER BY name;

-- Nested objects (no schema needed)
CREATE product SET
  name = 'MacBook Pro',
  specs = { cpu: 'M3', ram: '16GB', storage: '512GB' },
  tags = ['laptop', 'apple', 'pro'];

SELECT * FROM product WHERE specs.ram = '16GB';
Enter fullscreen mode Exit fullscreen mode

JavaScript SDK

import Surreal from 'surrealdb.js';

const db = new Surreal();
await db.connect('http://localhost:8000/rpc');
await db.signin({ user: 'root', pass: 'root' });
await db.use({ ns: 'myapp', db: 'main' });

// Create
const user = await db.create('user', {
  name: 'Alice',
  email: 'alice@example.com'
});

// Select with filter
const active = await db.query(
  'SELECT * FROM user WHERE active = true ORDER BY created DESC LIMIT 10'
);

// Graph query
const network = await db.query(`
  SELECT name, ->follows->user.name AS friends,
         ->follows->user->follows->user.name AS friends_of_friends
  FROM user:alice
`);

// Live queries (real-time)
await db.live('user', (action, result) => {
  console.log(`User ${action}:`, result);
});
Enter fullscreen mode Exit fullscreen mode

REST API

curl -X POST http://localhost:8000/sql \
  -H "Authorization: Basic $(echo -n 'root:root' | base64)" \
  -H "NS: myapp" -H "DB: main" \
  -d "SELECT * FROM user LIMIT 10"
Enter fullscreen mode Exit fullscreen mode

Why This Matters

  • Multi-model: Documents + graphs + relations in one DB
  • SQL-like: Familiar syntax, no new query language to learn
  • Graph traversals: First-class RELATE and graph queries
  • Real-time: Live queries push changes to clients
  • Schemaless or strict: Choose per table

Need custom database tools or multi-model data solutions? I build developer tools. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.

Top comments (0)