Examples

Server client - Python

View source
agdb example: managing users and databases and running queries on agdb_server from Python

What it shows

The Python version of the server client example using the agdb_api Python package. It checks the server status, logs in as the admin, creates a user and a memory mapped database and runs all queries in a single batch, linking the inserted nodes with references to the results of earlier queries in the same batch (:0, :1).

Run it

From the repository root (requires uv):

pnpm run examples --filter agdb_api_python

The script builds the agdb_server and runs the example against a freshly started server. The port 3000 must be free (stop any agdb_server you started yourself). Against another server set the AGDB_SERVER_ADDRESS environment variable (default localhost:3000).

To use the client in your own project, install it from the repository:

pip install "git+https://github.com/agnesoft/agdb#subdirectory=agdb_api/python"

See the Python client page for details.

Users and databases

import os

from agdb_api import AgdbApi, DbKind, QueryBuilder, UrllibClient

# Create the client using the `urllib` transport of the standard library.
client = AgdbApi.new(
    UrllibClient(), os.environ.get("AGDB_SERVER_ADDRESS", "localhost:3000")
)

# Runs the status query against the server and raises if it is not
# accessible.
client.status()

# Login as server admin.
client.user_login("admin", "admin")

# Create user "py_user1".
client.admin_user_add("py_user1", "py_user1")

# Login as "py_user1".
client.user_login("py_user1", "py_user1")

# Creates memory mapped database "db1" for user "py_user1".
client.db_add("py_user1", "db1", DbKind.Mapped)

One batch with result references

Within a batch a query can refer to the result of an earlier query by its index prefixed with a colon. The edges are inserted from the result of query :0 (the users root) to the result of query :1 (the user nodes):

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 with specific username.
    QueryBuilder.select()
    .search()
    .from_("users")
    .where()
    .key("username")
    .value("user1")
    .query(),
]

# Execute the queries.
_, results = client.db_exec_mut("py_user1", "db1", queries)

# Print the result of the last query.
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()}")

The client returns the HTTP status and the list of results, one per query, so results[3] is the result of the search. Names that are Python keywords get a trailing underscore (from_).

Learn more