Skip to main content

Simple library that dynamically creates repositories for MongoDB

Project description

mongorepo

Simple lib for python & mongodb, provides auto repository factory based on Entity type

Example with mongorepo.async_repository

import mongorepo


def async_mongo_client(mongo_uri: str = 'mongodb://mongodb:27017/') -> AsyncIOMotorClient:
    async_client = AsyncIOMotorClient(mongo_uri)
    return async_client


@dataclass
class Person:
    id: str
    name: str
    skills: list[str] = field(default_factory=list)    


@mongorepo.async_repository(
    array_fields=['skills'],
    config=RepositoryConfig(entity_type=Person, collection=async_mongo_client().people_db.people)
)
class MongoRepository:
    ...


repo = MongoRepository()


admin = Person(id='289083', name='admin', skills=['python', 'c++', 'java', 'rust'])

await repo.add(admin)
await repo.skills__append('c', id='289083')
await repo.skills__remove('python', id='289083')

user = await repo.get(id='289083')
print(user.skills)  # ['c++', 'java', 'rust', 'c']

Example with implement decorator

from mongorepo.implement import implement
from mongorepo.implement.methods import GetMethod, AddMethod
from mongorepo import use_collection


def async_mongo_client(mongo_uri: str = 'mongodb://mongodb:27017/') -> AsyncIOMotorClient:
    async_client = AsyncIOMotorClient(mongo_uri)
    return async_client


@dataclass
class Author:
    name: str


@dataclass
class Message:
    id: str
    body: str
    author: Author


class MessageRepository(typing.Protocol):
    async def add_message(self, message: Message):
        ...

    async def get_message(self, id: str) -> Message | None:
        ...


@implement(
    GetMethod(MessageRepository.get_message, filters=['id']),
    AddMethod(MessageRepository.add_message, entity='message'),
    config=RepositoryConfig(entity_type=Message, collection=async_mongo_client().messages_db.messages)
)
class MongoMessageRepository:
    ...

repo: MessageRepository = MongoMessageRepository()


await repo.add_message(
    message=Message(id='1', body='hello', author=Author(name='friend'))
)

message = await repo.get_message(id='1')
print(message)  # Message(id='1', body='hello', author=Author(name='friend'))

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

mongorepo-4.0.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

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

mongorepo-4.0.0-py3-none-any.whl (35.2 kB view details)

Uploaded Python 3

File details

Details for the file mongorepo-4.0.0.tar.gz.

File metadata

  • Download URL: mongorepo-4.0.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.12.13 Linux/6.17.0-1010-azure

File hashes

Hashes for mongorepo-4.0.0.tar.gz
Algorithm Hash digest
SHA256 db08a17d5bed2e2839b70a1e013ded500712eb729658efa9d85e8f1227c3213f
MD5 6edfafe25ff18fc6eeb20f4fb034b1bb
BLAKE2b-256 050f38f719553f371d8369764e8c744449ab447cc48dd62836ad3b893fd10691

See more details on using hashes here.

File details

Details for the file mongorepo-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: mongorepo-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 35.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.12.13 Linux/6.17.0-1010-azure

File hashes

Hashes for mongorepo-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 304b5557510f5904b0cf050df02071a26b9f638fef55a2c34bb30224981b8771
MD5 52f65ea8249182301d34954c1effbca8
BLAKE2b-256 c9a15237647264127e823ad974a063656b0a3ae34ac92f1d0dee5268bb6c736b

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