DEV Community

Alex Spinov
Alex Spinov

Posted on

SurrealDB Has a Free API — Heres How to Use One Database for Everything

SurrealDB is a multi-model database — relational, document, graph, and time-series in one. Replace your PostgreSQL + MongoDB + Neo4j stack with a single database.

Why SurrealDB?

  • Multi-model: SQL tables, JSON documents, graph relations
  • Real-time: Live queries with WebSocket
  • Permissions: Row-level security with SurrealQL
  • Embeddable: Run in-process or as a server
  • Single binary: Rust-powered, no dependencies
  • SurrealQL: SQL-like with graph traversal

Install

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

REST API

# Create a record
curl -X POST http://localhost:8000/sql \
  -H 'Authorization: Basic cm9vdDpyb290' \
  -H 'NS: test' -H 'DB: test' \
  -d 'CREATE user SET name = "Alice", email = "alice@example.com", age = 30;'

# Query
curl -X POST http://localhost:8000/sql \
  -H 'Authorization: Basic cm9vdDpyb290' \
  -H 'NS: test' -H 'DB: test' \
  -d 'SELECT * FROM user WHERE age > 25;'
Enter fullscreen mode Exit fullscreen mode

SurrealQL (SQL + Graph + Document)

-- Relational
CREATE user:alice SET name = 'Alice', email = 'alice@example.com';
CREATE user:bob SET name = 'Bob', email = 'bob@example.com';

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

-- Graph traversal
SELECT ->follows->user.name FROM user:alice;
-- ['Bob']

-- Nested documents
CREATE post SET
  title = 'My Post',
  tags = ['rust', 'database'],
  metadata = { views: 0, featured: false };
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.use({ namespace: 'test', database: 'test' });
await db.signin({ username: 'root', password: 'root' });

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

// Query
const results = await db.query('SELECT * FROM user WHERE age > $min', { min: 25 });

// Live query
await db.live('user', (action, result) => {
  console.log(action, result); // 'CREATE', { id: 'user:xxx', ... }
});
Enter fullscreen mode Exit fullscreen mode

Permissions

DEFINE TABLE post SCHEMALESS
  PERMISSIONS
    FOR select WHERE published = true OR author = $auth.id
    FOR create WHERE $auth.id != NONE
    FOR update WHERE author = $auth.id
    FOR delete WHERE author = $auth.id OR $auth.role = 'admin';
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case

A social app used PostgreSQL for users, MongoDB for posts, and Neo4j for social graph. After migrating to SurrealDB, they eliminated 2 databases and the sync code between them. One query now fetches a user, their posts, and friend recommendations — all from one database.


Need to automate data collection? Check out my Apify actors for ready-made scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)