Skip to main content

Simple lib that dynamically creates repositories for MongoDB

Project description

mongorepo

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

Example with BaseMongoRepository

from mongorepo.classes import BaseMongoRepository


def mongo_client(mongo_uri: str = 'mongodb://mongodb:27017/') -> pymongo.MongoClient:
    client: pymongo.MongoClient = pymongo.MongoClient(mongo_uri)
    return client


@dataclass
class UserDTO:
    username: str = ''
    password: str = ''


class SimpleMongoRepository(BaseMongoRepository[UserDTO]):
    ...


repo = SimpleMongoRepository(collection=mongo_client().users_db.users)

new_user = UserDTO(username='admin', password='1234')
repo.add(new_user)

user = repo.get(username='admin')
print(user)  # UserDTO(username='admin', password='1234')

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'])
class MongoRepository:
    class Meta:
        dto = Person
        collection = async_mongo_client().people_db.people


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 implements decorator

from mongorepo.implements import implements
from mongorepo.implements.methods import GetMethod, AddMethod


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


@implements(
    MessageRepository,
    GetMethod(MessageRepository.get_message, filters=['id']),
    AddMethod(MessageRepository.add_message, dto='message')
)
class MongoMessageRepository:
    class Meta:
        dto = Message
        collection = async_mongo_client().messages_db.messages


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-2.3.0.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

mongorepo-2.3.0-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mongorepo-2.3.0.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for mongorepo-2.3.0.tar.gz
Algorithm Hash digest
SHA256 0bcd373e3d02b25c4d9ef97e9733eda72139a30ad1cb51f262f56a682a3118ed
MD5 e01b3c97e55b11389df6b2bba40f9b74
BLAKE2b-256 94abd8b8796d61a3da1295a153d3679e9947ed8ea2fcbec81412d1a7b2098767

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mongorepo-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for mongorepo-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a3ef01d07a1fe835fa6737328dfe7e5362218a68660294125d9eb48be47a6b5
MD5 290946d5866fed1de3d78ac6f3621f68
BLAKE2b-256 1d05df54cf6f324c238252befeb4af15c5b7e1070928c776c2d25a34c2c17679

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