Skip to main content

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

  1. AsyncCollectionProtocol – base protocol, document: dict[str, Any]. Full CRUD.
  2. RepositoryProtocol[TCreate, TUpdate, TOut] – typed CRUD + explicit transforms: to_doc_create, to_doc_update, from_doc_out.
  3. BaseAsyncCollection – implements AsyncCollectionProtocol (dict-based).
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ildev_mongodb-0.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

ildev_mongodb-0.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

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

Hashes for ildev_mongodb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c242dbbbf34dd6f52145667bb52498e862b68455eb44c0df5337d747422824dc
MD5 51834a7d42a5082bcad2cab3d8feeee5
BLAKE2b-256 abf1e7004cd6d440fb41a76b66cdeecfb0513697e3d4b1eef77fcf9805149dc3

See more details on using hashes here.

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

Hashes for ildev_mongodb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b69c45c9355b2415141b7cb8a2c3134b74b802541221df8b9accd3d3af043734
MD5 8099313ddfd03c30ab9b00b5eaedca26
BLAKE2b-256 7f9daaed4b5d424f49af0c18aaf2958c0be88b3aee1dc79fc336d0974fc6dfe9

See more details on using hashes here.

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