Skip to content

Instantly share code, notes, and snippets.

@janpauldahlke
Created November 7, 2022 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janpauldahlke/583e2dcf3bd43a4e09069c39e2bdd74b to your computer and use it in GitHub Desktop.
Save janpauldahlke/583e2dcf3bd43a4e09069c39e2bdd74b to your computer and use it in GitHub Desktop.
surrealDB CLI

hands on surrealdb

prequisites

install surreal:

sudo curl -sSf https://install.surrealdb.com | sh
rustc 1.64.0 (a55dd71d5 2022-09-19)

alternative is docker alike:

podman run --rm -p 8000:8000 surrealdb/surrealdb:latest start

ensure exposing surreal to $path or mv into bin!

sudo mv *******/home/janpaul/.surrealdb/surreal /usr/local/bin

app.js contains an example how to consume the js client

Access surrealDB CLI like this

credit to CodeToTheMoon

startup:

surreal start --log debug --user root --pass root memory
surreal sql --conn http://localhost:8000 --user root --pass root --ns test --db test --pretty

do some commands to verify,

SELECT * FROM player;
SELECT * FROM armor; 

getting data of linked armor by using fetch

SELECT * FROM player FETCH armor;

WHERE

SELECT * FROM player WHERE luck > 3

question how would be acutall WHERE on FETCH, SELECT * FROM player WHERE luck > 0 FETCH armor WHERE resistance > 10 is not quite working

adding up all values of a column like this

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

graphs and edges

creates a relationship that tells player hero also wants to buy mithril RELATE player:hero->wants_to_buy->armor:mithril RELATE player:hagbard->wants_to_buy->armor:mithril

selecting edge

SELECT * FROM wants_to_buy;

!not working

SELECT id, ->wants_to_buy->armor:mithril as wtf FROM player;

!also not working

SELECT id, <-want_to_buy<-player as players from armor:mithril

we could add another field to the player table with

UPDATE player SET level = 5;

BUT we created our table schemaful and now we can not add
we would need to run

DEFINE TABLE player SCHEMALESS;

event tables, kinda db event listeners

DEFINE EVENT lvl_up ON TABLE player WHEN $before.level < $after.level THEN ( CREATE lvl_up SET time = time::now(), level = $after.level, player = player.id)

trigger event by

UPDATE player:Hagbard SET levl = 7;

querry result now and enjoy

SELECT * from lvl_up;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment