Surreal DB, The ultimate cloud database.

Sudirsha
featurepreneur
Published in
5 min readSep 26, 2022

With an SQL-style query language, real-time queries with highly-efficient related data retrieval, advanced security permissions for multi-tenant access, and support for performant analytical workloads, SurrealDB is the next generation serverless database.

Why use SurrealDB?

SurrealDB is an innovative NewSQL cloud database, suitable for serverless applications, jamstack applications, single-page applications, and traditional applications. It is unmatched in its versatility and financial value, with the ability for deployment on cloud, on-premise, embedded, and edge computing environments.

Installation

Install on macOS

The quickest way to get going with SurrealDB on macOS is to use Homebrew. This will install both the command-line tools, and the SurrealDB server as a single executable. If you don’t use Homebrew, follow the instructions for Linux below to install SurrealDB.

brew install surrealdb/tap/surreal

Install on Linux

The easiest and preferred way to get going with SurrealDB on Unix operating systems is to install and use the SurrealDB command-line tool. Run the following command in your terminal and follow the on-screen instructions.

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

Run using Docker

Docker can be used to manage and run SurrealDB database instances without the need to install any command-line tools. The SurrealDB docker container contains the full command-line tools for importing and exporting data from a running server, or for running a server itself.

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

How to Run?

First of all start the server

  1. Use server start
surreal start --log debug --user root --pass root memory

Or If you have installed using docker start the container

docker start surrealdb/surrealdb

Once you have started the DB there are several ways to interact with the DB.

  1. HTTP Client.
  2. CLI
  3. Client or Server Side Libraries.

To check out the supported libraries visit https://surrealdb.com/docs/integration/libraries

Now that we have completed the basic setup let’s try to test out Surreal Queries.

In this article, I’m going to use CLI tool to query into DB.

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

To start the CLI tool enter above command in new terminal.

here the username=root, password=root, namespace=test, dbname=test.

This command will open a live interpreter to execute queries.

SurrealQL

Let’s create records the first here I am going to create two Employee records with E24,E25.

CREATE EMPLOYEE:E24 SET name = 'John', dept = 'Sales';CREATE EMPLOYEE:E25 SET name = 'Doe', dept = 'HR';

It’ll create 2 records of Employee with ID E24 and E25 and it’s quite simple.

Similar to Sql SELECT can be used to query result from Db

SELECT * FROM EMPLOYEE;

This will return all records of Employees.

If you want to select a particular record.

Similar to SQL it supports WHERE clause and other comparison operators as well.All CRUD commands work quite similar to normal

Now let’s try some interesting features

Storing record links within records

Records ids can be stored directly within other records, either as top-level properties, or nested within objects or arrays.

CREATE student:jamie SET name = 'Jaime', friends = [student:tobie, person:simon];
CREATE student:tobie SET name = 'Tobie', friends = [student:simon, person:marcus];
CREATE student:simon SET name = 'Simon', friends = [student:jaime, person:tobie];
CREATE student:marcus SET name = 'Marcus', friends = [student:tobie];

Here as you can see we have links to other records as well.

And one other interesting aspect of this DB is that you can use ‘.’ to access members of a record .

SELECT friends.friends.friends.name FROM student:tobie;

To know more about the data types available in SurrealDB refer

It supports basic types like null, booleans, strings to complex types like objects, Arrays, etc...

There is one more interesting feature in DB Future check out more about it https://surrealdb.com/docs/surrealql/datamodel/futures

This is one of the most unique features in surrealDb is avoiding join by interrelation of tables.

Tables, documents, and graphs. Store and model your data in any way

Inter-document record links allow for simple to understand and highly-performant related queries without the use of JOINs, eliminating the N+1 query problem.

Let’s look at an example

CREATE food:biriyani SET price=200.00, shop='restaurant1';
CREATE food:friedrice SET price=150.00, shop='restaurant2';

Let’s create relation among these records student & food.

RELATE student:jamie->ate->food:biriyani SET when=time::now();
RELATE student:marcus ->ate->food:friedrice SET when = time::now();

It’s time to query

select ->ate->food from student:marcus;

The retrieval can be done in reverse order as well for example let’s try to fetch the student who ate fried rice and biriyani.

let’s relate one more food to marcus.

RELATE student:marcus ->ate->food:biriyani SET when = time::now();

Now if we query students who ate fried rice and biriyani

select <-ate<-student from food:biriyani AND food:friedrice;

And that’s how this works.Since we can relate records we don’t need joins.

How can you implement SurrealDb

There are several other ways to utilize surrealDb like javascript webkit api, client libraries or if you want to use cli tool you can execute a .sql file using

cat myfile.sql | surreal sql --conn http://localhost:8000 --user root --pass root --ns test --db test

Conclusion

SurrealDb is quite now and improving each and every day, it’s built with rust one of the important reason for it’s super fast query time which can be witnessed from the screenshot’s time stamps. Even though the library support for popular programming languages isn’t available as of now. It looks quite promising due to its features, speed and simplicity. I couldn’t address all the features of surrealDB in this article feel free to checkout

for features and

to deep dive into SurrealQL.

Thanks for reading.

--

--