Exploring the Basics of SurrealDB: A Modern Database Solution Link to heading

SurrealDB is an emerging database technology that aims to simplify data storage and retrieval. It combines the flexibility of document stores with the power of graph databases, making it a versatile solution for modern applications. In this post, we’ll dive deep into the fundamentals of SurrealDB, explore its features, and provide practical code examples to get you started.

What is SurrealDB? Link to heading

SurrealDB is an innovative database designed to handle complex data relationships and queries efficiently. It supports a variety of data models, including documents, graphs, and key-value pairs, providing a unified interface for developers. This flexibility enables SurrealDB to cater to a wide range of applications, from simple CRUD operations to complex data analysis.

Key Features of SurrealDB Link to heading

  1. Multi-Model Support: SurrealDB supports document, graph, and key-value data models, allowing you to choose the best model for your use case.
  2. Schema-less Design: You can store data without defining a schema upfront, making it easier to adapt to changing requirements.
  3. Rich Query Language: SurrealDB offers a powerful query language that supports complex queries, including joins, aggregations, and traversals.
  4. Scalability: It is designed to scale horizontally, ensuring performance remains consistent as your data grows.
  5. ACID Transactions: SurrealDB supports transactions, ensuring data integrity and consistency.

Getting Started with SurrealDB Link to heading

Before diving into code examples, let’s set up SurrealDB. You can follow the official installation guide to install SurrealDB on your machine. Once installed, you can start the database server using the following command:

surreal start

Connecting to SurrealDB Link to heading

SurrealDB provides client libraries for various programming languages. For this example, we’ll use Python. First, install the SurrealDB Python client:

pip install surrealdb

Next, create a Python script to connect to the database:

from surrealdb import Surreal

async def main():
    db = await Surreal.connect("http://localhost:8000")
    await db.signin({"user": "root", "pass": "root"})
    await db.use("test", "test")

import asyncio
asyncio.run(main())

This script connects to the SurrealDB server running on localhost:8000 and signs in with the default credentials.

Working with Data in SurrealDB Link to heading

Creating and Retrieving Documents Link to heading

SurrealDB allows you to store documents without defining a schema. Here’s how you can create and retrieve documents:

async def create_document(db):
    await db.create("person", {
        "name": "Alice",
        "age": 30,
        "email": "alice@example.com"
    })

async def get_document(db):
    person = await db.select("person")
    print(person)

asyncio.run(create_document(db))
asyncio.run(get_document(db))

Updating and Deleting Documents Link to heading

You can update and delete documents using the update and delete methods:

async def update_document(db):
    await db.update("person", {
        "name": "Alice",
        "age": 31,
        "email": "alice@example.com"
    })

async def delete_document(db):
    await db.delete("person")

asyncio.run(update_document(db))
asyncio.run(delete_document(db))

Working with Graphs Link to heading

SurrealDB’s graph capabilities allow you to model complex relationships between entities. Here’s an example of creating and querying a graph:

async def create_graph(db):
    await db.create("person", {
        "name": "Bob",
        "friends": [
            {"name": "Charlie", "age": 25},
            {"name": "David", "age": 28}
        ]
    })

async def query_graph(db):
    friends = await db.query("SELECT * FROM person WHERE name='Bob'->friends")
    print(friends)

asyncio.run(create_graph(db))
asyncio.run(query_graph(db))

Advanced Queries Link to heading

SurrealDB’s query language supports advanced operations like joins and aggregations. Here’s an example:

async def advanced_query(db):
    result = await db.query("""
        SELECT person.name, COUNT(friend) AS num_friends
        FROM person
        LET friend = person->friends
        GROUP BY person.name
    """)
    print(result)

asyncio.run(advanced_query(db))

Conclusion Link to heading

SurrealDB is a powerful and flexible database solution that combines the best features of document and graph databases. Its schema-less design, rich query language, and scalability make it an ideal choice for modern applications. We hope this guide has provided you with a solid foundation to start exploring SurrealDB.

For further reading, you can check out the official SurrealDB documentation and explore more advanced features and use cases.

SurrealDB Logo

References Link to heading

  1. SurrealDB Official Documentation
  2. SurrealDB GitHub Repository