A thin async layer between Pydantic and MongoDB
Project description
A thin async layer between Pydantic and MongoDB.
What is
Tokka is a MongoDB/Motor Async wrapper for Pydantic models.
As a heavy Pydantic/ FastAPI user, I faced myself writing some recurrent boilerplate code to make things work and remain pleasantly readable when on projects involving MongoDB.
Nowadays, Pydantic-core is written in Rust, and it's blazing fast. So, Tokka abuses from Pydantic's model_dump method to serialize Pydantic models into Dict/MongoDB documents.
No magic, no complex things, only dump. I tried to keep the code as simple as possible and to not fall deep into Pydantic's internals. I also tried to keep the code as close as possible to Pymongo's API, so it's familiar to understand and use. But I took some liberties to make things more Pythonic adding some syntactic sugar here and there, as well as some extra agnostic functionalities. In addition, Pymongo methods has some pretty strange kwargs documentation and a not-so-good type annotations, then I worked on trying make it a little bit friendly.
Personally, I see Tokka as an ingenuous package for lazy people like me. If you can make some use of it or it make you write less code, I'll be glad.
Installation
pip install tokka
Quick usage
from pydantic import BaseModel
from tokka import Database
from tokka import Collection
import asyncio
class User(BaseModel):
"""Sample data."""
name: str
email: str
class DB(Database):
"""A tokka.Database subclass to easily accesst the your collections."""
@property
def users(self) -> Collection:
return self.get_collection("users")
if __name__ == "__main__":
db = DB("sampleDB", connection="YOUR MONGODB URI")
user1 = User(name="John Doe", email="john.doe@tokka.com.br")
user2 = User(name="Emma Soo", email="emma.sue@tokka.com.br")
async def tasks() -> None:
insert_results = await asyncio.gather(
db.users.insert_one(user1),
db.users.find_one(user1, filter_by="name"),
)
print(insert_results)
replace_one_results = await asyncio.gather(
db.users.replace_one(user1, user2, filter_by="email"),
db.users.find_one(user2, filter_by="name"),
)
print(replace_one_results)
find_one_and_delete_results = await asyncio.gather(
db.users.find_one_and_delete(user2, filter_by="name"),
)
print(find_one_and_delete_results)
asyncio.run(tasks())
db.close()
Docs
Tokka is almost a syntatic-sugar package wrapping Motor and Pydantic.
Docstrings and repo samples should be enought to get you running in a few seconds.
Testing
Tokka tests run using transactions. Therefore, the containerized Mongo MUST be started as a replica set.
docker run --rm -d -p 27017:27017 \
-h $(hostname) --name mongo mongo:7.0 \
--replSet=tokka && sleep 4
docker exec mongo mongosh --quiet --eval "rs.initiate();"
After container startup, simple run pytest:
pytest -s
Benchmarking
Formal benchmarks are still necessary, but initial executions showed an impact of less than <0.1s using Tokka.
License
This project is licensed under the terms of the MIT license.
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
Built Distribution
File details
Details for the file tokka-0.1.1b0.tar.gz
.
File metadata
- Download URL: tokka-0.1.1b0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.13.1 CPython/3.12.2 Linux/5.15.146.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2b3b726914f4fef95df46e1c0a9780e90bd45ace80a8fdb41fd6f7de73b966e |
|
MD5 | 6c068ae75e7f4d9b775e77a21b05cc02 |
|
BLAKE2b-256 | 5cff27b1f79d0b30204d7de4fea764efaefa9fbf13e1d9e31cb67a3efd68ce1a |
File details
Details for the file tokka-0.1.1b0-py3-none-any.whl
.
File metadata
- Download URL: tokka-0.1.1b0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.13.1 CPython/3.12.2 Linux/5.15.146.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 316f6e9aed40305e5d4577aca626e78b0c99c826c44bb071d3e5ac94b4c167af |
|
MD5 | 355da423eee13135099db8a27d8adfee |
|
BLAKE2b-256 | b39df04b636189314adeee2dc8175260ab86ba14ece3c4b80ef1e92692e7dfba |