Python library async ORM for mongodb. Object as pyDantic
Project description
Aio Mongo Pydantic ORM (ampo)
Features:
- Asynchronous
Usage
All example run into:
python -m asyncio
Create and get object
from ampo import CollectionWorker, AMPODatabase, ORMConfig, init_collection
# Initilize DB before calls db methods
AMPODatabase(url="mongodb://test")
# Pydantic Model
class ModelA(CollectionWorker):
field1: str
field2: int
model_config = ORMConfig(
orm_collection="test"
)
await init_collection()
inst_a = ModelA("test", 123)
await inst_a.save()
# Get object
inst_a = await ModelA.get(field1="test")
Id
For search by 'id' usages in filter '_id' or 'id' name.
Indexes
# import
# Initilize DB before calls db methods
AMPODatabase(url="mongodb://test")
# Pydantic Model
class ModelA(CollectionWorker):
field1: str
model_config = ORMConfig(
orm_collection="test",
orm_indexes=[
{
"keys": ["field1"],
"options": {
"unique": True
}
}
]
)
# This method create indexes
# Call only one time
await init_collection()
Suppport options:
- unique
- expireAfterSeconds
Keys is list of fields.
TTL Index
It works only with single field (TTL Indexes).
You should set the option 'expireAfterSeconds', and field 'keys' should have only single field.
Example:
# import
# Initilize DB before calls db methods
AMPODatabase(url="mongodb://test")
# Pydantic Model
class ModelA(CollectionWorker):
field1: datetime
model_config = ORMConfig(
orm_collection="test",
orm_indexes=[
{
"keys": ["field1"],
"options": {
"expireAfterSeconds": 10
}
}
]
)
# optional, set new value
ModelA.update_expiration_value("field1", 20)
await init_collection()
if you want to set the 'expireAfterSeconds' only from method 'update_expiration_value', set it to '-1'. if you want skip the index changed, call method 'expiration_index_skip' before init_collection.
Relationships between documents
Embeded
It is supported by default. Just, you need create the embedded document as class of pydantic - 'BaseModel'. It will be stored into db as object.
Example:
from pydantic import BaseModel
class Embeded(BaseModel):
name: str
class ModelA(CollectionWorker):
field1: str
field2: Embeded
model_config = ORMConfig(
orm_collection="test"
)
Lock Record
It is a mechanism that allows you to retrieve a record with a lock. It is based on the findOneAndUpdate(). When the record is found, the field "lock_field" is set to True. And when the next search is made, this record will be skipped.
Example:
import datetime
from typing import Optional
from ampo import CollectionWorker, AMPODatabase, ORMConfig, init_collection
# Pydantic Model
class ModelA(CollectionWorker):
field1: str
lfield: bool = False
field_dt_start: Optional[datetime.datetime] = None
model_config = ORMConfig(
orm_collection="test",
orm_lock_record={
"lock_field": "lfield",
"lock_field_time_start": "field_dt_start",
}
)
await init_collection()
inst_a = ModelA("test", 123)
await inst_a.save()
inst_a = await ModelA.get_and_lock(field1="test")
# process
await inst_a.reset_lock()
# as context
async with ModelA.get_and_lock_context(field1="test") as inst_a:
pass
# process
Development
Style:
Run tests:
env TEST_MONGO_URL=mongodb://localhost/test pytest
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
Built Distribution
File details
Details for the file ampo-0.2.7.tar.gz
.
File metadata
- Download URL: ampo-0.2.7.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8b76056fb47370a7c80a23ec49906444fc06822261ad99e15277f9a1539b7f0 |
|
MD5 | 8265e31688912a721395f08d365b820f |
|
BLAKE2b-256 | 5623bfb278748f967121bac7f3a372ff3edf96902003c2038b149f730f9a3fb0 |
File details
Details for the file ampo-0.2.7-py3-none-any.whl
.
File metadata
- Download URL: ampo-0.2.7-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b88f52a0ea935354178a995a9bc3b0119dcd0b3f77360b42f6ae88953f22c0d |
|
MD5 | 07581ec079a2b98221189ee09f76a676 |
|
BLAKE2b-256 | dc84d0f17fbbfbf1680797d9db51cf2e016d4e4ab7858d08b8420a4cfa2b70ca |