My experience with SurrealDB

  • 2024-03-19 21:00:13.110593
  • Databases
  • 398 Hits

 I was looking for a fresh type of db for my personal website and surprisingly discovered Surrealdb, yes this project uses Surrealdb but it runs on the disk instead of in memory its still really fast,

and it did save me a lot of time figuring out tables and types you can build your database along your app as you go,

adding new fields when necessary without having to run alter tables and waste time figuring what type and size of that type to use,

so far my experience with Surrealdb is very positive and the community on github seems pretty alive.

The installation processes was quick and easy all there was needed was one command check the installation on their website https://surrealdb.com/install Installation commands:


macOS:

brew install surrealdb/tap/surreal


Linux:

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


 Windows (powershell):

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


 Start surreal to store data in the disk: *After the two // paste your directory like


 Start surreal to store data in file: 

surreal start --log debug --user root --pass root file://PATH_TO_FOLDER


 Start surreal to store data in memory: 

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


 storing data memory is a good way to be used as cache once you stop it though your data disappears use postman or use curl to interact with the database  curl --request POST --header "Accept: application/json" --header "NS: test" --header "DB: test" --user "root:root" --data "YOUR QUERY HERE" http://localhost:8000/sql



 A simple example on how to use the database 

CREATE gym:uk SET name = "UniverseFitness UK", craeted_at = time::now();


 gym is the table name uk is the id you can also just say gym without the :uk then the id will be generated automaticlly 


CREATE gym:usa SET name = "UniverseFitness USA", craeted_at = time::now();


 now select the gym table with

SELECT * FROM gym;


 now lets add people to our gym 

CREATE persons:martin SET name = "martin", craeted_at = time::now();
CREATE persons:kirilov SET name = "kirilov", craeted_at = time::now();

 lets select our users 

SELECT * FROM persons;

 assign a person to a specific gym

UPDATE persons:martin SET gym_member = gym:uk;



 assign a user to multiple gyms

 UPDATE persons:kirilov SET gym_member = ["gym:uk","gym:usa"];

 now we have related both of the tables and can simply run a select statement to bring the gym_member 

SELECT gym_member.name FROM persons:martin;
SELECT gym_member.name FROM persons:kirilov;


 Now lets delete on of our persons

DELETE FROM persons WHERE name = "martin";


 SELECT * FROM persons;


 With that you got a taste on how things work but beware that is still a beta project Update: 12/18/2022


Had to use a secondary database but the port was already in use so an exception was raised "thread main panicked at error binding to 0.0.0.0:8000: error creating server listener: Only one usage of each socket address (protocol/network address/port) is normally permitted." So to fix the above error you just have to --bind it to another port like that: surreal start --bind="127.0.0.1:8001" --log debug --user root --pass root file://PATH_TO_FOLDER



 Im gonna keep updating this post with new interesting stuff I find along the way.