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
Release history Release notifications | RSS feed
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.1.tar.gz
(13.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
mongorepo-0.2.1-py3-none-any.whl
(20.6 kB
view details)
File details
Details for the file mongorepo-0.2.1.tar.gz.
File metadata
- Download URL: mongorepo-0.2.1.tar.gz
- Upload date:
- Size: 13.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dd5af54d1538caf65852c1d65d425837bb7034f66aeeb13b7ee4c373c1a3e37
|
|
| MD5 |
67efbfdf0ce6f618a444bf0340d59fb0
|
|
| BLAKE2b-256 |
7badd4de73807894a5d85b33c939bc30395c6d5f6970b2fd6f58074385dc2f28
|
File details
Details for the file mongorepo-0.2.1-py3-none-any.whl.
File metadata
- Download URL: mongorepo-0.2.1-py3-none-any.whl
- Upload date:
- Size: 20.6 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29bd3c07982f503e1ace797fd9cb61cadd98ea3621f8cc8a47cf43a6c8195d72
|
|
| MD5 |
36deab4cd425621fb745daf5061b81f9
|
|
| BLAKE2b-256 |
1999d40120ae683336d3b7be9c4c8bd6e1c652e5ee54daaeb02b3ab4fb4bc6df
|