Surreal DB

From Coders.Bay Wiki
Revision as of 10:24, 24 November 2022 by (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Surreal DB[edit]

Surreal DB is aiming to be at the intersection of relational, document and graph databases, while still remaining simple to use with an SQL-like query language.
It is schemaless by default, implicitely creates tables and uses record links instead of foreign keys (RELATE keyword).
more info:



curl -sSf | sh


brew install surrealdb/tap/surreal

Windows: (powershell!)[edit]

iwr -useb | iex

more info:[edit]

run server: (cmd)[edit]

surreal start --log trace --user root --pass root memory

run client (cmd - instead of postman or similar)[edit]

surreal sql --conn http://localhost:8000 --user root --pass root --ns whatever --db whatever --pretty



create armor:plate set resistance = 20;
create armor:leather set resistance = 3;
create player:leeroy set strength = 10, armor = armor:plate;
create player:bob set strength = 50, armor = armor:leather;

SELECT * FROM player FETCH armor;

SELECT math::sum(strength) FROM player GROUP BY ALL;

CREATE armor:dragon SET resistance = 50;
RELATE player:leeroy->wants_to_buy->armor:dragon;
SELECT id, ->wants_to_buy->armor as wtb from player;
SELECT id, <-wants_to_buy<-player as players from armor:dragon;

JavaScript Function[edit]

CREATE film SET ratings = [ { rating: 6, user: user:bt8e39uh1ouhfm8ko8s0 }, { rating: 8, user: user:bsilfhu88j04rgs0ga70 }, ], featured = function() { return this.ratings.filter(r => { return r.rating >= 7; }).map(r => { return { ...r, rating: r.rating * 10 }; }); }

Humans driving cars / Relations[edit]

CREATE human:john SET name="John", age=50;
CREATE human:chad SET name="Chad", age=45;

Embedded One-to-Many[edit]

UPDATE human SET skills += ['breathing', 'walking'];


UPDATE human:chad SET bff = human:john;
SELECT, bff.age FROM human:chad;


CREATE car:tesla
SET model='Model S', ev=true, price=99000;

CREATE car:jeep
SET model='Wrangler', ev=false, price=50000;

UPDATE human:chad SET cars = ['car:tesla', 'car:jeep'];
UPDATE car:tesla SET owner = human:chad;
UPDATE car:jeep SET owner = human:chad;

SELECT * FROM car WHERE owner == human:chad;
SELECT cars from human:chad;


CREATE part:tire SET brand='Michelin', size=5;
CREATE part:gastank SET brand='Tanksy', size=10;
CREATE part:battery SET brand='Xi Ping', size=20;

UPDATE car:jeep SET parts = ['part:tire', 'part:gastank'];
UPDATE car:tesla SET parts = ['part:tire', 'part:battery'];

SELECT parts from car:jeep;

SELECT FROM human:chad;


RELATE human:john ->drove->car:jeep SET when = time::now(), destination = 'the club';
RELATE human:chad ->drove->car:tesla SET when = time::now(), destination = 'the library';

SELECT ->drove->car FROM human;

SELECT <-drove<-human FROM car;

SELECT * from drove fetch in, out;