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.

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.

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.2.2

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.2.2
File Size Uploaded Provenance Actions
tealdb-0.2.2.tar.gz 232.5 kB Without provenance Details for tealdb-0.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 383.1 kB

Release files / tealdb-0.2.2.tar.gz

Download URL tealdb-0.2.2.tar.gz
Size 232.5 kB
Tags Source
SHA-256 checksum
How to use checksums
259486a45a5924c6f5bc63c8ee0183dfaca23c8753097085cfabc5f55a75e62f
BLAKE2b-256 checksum
How to use checksums
7191ccb02ec673bd61c22ff4cd8539cbc6ee858a6acd159dcf660ca2767e85b3
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.2.2-py3-none-any.whl

Download URL tealdb-0.2.2-py3-none-any.whl
Size 150.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee7cfda00af25748520b3aae2b676bd53d3af45c8ea7795a3e10c741442a35b0
BLAKE2b-256 checksum
How to use checksums
b2a20669d3740991885ba7a9d98b8b04c8bee4ad98e9ea65f4c50c3b384bc041
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.2.2

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