Surreal DB

From Coders.Bay Wiki
Revision as of 10:17, 24 November 2022 by 85.31.21.47 (talk) (A few examples of SurrealDB syntax to experiment with)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

INSTALL:

Linux:

curl -sSf https://install.surrealdb.com | sh

macOS:

brew install surrealdb/tap/surreal

Windows: (powershell!)

iwr https://windows.surrealdb.com -useb | iex

more info:

https://surrealdb.com/install

run server: (cmd)

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

run client (cmd - instead of postman or similar)

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

Examples

RPG

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

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

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

Embedded One-to-Many

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

One-To-One

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

One-to-Many

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;

Many-thru

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 cars.parts.brand FROM human:chad;

RECORDS

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;