SOLID, extensible MongoDB client for Python using Motor (AsyncCollectionProtocol + RepositoryProtocol TCreate/TUpdate/TOut)
Project description
ildev-mongodb
SOLID, extensible MongoDB client for Python using Motor. Dict-based AsyncCollectionProtocol and RepositoryProtocol (TCreate/TUpdate/TOut + to_doc_create, to_doc_update, from_doc_out). Dependency Inversion and DI.
Requires: Python ≥3.10, Motor.
Install
pip install ildev-mongodb
Layers
- AsyncCollectionProtocol – base protocol,
document: dict[str, Any]. Full CRUD. - RepositoryProtocol[TCreate, TUpdate, TOut] – typed CRUD + explicit transforms:
to_doc_create,to_doc_update,from_doc_out. - BaseAsyncCollection – implements AsyncCollectionProtocol (dict-based).
- BaseTypedRepository – implements RepositoryProtocol; wraps AsyncCollectionProtocol and uses the three transforms.
Quick start (dict-based)
import asyncio
from ildev_mongodb import create_async_client
async def main():
async with create_async_client("mongodb://localhost:27017/") as client:
db = client.get_database("mydb")
coll = db.get_collection("items")
await coll.insert_one({"name": "test"})
doc = await coll.find_one({"name": "test"})
print(doc)
asyncio.run(main())
Quick start (typed TCreate/TUpdate/TOut)
import asyncio
from ildev_mongodb import create_async_client, BaseTypedRepository
class Item:
def __init__(self, name: str, value: int): ...
def to_doc(self): ...
@classmethod
def from_doc(cls, d: dict): ...
async def main():
async with create_async_client("mongodb://localhost:27017/") as client:
raw = client.get_database("mydb").get_collection("items")
items = BaseTypedRepository(
raw,
to_doc_create=lambda x: x.to_doc(),
to_doc_update=lambda u: u if isinstance(u, dict) else u.to_doc(),
from_doc_out=Item.from_doc,
)
await items.insert_one(Item("x", 1))
one = await items.find_one({"name": "x"})
await items.update_one({"name": "x"}, {"$set": {"value": 2}})
async for doc in items.find():
print(doc.name, doc.value)
asyncio.run(main())
Full CRUD
AsyncCollectionProtocol (dict-based): insert_one, insert_many, find_one, find, update_one, update_many, delete_one, delete_many, count_documents.
RepositoryProtocol[TCreate, TUpdate, TOut]: same CRUD with typed payloads; exposes to_doc_create, to_doc_update, from_doc_out and .collection.
BaseTypedRepository implements RepositoryProtocol by wrapping AsyncCollectionProtocol (e.g. BaseAsyncCollection) and the three transform callables.
Design (SOLID, DI)
- AsyncCollectionProtocol – dict-based collection; BaseAsyncCollection implements it.
- RepositoryProtocol – typed repository with explicit doc transforms; BaseTypedRepository implements it.
- create_async_client() – DI entry point; yields BaseAsyncClient.
Direct client construction
from ildev_mongodb import BaseAsyncClient
async def main():
client = BaseAsyncClient(uri="mongodb://localhost:27017/")
try:
db = client.get_database("mydb")
coll = db.get_collection("items")
# ...
finally:
await client.close()
PyPI
pip install build twine
python -m build
twine upload dist/*
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ildev_mongodb-0.1.0.tar.gz.
File metadata
- Download URL: ildev_mongodb-0.1.0.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c242dbbbf34dd6f52145667bb52498e862b68455eb44c0df5337d747422824dc
|
|
| MD5 |
51834a7d42a5082bcad2cab3d8feeee5
|
|
| BLAKE2b-256 |
abf1e7004cd6d440fb41a76b66cdeecfb0513697e3d4b1eef77fcf9805149dc3
|
File details
Details for the file ildev_mongodb-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ildev_mongodb-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b69c45c9355b2415141b7cb8a2c3134b74b802541221df8b9accd3d3af043734
|
|
| MD5 |
8099313ddfd03c30ab9b00b5eaedca26
|
|
| BLAKE2b-256 |
7f9daaed4b5d424f49af0c18aaf2958c0be88b3aee1dc79fc336d0974fc6dfe9
|