Skip to main content

Tealdb

One consistent, function-style API for several databases, with sync and async versions. Python 3.10+. Backends are optional extras and are imported lazily.

Backend Engines Extra
sqltools SQLite, PostgreSQL, MySQL/MariaDB, DuckDB (via SQLAlchemy) tealdb[sql] (+ postgres, mysql, duckdb)
mango MongoDB tealdb[mongo]
tiny TinyDB (JSON file or memory) tealdb[tiny]
redis Redis tealdb[redis]
valkey Valkey tealdb[valkey]
pip install "tealdb[sql]"        # pick the extras you need

Quick start

import tealdb

db = tealdb.connect("sqlite:///app.db")        # backend chosen from the URL scheme
sql = tealdb.backend(db)                        # the backend module for this handle

sql.create(db, "users", {"id": "int", "name": "text", "age": "int"})
sql.add(db, "users", {"id": 1, "name": "Ann", "age": 30})
sql.find(db, "users", {"age": {"gte": 18}}, sort=["name"], limit=10)
sql.update(db, "users", {"name": "Ann"}, {"age": 31})
sql.delete(db, "users", {"name": "Ann"})

with sql.transaction(db) as tx:                 # a transaction is accepted wherever a db is
    sql.add(tx, "users", {"id": 2, "name": "Bob", "age": 25})

tealdb.close(db)

You can also import a backend directly:

from tealdb import sqltools, mango, valkey, redis, tiny        # sync
from tealdb.aio import sqltools, mango, valkey, redis, tiny    # async, same names

Async works the same way, with await:

import tealdb.aio as aio

db = await aio.connect("sqlite+aiosqlite:///app.db")
sql = aio.backend(db)
async with sql.transaction(db) as tx:
    await sql.add(tx, "users", {"id": 1, "name": "Ann", "age": 30})
await aio.close(db)

Connection URLs: sqlite:///file.db, postgresql://user:pw@host/db, mysql://..., duckdb:///file.duckdb, mongodb://host/dbname, tiny:///file.json, redis://host:6379/0, valkey://host:6379/0. You can also pass a ConnectionConfig, or call connect() with no arguments to read TEALDB_URL (or TEALDB_SCHEME, TEALDB_HOST, ...). Backend options go as keyword arguments, e.g. tealdb.connect("redis://...", prefix="app:").

Filters (shared by sqltools, mango and tiny)

{"age": 30}                        # equality
{"age": {"gte": 18, "lt": 65}}     # eq ne gt gte lt lte in not_in exists regex contains icontains
{"or": [{"age": {"lt": 18}}, {"city": None}]}   # or / and / not combine filters
{"name": {"in": ["Ann", "Bob"]}}

Rows are plain dicts. Results of writes are InsertResult / WriteResult.

Key/value backends (redis, valkey)

from tealdb import redis
db = redis.connect("redis://localhost:6379/0", prefix="app:")
redis.set(db, "user:1", {"name": "Ann"}, ttl=60)     # values are JSON (never pickle)
redis.get(db, "user:1"); redis.incr(db, "visits")
redis.hset(db, "h", "field", 1); redis.list_push(db, "q", 1, 2); redis.set_add(db, "s", "a")
redis.delete_all(db, confirm=True)                    # only keys under your prefix
with redis.transaction(db) as tx:                     # queued, atomic; reads are refused inside
    redis.set(tx, "a", 1)

Safety

  • Errors never contain passwords, full URLs, SQL, values or driver text, and the original driver exception is not reachable through __context__.
  • Passwords are masked in repr/logs (Secret). There are no plaintext password helpers.
  • Identifiers and field names are validated; values are always bound as parameters.
  • Destructive operations (delete_all, ...) require confirm=True.
  • Mongo $where/$function and server-side writes in aggregation are refused by default.

SQL extras (sqltools)

from tealdb import UniqueViolation, sqltools as sql

sql.migrate(db, "migrations")                    # numbered .sql files, applied once, checksummed
page = sql.page(db, "orders", {"state": "open"}, number=2, size=25)   # rows + total (also mango, tiny)

with sql.transaction(db) as tx:
    stock = sql.find_one(tx, "stock", {"product_id": 7}, for_update=True)   # row lock
    ...

try:
    sql.add(db, "users", {"email": email})
except UniqueViolation as e:                     # e.constraint, e.table, e.columns (never values)
    ...

stop = sql.on_query(db, slow=0.5)                # log statements slower than 0.5 s

Also transaction(db, read_only=True, isolation="repeatable read"), savepoints with a nested transaction(tx), run_script(db, text) for trusted multi-statement SQL, and the command line tealdb migrate FOLDER [--status] (URL from TEALDB_URL).

Engine notes

  • MySQL/MariaDB DDL commits implicitly, so schema changes (rename_table, ...) are refused inside a transaction.
  • On MariaDB, json columns hold text (MariaDB stores JSON as LONGTEXT); passing a dict/list is refused.
  • MySQL decimal columns default to DECIMAL(38,10) so fractions are kept.

Errors

TealError is the base class; also ConnectionFailed, NotFound, InvalidQuery, UnsupportedOperation, ConstraintViolation (with UniqueViolation, ForeignKeyViolation, CheckViolation, NotNullViolation), SerializationFailure (retry the transaction), Busy and MigrationError. Errors raised at COMMIT are classified the same way. Messages never contain SQL, values or passwords. See CHANGES.md for what changed between versions.

Development

pip install -e ".[dev]"
ruff check src tests && mypy src/tealdb && pytest

Optional real-server tests: set TEALDB_TEST_POSTGRES_URL, TEALDB_TEST_MYSQL_URL, TEALDB_TEST_MONGO_URL, TEALDB_TEST_REDIS_URL, TEALDB_TEST_VALKEY_URL (database names must contain "test"; tests only touch what they create).

License

MIT

Metadata

Release files for tealdb 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tealdb 0.3.0
File Size Uploaded Provenance Actions
tealdb-0.3.0.tar.gz 307.1 kB Without provenance Details for tealdb-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tealdb 0.3.0
File Interpreter ABI Platform Actions
tealdb-0.3.0-py3-none-any.whl Python 3 none any Details for tealdb-0.3.0-py3-none-any.whl

Total release size: 482.6 kB

Release files / tealdb-0.3.0.tar.gz

Download URL tealdb-0.3.0.tar.gz
Size 307.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b9d14ca9ecd53281fb8fbcc7206bc32120e74d2263676c5039636e74425d4261
BLAKE2b-256 checksum
How to use checksums
a4f7ee48e504dfcec89c6ceaadd86d2522e1b3ff700e43ef7ace603bf086c66e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / tealdb-0.3.0-py3-none-any.whl

Download URL tealdb-0.3.0-py3-none-any.whl
Size 175.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9361a867c6f505ef8dbf4cd98a5f55c7f0c9ad89c79cb69ac1d5f23404064b4
BLAKE2b-256 checksum
How to use checksums
4764c0ed7b0f0f496b7946155af14614b6c76d3887a4c9e9b51e2b9f62691cb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for tealdb 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page