Metadata-Version: 2.5
Name: tealdb
Version: 0.3.0
Summary: One consistent function-style API for many databases.
Project-URL: Homepage, https://octalynx.com
Author: octalynx
License: MIT
License-File: LICENSE
Keywords: database,duckdb,mongodb,redis,sql,tinydb,valkey
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: async
Requires-Dist: aiomysql>=0.2; extra == 'async'
Requires-Dist: aiosqlite>=0.20; extra == 'async'
Provides-Extra: async-mysql
Requires-Dist: aiomysql>=0.2; extra == 'async-mysql'
Provides-Extra: async-sqlite
Requires-Dist: aiosqlite>=0.20; extra == 'async-sqlite'
Provides-Extra: dev
Requires-Dist: aiomysql>=0.2; extra == 'dev'
Requires-Dist: aiosqlite>=0.20; extra == 'dev'
Requires-Dist: duckdb-engine>=0.13; extra == 'dev'
Requires-Dist: duckdb>=1.0; extra == 'dev'
Requires-Dist: fakeredis>=2.26; extra == 'dev'
Requires-Dist: greenlet>=3.0; extra == 'dev'
Requires-Dist: mongomock>=4.1; extra == 'dev'
Requires-Dist: mypy>=1.10.0; extra == 'dev'
Requires-Dist: psycopg[binary]>=3.1; extra == 'dev'
Requires-Dist: pymongo>=4.13; extra == 'dev'
Requires-Dist: pymysql>=1.1; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24.0; extra == 'dev'
Requires-Dist: pytest-cov>=5.0.0; extra == 'dev'
Requires-Dist: pytest>=8.2.0; extra == 'dev'
Requires-Dist: redis>=5.0.0; extra == 'dev'
Requires-Dist: ruff>=0.6.0; extra == 'dev'
Requires-Dist: sqlalchemy>=2.0.30; extra == 'dev'
Requires-Dist: sqlmodel>=0.0.22; extra == 'dev'
Requires-Dist: tinydb>=4.8.0; extra == 'dev'
Requires-Dist: valkey>=6.0.0; extra == 'dev'
Provides-Extra: duckdb
Requires-Dist: duckdb-engine>=0.13; extra == 'duckdb'
Requires-Dist: duckdb>=1.0; extra == 'duckdb'
Provides-Extra: mongo
Requires-Dist: pymongo>=4.13; extra == 'mongo'
Provides-Extra: mysql
Requires-Dist: aiomysql>=0.2; extra == 'mysql'
Requires-Dist: pymysql>=1.1; extra == 'mysql'
Provides-Extra: postgres
Requires-Dist: psycopg[binary]>=3.1; extra == 'postgres'
Provides-Extra: redis
Requires-Dist: redis>=5.0.0; extra == 'redis'
Provides-Extra: sql
Requires-Dist: aiosqlite>=0.20; extra == 'sql'
Requires-Dist: greenlet>=3.0; extra == 'sql'
Requires-Dist: sqlalchemy>=2.0.30; extra == 'sql'
Requires-Dist: sqlmodel>=0.0.22; extra == 'sql'
Provides-Extra: tiny
Requires-Dist: tinydb>=4.8.0; extra == 'tiny'
Provides-Extra: valkey
Requires-Dist: valkey>=6.0.0; extra == 'valkey'
Description-Content-Type: text/markdown

# 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]` |

```bash
pip install "tealdb[sql]"        # pick the extras you need
```

## Quick start

```python
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:

```python
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`:

```python
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)

```python
{"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)

```python
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)

```python
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

```bash
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
