Skip to main content

Python SDK for mesahub — access SQLite databases from Python

Project description

MesaHub Python SDK

Python SDK for MesaHub — access SQLite databases from Python with raw SQL or a high-level table API.

Installation

pip install mesahub

Requires Python 3.11+ and httpx.

Quick Start

from mesahub import MesahubClient

client = MesahubClient(
    api_key="shs_your_api_key",       # from mesahub.app → Settings → API Keys
    api_url="https://api.mesahub.app", # or your self-hosted core URL
)

db    = client.db("my-app-db")        # your database slug from the dashboard
users = db.table("users")

# High-level table API
all_rows = users.find(where={"active": 1}, limit=20)
alice    = users.find_one(where={"email": "alice@example.com"})
count    = users.count(where={"active": 1})
new_row  = users.insert({"name": "Bob", "email": "bob@example.com", "active": 1})

users.update(where={"id": new_row["id"]}, set={"name": "Robert"})
users.delete(where={"id": new_row["id"]})

# Raw SQL
result = db.query("SELECT * FROM users WHERE active = ?", [1])
print(result["rows"])  # [{"id": 1, "name": "Alice", ...}]

db.exec("CREATE TABLE IF NOT EXISTS logs (msg TEXT, created_at TEXT)")

client.close()

Context manager

with MesahubClient(api_key="shs_...", api_url="https://api.mesahub.app") as client:
    db = client.db("my-app-db")
    rows = db.table("orders").find(where={"status": "pending"})

Connection String

from mesahub import MesahubClient, parse_mesahub_url

info   = parse_mesahub_url("mh://shs_abc@mycore.railway.app/mydb")
client = MesahubClient(
    api_key=info["api_key"],
    api_url=info["api_url"],
    route_prefix=info["route_prefix"],
)
db = client.db(info["db_name"])

Format: mh://apikey@host[:port]/dbname

  • Remote hosts (e.g. railway.app) → HTTPS, /v1/ routes
  • localhost / Docker service names / *.internal → HTTP, /api/ routes

WHERE Filters

# Shorthand equality
users.find(where={"active": 1})

# Comparison operators
users.find(where={"age": {"gte": 18}, "score": {"lt": 100}})

# LIKE
users.find(where={"name": {"like": "%alice%"}})

# IN / NOT IN
users.find(where={"role": {"in": ["admin", "editor"]}})

# NULL checks
users.find(where={"deleted_at": {"is_null": True}})
users.find(where={"verified_at": {"is_not_null": True}})

All conditions in a single where dict are combined with AND.

Operator key SQL equivalent
(plain value) = ?
eq = ?
ne != ?
gt > ?
gte >= ?
lt < ?
lte <= ?
like LIKE ?
not_like NOT LIKE ?
in IN (...)
not_in NOT IN (...)
is_null IS NULL
is_not_null IS NOT NULL

find() Options

users.find(
    where={"active": 1},
    select=["id", "name", "email"],
    order_by=[{"column": "created_at", "direction": "desc"}],
    limit=10,
    offset=20,
)

insert_many()

db.table("logs").insert_many(
    [{"msg": "started"}, {"msg": "done"}],
    on_conflict="ignore",  # "ignore" | "replace" | None
)

Files

files = db.files

# List
listing = files.list(limit=10, folder_prefix="reports/")

# Upload
record = files.upload(open("report.pdf", "rb").read(), "report.pdf", "application/pdf")
print(record["url"])

# Download
data = files.download(record["id"])

# Delete
files.delete(record["id"])

Error Handling

from mesahub import MesahubError, AuthenticationError, NotFoundError

try:
    db.query("SELECT * FROM users")
except AuthenticationError:
    print("Invalid API key")
except MesahubError as e:
    print(f"[{e.code}] {e.status_code}: {e}")

Self-Hosting

Replace api_url with your own core server URL and optionally set route_prefix="api" for direct Go access (bypassing Caddy):

client = MesahubClient(
    api_key="your-admin-token",
    api_url="http://localhost:4004",
    route_prefix="api",
)

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

mesahub-1.0.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mesahub-1.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file mesahub-1.0.0.tar.gz.

File metadata

  • Download URL: mesahub-1.0.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mesahub-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2314dd7adb699eba1a1bdb36ea426ea0910ec77a8ec7246950614bf3d7506556
MD5 f6a9ae284300146310892cf147b15527
BLAKE2b-256 a8d20c4b44d22fb32325783af80cdfe94619a8349747b9a28005979aa029ea07

See more details on using hashes here.

Provenance

The following attestation bundles were made for mesahub-1.0.0.tar.gz:

Publisher: publish.yml on mesahub-db/mesahub-pkg-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mesahub-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: mesahub-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mesahub-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c98599c2a692c4066b53dc1268c9efabd06957f36ad3691500d3b666426a32b
MD5 754c656e5619501139cdfbea3feef0cc
BLAKE2b-256 f6150148a8855eed83da1647d1662bac2c6e40fd960c51bae3bb3b4ea59f2da1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mesahub-1.0.0-py3-none-any.whl:

Publisher: publish.yml on mesahub-db/mesahub-pkg-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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