Difference between revisions of "Surreal DB"
(A few examples of SurrealDB syntax to experiment with) |
|||
Line 1: | Line 1: | ||
=== Surreal DB === | |||
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. <br> | |||
It is schemaless by default, implicitely creates tables and uses record links instead of foreign keys (RELATE keyword). <br> | |||
more info: https://surrealdb.com/ <br> | |||
=== INSTALL: === | === INSTALL: === | ||
==== Linux: ==== | ==== Linux: ==== |
Latest revision as of 10:24, 24 November 2022
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: https://surrealdb.com/
INSTALL:[edit]
Linux:[edit]
curl -sSf https://install.surrealdb.com | sh
macOS:[edit]
brew install surrealdb/tap/surreal
Windows: (powershell!)[edit]
iwr https://windows.surrealdb.com -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
Examples[edit]
RPG[edit]
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'];
One-To-One[edit]
UPDATE human:chad SET bff = human:john;
SELECT bff.name, bff.age FROM human:chad;
One-to-Many[edit]
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[edit]
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[edit]
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;