Api Docs

Python

Python client generated from the Rust client, with usage examples

The python agdb API client is generated from the Rust client (agdb_api) and its query builder from the Rust one (agdb), so both have the same methods. It requires Python 3.11 or later, has no dependencies and sends the requests with urllib of the standard library. The following is the quickstart guide for the agdb client in Python (connecting to the server). It assumes an agdb_server is running locally. Please refer to the server guide to learn how to run the server.

Looking for... how to run a server? | another language? | embedded db guide?

Usage

The following is the from-scratch guide to use agdb_api python package.

Install Python

https://www.python.org/downloads/

Create your project

Let's create a directory (e.g. my_agdb) with a virtual environment:

mkdir my_agdb
cd my_agdb
python -m venv .venv
source .venv/bin/activate

Add agdb_api as a dependency.

pip install "git+https://github.com/agnesoft/agdb#subdirectory=agdb_api/python"
The package is fully typed, consider checking your code with mypy and formatting it with ruff.

Create a client

In your main script (e.g. main.py) create a client connecting to the server:

from agdb_api import AgdbApi, DbKind, QueryBuilder, UrllibClient


def main() -> None:
    # Creates a client connecting to the remote server.
    client = AgdbApi.new(UrllibClient(), "localhost:3000")
Each method takes the parameters of the Rust client, by their names, and returns the status code, with the response body if there is one ((status, body)). A response that is not a success raises an AgdbApiError holding the status and the description. The names that are Python keywords get a trailing _, e.g. from_.

Create a database user

To create a database user we use the default admin user:

client.user_login("admin", "admin")
client.admin_user_add("user1", "password123")
client.user_login("user1", "password123")

Create a database

client.db_add("user1", "db1", DbKind.Mapped)  # memory mapped type, other options are DbKind.Memory and DbKind.File

Execute queries

To execute queries against the database we call db_exec (read only queries) and db_exec_mut (for queries that also write to the database) with the user and their database.

Notice we are feeding results of the previous query to the next one with special alias ":0" and ":1" referencing first and second result respectively.
# Prepare the queries to be executed on the remote database.
queries = [
    # :0: Inserts a root node aliased "users"
    QueryBuilder.insert().nodes().aliases(["users"]).query(),
    # :1: Inserts more nodes with some data
    QueryBuilder.insert()
    .nodes()
    .values(
        [
            {"username": "user1", "password": "password123"},
            {"username": "user2", "password": "password456"},
        ]
    )
    .query(),
    # :2: Connect the root to the inserted nodes with edges referencing both from previous queries
    QueryBuilder.insert().edges().from_(":0").to(":1").query(),
    # :3: Find a node starting at the "users" node (could also be ":0" in this instance) with specific username
    QueryBuilder.select()
    .search()
    .from_("users")
    .where()
    .key("username")
    .value("user1")
    .query(),
]

# Execute queries.
_, results = client.db_exec_mut("user1", "db1", queries)
user = results[3].elements[0]
print(f"User (id: {user.id})")

for kv in user.values:
    print(f"{kv.key.to_json()}: {kv.value.to_json()}")

Run the program

Make sure the agdb_server is running at localhost:3000.
To run the example of this repository against a fresh server, call pnpm run examples --filter agdb_api_python from the root.
python main.py

Full program

https://github.com/agnesoft/agdb/tree/main/agdb_api/python/examples/server_client