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 class

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 decorator

from mongorepo.asyncio.decorators import async_mongo_repository


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)    


@async_mongo_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']

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

Uploaded Source

Built Distribution

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

mongorepo-0.2.2-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mongorepo-0.2.2.tar.gz
Algorithm Hash digest
SHA256 eed300a8445f60d41849f4ba48825ed4e2d3beefc7dfe059fb5a3d0b3c492a42
MD5 09dc9641c1fd2c883f9f94b313d0acc7
BLAKE2b-256 767dd0ff60d99f982838e8f010696ca2f5cf51e4eb3dda1f7487063e086ad834

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mongorepo-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49cbc4a910d2c9e8cd7d3e9b26841966f949cea11dcc7c3705e96d2f94f238d3
MD5 166b144c0e7f2c9fb97d83eddaba30ea
BLAKE2b-256 281c72ac1b17c48bc4a5d8c2ec914f2e4d7115649870570a3dc527e35887d2d9

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