Skip to main content

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))

Release files for mongorepository 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongorepository 0.8.0
File Size Uploaded
mongorepository-0.8.0.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mongorepository 0.8.0
File Interpreter ABI Platform
mongorepository-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / mongorepository-0.8.0.tar.gz

Download URL mongorepository-0.8.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e50850017e7ea13b469efb7420c1dee1848c41a4da9e281aa46d9e4d68fc3325
BLAKE2b-256 checksum
How to use checksums
72d1250d7b6f366af0c46cf8ef1250e49262c08af0f710a90394a3f333f6fb34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.12.3 Linux/6.11.0-1018-azure

Release files / mongorepository-0.8.0-py3-none-any.whl

Download URL mongorepository-0.8.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c18965f9a65f4f5a0685fd15da827b538c1c49799c082b78ab54084941acbd4a
BLAKE2b-256 checksum
How to use checksums
50fdbf4c904bb3bb0d052a05eeeb748148d87cd1b7b7237e4e539f3d515c8921
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.12.3 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.1

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page