Skip to main content

SurrealDB python client

Project description


 

The official SurrealDB SDK for Python.


       

     

surrealdb.py

The official SurrealDB SDK for Python.

Documentation

View the SDK documentation here.

How to install

pip install surrealdb

Quick start

In this short guide, you will learn how to install, import, and initialize the SDK, as well as perform the basic data manipulation queries. This guide uses the Surreal class, but this example would also work with AsyncSurreal class, with the addition of await in front of the class methods.

Install

pip install surrealdb

Learn the basics

# Import the Surreal class
from surrealdb import Surreal

# Using a context manger to automatically connect and disconnect
with Surreal("ws://localhost:8000/rpc") as db:
    db.signin({"username": 'root', "password": 'root'})
    db.use("namepace_test", "database_test")

    # Create a record in the person table
    db.create(
        "person",
        {
            "user": "me",
            "password": "safe",
            "marketing": True,
            "tags": ["python", "documentation"],
        },
    )

    # Read all the records in the table
    print(db.select("person"))

    # Update all records in the table
    print(db.update("person", {
        "user":"you",
        "password":"very_safe",
        "marketing": False,
        "tags": ["Awesome"]
    }))

    # Delete all records in the table
    print(db.delete("person"))

    # You can also use the query method 
    # doing all of the above and more in SurrealQl
    
    # In SurrealQL you can do a direct insert 
    # and the table will be created if it doesn't exist
    
    # Create
    db.query("""
    insert into person {
        user: 'me',
        password: 'very_safe',
        tags: ['python', 'documentation']
    };
    """)

    # Read
    print(db.query("select * from person"))
    
    # Update
    print(db.query("""
    update person content {
        user: 'you',
        password: 'more_safe',
        tags: ['awesome']
    };
    """))

    # Delete
    print(db.query("delete person"))

Next steps

Now that you have learned the basics of the SurrealDB SDK for Python, you can learn more about the SDK and its methods in the methods section and data types section.

Contributing

Contributions to this library are welcome! If you encounter issues, have feature requests, or want to make improvements, feel free to open issues or submit pull requests.

If you want to contribute to the Github repo please read the general contributing guidelines on concepts such as how to create a pull requests here.

Getting the repo up and running

To contribute, it's a good idea to get the repo up and running first. We can do this by running the tests. If the tests pass, your PYTHONPATH works and the client is making successful calls to the database. To do this we must run the database with the following command:

# if the docker-compose binary is installed
docker-compose up -d

# if you are running docker compose directly through docker
docker compose up -d

Now that the database is running, we can enter a terminal session with all the requirements installed and PYTHONPATH configured with the command below:

bash scripts/term.sh

You will now be running an interactive terminal through a python virtual environment with all the dependencies installed. We can now run the tests with the following command:

python -m unittest discover

The number of tests might increase but at the time of writing this you should get a printout like the one below:

.........................................................................................................................................Error in live subscription: sent 1000 (OK); no close frame received
..........................................................................................
----------------------------------------------------------------------
Ran 227 tests in 6.313s

OK

Finally, we clean up the database with the command below:

# if the docker-compose binary is installed
docker-compose down

# if you are running docker compose directly through docker
docker compose down

To exit the terminal session merely execute the following command:

exit

And there we have it, our tests are passing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

surrealdb-1.0.4.tar.gz (47.9 kB view details)

Uploaded Source

Built Distribution

surrealdb-1.0.4-py3-none-any.whl (58.9 kB view details)

Uploaded Python 3

File details

Details for the file surrealdb-1.0.4.tar.gz.

File metadata

  • Download URL: surrealdb-1.0.4.tar.gz
  • Upload date:
  • Size: 47.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.3.0

File hashes

Hashes for surrealdb-1.0.4.tar.gz
Algorithm Hash digest
SHA256 f582bb78784c142c7432d743c6fa60e2875a081537087dea8bfb5b1a468864f1
MD5 17dddf2934ba40807a58f30ce13509c9
BLAKE2b-256 bfeca9defd7b7bd36d71181b7081d9f3929f988aa66836874e7c8c1b89e2d582

See more details on using hashes here.

File details

Details for the file surrealdb-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: surrealdb-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 58.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.3.0

File hashes

Hashes for surrealdb-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c617f572addd1e004e5ed83517518b6a83d0ba7f87f19e18c11e22fa96bf95e
MD5 56ea9a919652b1648aab8eb93a08e3fd
BLAKE2b-256 b1d41650a734206bfdfb112fb14db8ffaf3195385dd0fb8534396dbdb1814d22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page