Skip to main content

No project description provided

Project description

Mongo Repository

This package provide a sync and async repositories utilities for mongodb.

Tutorial

=======

The first step is to create a schema inheriting from MongoBaseModel and then create a specialized repository inheriting from Repository or AsyncRepository according to the application.

Creating a db schema

from mongorepository.models import MongoBaseModel

class Person(MongoBaseModel):
    name: str
    age: int
    job: Optional[str]

Then create a person repository inheriting from Repository

import pymongo
from mongorepository.repositories.mongo import Repository

def get_database(db_name: str):
    client = pymongo.MongoClient("mongodb://localhost:2707")
    return client.get_database(db_name)

class PersonRepository(Repository[Person]):
    def __init__(self):
        super().__init__(get_database("my_db_name"))

    class Config:
        collection = "persons"

And then:


person = Person(name="John Doe", age=33)
repository = PersonRepository()

repository.save(person)

For async flow use this:

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from mongorepository.repositories.async_mongo import AsyncRepository


def get_database(db_name):
    client = AsyncIOMotorClient("mongodb://localhost:27017")
    database = client.get_database(db_name)
    database.get_io_loop = asyncio.get_event_loop
    return database


class PersonRepository(AsyncRepository[Person]):

    def __init__(self):
        super().__init__(get_database("my_db_name"))

    class Config:
        collection = "persons"


person = Person(name="John Doe", age=33)
repository = PersonRepository()

asyncio.run(repository.save(person))

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

mongorepository-0.6.5.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

mongorepository-0.6.5-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file mongorepository-0.6.5.tar.gz.

File metadata

  • Download URL: mongorepository-0.6.5.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for mongorepository-0.6.5.tar.gz
Algorithm Hash digest
SHA256 64ca0171f96cbd7ad7f068782ac4b19f8bd2adf84f52be5f778c5a1e6dee6fec
MD5 c0567286ae891812e6b6e9c94a9a9f14
BLAKE2b-256 90e56c9af5299b2abc96b5b4c3005824d2b45b150dd6255df6498a0ae3ea4c64

See more details on using hashes here.

File details

Details for the file mongorepository-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: mongorepository-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for mongorepository-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 67325a65508d1dea46af471b41f2adaa437f657db6fa60430cb19f00fe1596ee
MD5 a2e1eab54f921d14108bdce6ecc2b96a
BLAKE2b-256 a5d8c9e62cef6d0a96a1adc57336e82437892e4aef5dc8297e61769badf776d9

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