hands on surrealdb
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
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;
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;