Name | odmantic JSON |
Version |
1.0.2
JSON |
| download |
home_page | None |
Summary | ODMantic, an AsyncIO MongoDB Object Document Mapper for Python using type hints |
upload_time | 2024-04-26 23:29:19 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | None |
keywords |
mongodb
asyncio
types
pydantic
motor
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<b><h1>ODMantic</h1></b>
[![build](https://github.com/art049/odmantic/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/art049/odmantic/actions/workflows/ci.yml)
[![coverage](https://codecov.io/gh/art049/odmantic/branch/master/graph/badge.svg?token=3NYZK14STZ)](https://codecov.io/gh/art049/odmantic)
![python-3.8-3.9-3.10-3.11-3.12](https://img.shields.io/badge/python-3.8%20|%203.9%20|%203.10%20|%203.11%20|%203.12-informational.svg)
[![Package version](https://img.shields.io/pypi/v/odmantic?color=%2334D058&label=pypi)](https://pypi.org/project/odmantic)
[![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/art049/odmantic)
---
**Documentation**: [https://art049.github.io/odmantic/](https://art049.github.io/odmantic/)
---
Sync and Async ODM (Object Document Mapper) for <a href="https://www.mongodb.com/"
target="_blank">MongoDB</a> based on standard Python type hints. Built on top of <a
href="https://docs.pydantic.dev/" target="_blank">Pydantic</a> for model
definition and validation.
Core features:
- **Simple**: define your model by typing your fields using Python types, build queries
using Python comparison operators
- **Developer experience**: field/method autocompletion, type hints, data validation,
performing database operations with a functional API
- **Fully typed**: leverage static analysis to reduce runtime issues
- **AsyncIO support**: works well with ASGI frameworks (<a href="https://fastapi.tiangolo.com/"
target="_blank">FastAPI</a>, <a href="https://pgjones.gitlab.io/quart/"
target="_blank">quart</a>, <a href="https://sanicframework.org/"
target="_blank">sanic</a>, <a href="https://www.starlette.io/"
target="_blank">Starlette</a>, ...) but works also perfectly in synchronous environments
- **Serialization**: built-in JSON serialization and JSON schema generation
## Requirements
**Python**: 3.8 and later (tested against 3.8, 3.9, 3.10 and 3.11)
**Pydantic**: 2.5 and later
**MongoDB**: 4.0 and later
## Installation
```shell
pip install odmantic
```
## Example
> To enjoy an async context without any code boilerplate, you can reproduce the
> following steps using the AsyncIO REPL (only for Python 3.8+).
>
> ```
> python3.8 -m asyncio
> ```
>
> If you are using an earlier version of Python, you can use <a
> href="https://ipython.readthedocs.io/en/stable/install/index.html"
> target="_blank">IPython</a> which provide an Autoawait feature (starting from Python
> 3.6).
### Define your first model
```python
from typing import Optional
from odmantic import Field, Model
class Publisher(Model):
name: str
founded: int = Field(ge=1440)
location: Optional[str] = None
```
By defining the `Publisher` class, we've just created an ODMantic model 🎉. In this
example, the model will represent book publishers.
This model contains three fields:
- `name`: This is the name of the Publisher. This is a simple string field without any
specific validation, but it will be required to build a new Publisher.
- `founded`: This is the year of foundation of the Publisher. Since the printing press was invented in 1440, it would be handy to allow only values above 1440. The
`ge` keyword argument passed to the Field is exactly doing this. The model will
require a founded value greater or equal than 1440.
- `location`: This field will contain the country code of the Publisher. Defining this
field as `Optional` with a `None` default value makes it a non required field that
will be set automatically when not specified.
The collection name has been defined by ODMantic as well. In this case it will be
`publisher`.
### Create some instances
```python
instances = [
Publisher(name="HarperCollins", founded=1989, location="US"),
Publisher(name="Hachette Livre", founded=1826, location="FR"),
Publisher(name="Lulu", founded=2002)
]
```
We defined three instances of the Publisher model. They all have a `name` property as it
was required. All the foundations years are later than 1440. The last publisher has no
location specified so by default this field is set to `None` (it will be stored as
`null` in the database).
For now, those instances only exists locally. We will persist them in a database in the
next step.
### Populate the database with your instances
> For the next steps, you'll need to start a local MongoDB server.The easiest way is
> to use docker. Simply run the next command in a terminal (closing the terminal will
> terminate the MongoDB instance and remove the container).
>
> ```shell
> docker run --rm -p 27017:27017 mongo
> ```
First, let's connect to the database using the engine. In ODMantic, every database
operation is performed using the engine object.
```python
from odmantic import AIOEngine
engine = AIOEngine()
```
By default, the `AIOEngine` (stands for AsyncIOEngine) automatically tries to connect to a
MongoDB instance running locally (on port 27017). Since we didn't provide any database name, it will use
the database named `test` by default.
The next step is to persist the instances we created before. We can perform this
operation using the `AIOEngine.save_all` method.
```python
await engine.save_all(instances)
```
Most of the engine I/O methods are asynchronous, hence the `await` keyword used here.
Once the operation is complete, we should be able to see our created documents in the
database. You can use <a href="https://www.mongodb.com/products/compass"
target="_blank">Compass</a> or <a href="https://robomongo.org/"
target="_blank">RoboMongo</a> if you'd like to have a graphical interface.
Another possibility is to use `mongo` CLI directly:
```shell
mongo --eval "db.publisher.find({})"
```
Output:
```js
connecting to: mongodb://127.0.0.1:27017
{
"_id": ObjectId("5f67b331514d6855bc5c54c9"),
"founded": 1989,
"location": "US",
"name": "HarperCollins"
},
{
"_id": ObjectId("5f67b331514d6855bc5c54ca"),
"founded":1826,
"location": "FR",
"name": "Hachette Livre"
},
{
"_id": ObjectId("5f67b331514d6855bc5c54cb"),
"founded": 2002,
"location": null,
"name": "Lulu"
}
```
The created instances are stored in the `test` database under the `publisher` collection.
We can see that an `_id` field has been added to each document. MongoDB need this field
to act as a primary key. Actually, this field is added by ODMantic and you can access it
under the name `id`.
```python
print(instances[0].id)
#> ObjectId("5f67b331514d6855bc5c54c9")
```
### Find instances matching a criteria
Since we now have some documents in the database, we can start building some queries.
First, let's find publishers created before the 2000s:
```python
early_publishers = await engine.find(Publisher, Publisher.founded <= 2000)
print(early_publishers)
#> [Publisher(name="HarperCollins", founded=1989, location="US),
#> Publisher(name="Hachette Livre", founded=1826, location="FR")]
```
Here, we called the `engine.find` method. The first argument we need to specify is the
Model class we want to query on (in our case `Publisher`). The second argument is the
actual query. Similarly to <a href="https://www.sqlalchemy.org/"
target="_blank">SQLAlchemy</a>, you can build ODMantic queries using the regular python
operators.
When awaited, the `engine.find` method will return the list of matching instances stored
in the database.
Another possibility is to query for at most one instance. For example, if we want to
retrieve a publisher from Canada (CA):
```python
ca_publisher = await engine.find_one(Publisher, Publisher.location == "CA")
print(ca_publisher)
#> None
```
Here the result is `None` because no matching instances have been found in the database.
The `engine.find_one` method returns an instance if one exists in the database
otherwise, it will return `None`.
### Modify an instance
Finally, let's edit some instances. For example, we can set the `location` for the
publisher named `Lulu`.
First, we need to gather the instance from the database:
```python
lulu = await engine.find_one(Publisher, Publisher.name == "Lulu")
print(lulu)
#> Publisher(name="Lulu", founded=2002, location=None)
```
We still have the same instance, with no location set. We can change this field:
```python
lulu.location = "US"
print(lulu)
#> Publisher(name="Lulu", founded=2002, location="US)
```
The location has been changed locally but the last step to persist this change is to
save the document:
```python
await engine.save(lulu)
```
We can now check the database state:
```shell
mongo --eval "db.publisher.find({name: 'Lulu'})"
```
Output:
```js hl_lines="5"
connecting to: mongodb://127.0.0.1:27017
{
"_id": ObjectId("5f67b331514d6855bc5c54cb"),
"founded": 2002,
"location": "US",
"name": "Lulu"
}
```
The document have been successfully updated !
Now, what if we would like to change the foundation date with an invalid one (before 1440) ?
```python
lulu.founded = 1000
#> ValidationError: 1 validation error for Publisher
#> founded
#> ensure this value is greater than 1440
#> (type=value_error.number.not_gt; limit_value=1440)
```
This will raise an exception as it's not matching the model definition.
### Next steps
If you already have experience with Pydantic and FastAPI, the [Usage with FastAPI](https://art049.github.io/odmantic/usage_fastapi/) example sould be interesting for you to get kickstarted.
Otherwise, to get started on more advanced practices like relations and building more
advanced queries, you can directly check the other sections of the
[documentation](https://art049.github.io/odmantic/).
If you wish to contribute to the project (Thank you! :smiley:), you can have a look to the
[Contributing](https://art049.github.io/odmantic/contributing/) section of the
documentation.
## License
This project is licensed under the terms of the <a
href="https://github.com/art049/odmantic/blob/master/LICENSE" target="_blank">ISC license</a>.
Raw data
{
"_id": null,
"home_page": null,
"name": "odmantic",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "mongodb, asyncio, types, pydantic, motor",
"author": null,
"author_email": "Arthur Pastel <arthur.pastel@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/d6/cf/77a4fccc2810bd5cbab5df36edb209e0e91b4bab390f0e969d67c17741bb/odmantic-1.0.2.tar.gz",
"platform": null,
"description": "<b><h1>ODMantic</h1></b>\n\n[![build](https://github.com/art049/odmantic/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/art049/odmantic/actions/workflows/ci.yml)\n[![coverage](https://codecov.io/gh/art049/odmantic/branch/master/graph/badge.svg?token=3NYZK14STZ)](https://codecov.io/gh/art049/odmantic)\n![python-3.8-3.9-3.10-3.11-3.12](https://img.shields.io/badge/python-3.8%20|%203.9%20|%203.10%20|%203.11%20|%203.12-informational.svg)\n[![Package version](https://img.shields.io/pypi/v/odmantic?color=%2334D058&label=pypi)](https://pypi.org/project/odmantic)\n[![CodSpeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/art049/odmantic)\n\n---\n\n**Documentation**: [https://art049.github.io/odmantic/](https://art049.github.io/odmantic/)\n\n---\n\nSync and Async ODM (Object Document Mapper) for <a href=\"https://www.mongodb.com/\"\ntarget=\"_blank\">MongoDB</a> based on standard Python type hints. Built on top of <a\nhref=\"https://docs.pydantic.dev/\" target=\"_blank\">Pydantic</a> for model\ndefinition and validation.\n\nCore features:\n\n- **Simple**: define your model by typing your fields using Python types, build queries\n using Python comparison operators\n\n- **Developer experience**: field/method autocompletion, type hints, data validation,\n performing database operations with a functional API\n\n- **Fully typed**: leverage static analysis to reduce runtime issues\n\n- **AsyncIO support**: works well with ASGI frameworks (<a href=\"https://fastapi.tiangolo.com/\"\n target=\"_blank\">FastAPI</a>, <a href=\"https://pgjones.gitlab.io/quart/\"\n target=\"_blank\">quart</a>, <a href=\"https://sanicframework.org/\"\n target=\"_blank\">sanic</a>, <a href=\"https://www.starlette.io/\"\n target=\"_blank\">Starlette</a>, ...) but works also perfectly in synchronous environments\n\n- **Serialization**: built-in JSON serialization and JSON schema generation\n\n## Requirements\n\n**Python**: 3.8 and later (tested against 3.8, 3.9, 3.10 and 3.11)\n\n**Pydantic**: 2.5 and later\n\n**MongoDB**: 4.0 and later\n\n## Installation\n\n```shell\npip install odmantic\n```\n\n## Example\n\n> To enjoy an async context without any code boilerplate, you can reproduce the\n> following steps using the AsyncIO REPL (only for Python 3.8+).\n>\n> ```\n> python3.8 -m asyncio\n> ```\n>\n> If you are using an earlier version of Python, you can use <a\n> href=\"https://ipython.readthedocs.io/en/stable/install/index.html\"\n> target=\"_blank\">IPython</a> which provide an Autoawait feature (starting from Python\n> 3.6).\n\n### Define your first model\n\n```python\nfrom typing import Optional\n\nfrom odmantic import Field, Model\n\n\nclass Publisher(Model):\n name: str\n founded: int = Field(ge=1440)\n location: Optional[str] = None\n```\n\nBy defining the `Publisher` class, we've just created an ODMantic model \ud83c\udf89. In this\nexample, the model will represent book publishers.\n\nThis model contains three fields:\n\n- `name`: This is the name of the Publisher. This is a simple string field without any\n specific validation, but it will be required to build a new Publisher.\n\n- `founded`: This is the year of foundation of the Publisher. Since the printing press was invented in 1440, it would be handy to allow only values above 1440. The\n `ge` keyword argument passed to the Field is exactly doing this. The model will\n require a founded value greater or equal than 1440.\n\n- `location`: This field will contain the country code of the Publisher. Defining this\n field as `Optional` with a `None` default value makes it a non required field that\n will be set automatically when not specified.\n\nThe collection name has been defined by ODMantic as well. In this case it will be\n`publisher`.\n\n### Create some instances\n\n```python\ninstances = [\n Publisher(name=\"HarperCollins\", founded=1989, location=\"US\"),\n Publisher(name=\"Hachette Livre\", founded=1826, location=\"FR\"),\n Publisher(name=\"Lulu\", founded=2002)\n]\n```\n\nWe defined three instances of the Publisher model. They all have a `name` property as it\nwas required. All the foundations years are later than 1440. The last publisher has no\nlocation specified so by default this field is set to `None` (it will be stored as\n`null` in the database).\n\nFor now, those instances only exists locally. We will persist them in a database in the\nnext step.\n\n### Populate the database with your instances\n\n> For the next steps, you'll need to start a local MongoDB server.The easiest way is\n> to use docker. Simply run the next command in a terminal (closing the terminal will\n> terminate the MongoDB instance and remove the container).\n>\n> ```shell\n> docker run --rm -p 27017:27017 mongo\n> ```\n\nFirst, let's connect to the database using the engine. In ODMantic, every database\noperation is performed using the engine object.\n\n```python\nfrom odmantic import AIOEngine\n\nengine = AIOEngine()\n```\n\nBy default, the `AIOEngine` (stands for AsyncIOEngine) automatically tries to connect to a\nMongoDB instance running locally (on port 27017). Since we didn't provide any database name, it will use\nthe database named `test` by default.\n\nThe next step is to persist the instances we created before. We can perform this\noperation using the `AIOEngine.save_all` method.\n\n```python\nawait engine.save_all(instances)\n```\n\nMost of the engine I/O methods are asynchronous, hence the `await` keyword used here.\nOnce the operation is complete, we should be able to see our created documents in the\ndatabase. You can use <a href=\"https://www.mongodb.com/products/compass\"\ntarget=\"_blank\">Compass</a> or <a href=\"https://robomongo.org/\"\ntarget=\"_blank\">RoboMongo</a> if you'd like to have a graphical interface.\n\nAnother possibility is to use `mongo` CLI directly:\n\n```shell\nmongo --eval \"db.publisher.find({})\"\n```\n\nOutput:\n\n```js\nconnecting to: mongodb://127.0.0.1:27017\n{\n \"_id\": ObjectId(\"5f67b331514d6855bc5c54c9\"),\n \"founded\": 1989,\n \"location\": \"US\",\n \"name\": \"HarperCollins\"\n},\n{\n \"_id\": ObjectId(\"5f67b331514d6855bc5c54ca\"),\n \"founded\":1826,\n \"location\": \"FR\",\n \"name\": \"Hachette Livre\"\n},\n{\n \"_id\": ObjectId(\"5f67b331514d6855bc5c54cb\"),\n \"founded\": 2002,\n \"location\": null,\n \"name\": \"Lulu\"\n}\n```\n\nThe created instances are stored in the `test` database under the `publisher` collection.\n\nWe can see that an `_id` field has been added to each document. MongoDB need this field\nto act as a primary key. Actually, this field is added by ODMantic and you can access it\nunder the name `id`.\n\n```python\nprint(instances[0].id)\n#> ObjectId(\"5f67b331514d6855bc5c54c9\")\n```\n\n### Find instances matching a criteria\n\nSince we now have some documents in the database, we can start building some queries.\n\nFirst, let's find publishers created before the 2000s:\n\n```python\nearly_publishers = await engine.find(Publisher, Publisher.founded <= 2000)\nprint(early_publishers)\n#> [Publisher(name=\"HarperCollins\", founded=1989, location=\"US),\n#> Publisher(name=\"Hachette Livre\", founded=1826, location=\"FR\")]\n```\n\nHere, we called the `engine.find` method. The first argument we need to specify is the\nModel class we want to query on (in our case `Publisher`). The second argument is the\nactual query. Similarly to <a href=\"https://www.sqlalchemy.org/\"\ntarget=\"_blank\">SQLAlchemy</a>, you can build ODMantic queries using the regular python\noperators.\n\nWhen awaited, the `engine.find` method will return the list of matching instances stored\nin the database.\n\nAnother possibility is to query for at most one instance. For example, if we want to\nretrieve a publisher from Canada (CA):\n\n```python\nca_publisher = await engine.find_one(Publisher, Publisher.location == \"CA\")\nprint(ca_publisher)\n#> None\n```\n\nHere the result is `None` because no matching instances have been found in the database.\nThe `engine.find_one` method returns an instance if one exists in the database\notherwise, it will return `None`.\n\n### Modify an instance\n\nFinally, let's edit some instances. For example, we can set the `location` for the\npublisher named `Lulu`.\nFirst, we need to gather the instance from the database:\n\n```python\nlulu = await engine.find_one(Publisher, Publisher.name == \"Lulu\")\nprint(lulu)\n#> Publisher(name=\"Lulu\", founded=2002, location=None)\n```\n\nWe still have the same instance, with no location set. We can change this field:\n\n```python\nlulu.location = \"US\"\nprint(lulu)\n#> Publisher(name=\"Lulu\", founded=2002, location=\"US)\n```\n\nThe location has been changed locally but the last step to persist this change is to\nsave the document:\n\n```python\nawait engine.save(lulu)\n```\n\nWe can now check the database state:\n\n```shell\nmongo --eval \"db.publisher.find({name: 'Lulu'})\"\n```\n\nOutput:\n\n```js hl_lines=\"5\"\nconnecting to: mongodb://127.0.0.1:27017\n{\n \"_id\": ObjectId(\"5f67b331514d6855bc5c54cb\"),\n \"founded\": 2002,\n \"location\": \"US\",\n \"name\": \"Lulu\"\n}\n```\n\nThe document have been successfully updated !\n\nNow, what if we would like to change the foundation date with an invalid one (before 1440) ?\n\n```python\nlulu.founded = 1000\n#> ValidationError: 1 validation error for Publisher\n#> founded\n#> ensure this value is greater than 1440\n#> (type=value_error.number.not_gt; limit_value=1440)\n```\n\nThis will raise an exception as it's not matching the model definition.\n\n### Next steps\n\nIf you already have experience with Pydantic and FastAPI, the [Usage with FastAPI](https://art049.github.io/odmantic/usage_fastapi/) example sould be interesting for you to get kickstarted.\n\nOtherwise, to get started on more advanced practices like relations and building more\nadvanced queries, you can directly check the other sections of the\n[documentation](https://art049.github.io/odmantic/).\n\nIf you wish to contribute to the project (Thank you! :smiley:), you can have a look to the\n[Contributing](https://art049.github.io/odmantic/contributing/) section of the\ndocumentation.\n\n## License\n\nThis project is licensed under the terms of the <a\nhref=\"https://github.com/art049/odmantic/blob/master/LICENSE\" target=\"_blank\">ISC license</a>.\n\n",
"bugtrack_url": null,
"license": null,
"summary": "ODMantic, an AsyncIO MongoDB Object Document Mapper for Python using type hints",
"version": "1.0.2",
"project_urls": {
"Documentation": "https://art049.github.io/odmantic",
"Source": "https://github.com/art049/odmantic"
},
"split_keywords": [
"mongodb",
" asyncio",
" types",
" pydantic",
" motor"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bf2dc8ccdb60ccd873bff3dcabfd46fc7cf24fe09150a31446f6b3b947322fe2",
"md5": "0ecae099f15ad4de46087a6839161d94",
"sha256": "0475c763687624b70ba4d52266a7d0bdfb74dda7ec40162669334e9d41c06049"
},
"downloads": -1,
"filename": "odmantic-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0ecae099f15ad4de46087a6839161d94",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 36891,
"upload_time": "2024-04-26T23:29:17",
"upload_time_iso_8601": "2024-04-26T23:29:17.843732Z",
"url": "https://files.pythonhosted.org/packages/bf/2d/c8ccdb60ccd873bff3dcabfd46fc7cf24fe09150a31446f6b3b947322fe2/odmantic-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d6cf77a4fccc2810bd5cbab5df36edb209e0e91b4bab390f0e969d67c17741bb",
"md5": "9514f2ccd03acd6fe05b3f3b2ff22a75",
"sha256": "bbd0d46bc920e0e6d966d9f3add7396bc92377d2c73860d071d5ca8102f57235"
},
"downloads": -1,
"filename": "odmantic-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "9514f2ccd03acd6fe05b3f3b2ff22a75",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 140263,
"upload_time": "2024-04-26T23:29:19",
"upload_time_iso_8601": "2024-04-26T23:29:19.746701Z",
"url": "https://files.pythonhosted.org/packages/d6/cf/77a4fccc2810bd5cbab5df36edb209e0e91b4bab390f0e969d67c17741bb/odmantic-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-26 23:29:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "art049",
"github_project": "odmantic",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "odmantic"
}