Python
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"
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")
(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.
":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)
Print the result of the final query to the console:
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
localhost:3000.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