https://github.com/jaykv/mongospecs
mongo odm for pydantic, msgspec, and attrs
https://github.com/jaykv/mongospecs
mongodb odm python
Last synced: 5 months ago
JSON representation
mongo odm for pydantic, msgspec, and attrs
- Host: GitHub
- URL: https://github.com/jaykv/mongospecs
- Owner: jaykv
- License: mit
- Created: 2023-11-26T06:06:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-16T04:17:24.000Z (over 1 year ago)
- Last Synced: 2025-09-04T21:24:56.856Z (11 months ago)
- Topics: mongodb, odm, python
- Language: Python
- Homepage:
- Size: 909 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongospecs
Built on top of the https://github.com/GetmeUK/MongoFrames ODM, with:
- Pydantic (`BaseModel`), attrs (`@define`), and msgspec (`Struct`) support for defining schema models (specs)
- Type-hints
- *Spec adapters*
- Build a spec out of existing BaseModels, attrs classes, or Structs
- *Raw BSON encode/decode for specs/mongo documents*
## Install
Choose from one of the following:
* `pip install mongospecs` for msgspec only
* `pip install mongospecs[pydantic]` for msgspec and pydantic only
* `pip install mongospecs[attrs]` for msgspec and attrs only
* `pip install mongospecs[pydantic,attrs]` for msgspec, pydantic, and attrs
### Example
```python
# Import Spec, either...
## 1. with pydantic:
from mongospecs.pydantic import Spec
## 2. with msgspec:
from mongospecs.msgspec import Spec
## 3. with attrs:
from mongospecs.attrs import Spec
# Define schema model
class Dragon(Spec):
_collection = "dragons" # Optional. If not defined, uses the class name by default.
name: str
breed: Optional[str] = None
# create
burt = Dragon(name="Burt", breed="Cold-drake")
print(burt.name) # Burt
print(burt.breed) # Cold-drake
# insert
burt.insert()
print(burt.id) # inserted document ObjectId
# fetch
doc = Dragon.find_one({"name": "Burt"}) # returns raw mongo document
# delete
burt.delete()
```