Skip to content

Commit

Permalink
Fix/documentation (#17)
Browse files Browse the repository at this point in the history
* documentation
  • Loading branch information
roman-right authored Apr 18, 2021
1 parent 2be23f1 commit d30edf5
Show file tree
Hide file tree
Showing 17 changed files with 1,235 additions and 98 deletions.
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,70 @@
![Beanie](https://raw.githubusercontent.com/roman-right/beanie/main/assets/logo/with_text.svg)

Beanie - is an asynchronous ODM for MongoDB, based on [Motor](https://motor.readthedocs.io/en/stable/)
[Beanie](https://github.com/roman-right/beanie) - is an asynchronous ODM for MongoDB, based on [Motor](https://motor.readthedocs.io/en/stable/)
and [Pydantic](https://pydantic-docs.helpmanual.io/).

It uses an abstraction over Pydantic models and Motor collections to work with the database. Class Document allows to
create, replace, update, get, find and aggregate.

Beanie supports migrations out of the box.

### Installation

#### PIP

##### Stable

```shell
pip install beanie
```

##### Beta with migrations
#### Poetry

```shell
pip install beanie==0.4.b1
poetry add beanie
```

#### Poetry
### Quick Start

##### Stable
```python
from typing import Optional, List

```shell
poetry add beanie
```
import motor
from beanie import Document, init_beanie
from pydantic import BaseModel

##### Beta with migrations

```shell
poetry add beanie==0.4.b1
class Tag(BaseModel):
name: str
color: str


class Note(Document):
title: str
text: Optional[str]
tag_list: List[Tag] = []


async def main():
client = motor.motor_asyncio.AsyncIOMotorClient(
"mongodb://user:pass@host:27017"
)
await init_beanie(database=client.db_name, document_models=[Note])

all_notes = await Note.find_all().to_list()
```

### Quick Start
### Materials

#### ODM
- **[Tutorial](https://roman-right.github.io/beanie/tutorial/odm/)** - ODM usage examples
- **[Documentation](https://roman-right.github.io/beanie/documentation/odm/)** - Full list of the ODM classes and
methods with descriptions

**[Documentation](https://roman-right.github.io/beanie/)** - here you can find all the methods descriptions and usage
examples
#### Migrations
- **[Tutorial](https://roman-right.github.io/beanie/tutorial/odm/)** - Migrations usage examples

### Resources

- **[GitHub](https://github.com/roman-right/beanie)** - GitHub page of the project
- **[Changelog](https://roman-right.github.io/beanie/changelog)** - list of all the valuable changes
- **[Discord](https://discord.gg/ZTTnM7rMaz)** - ask your questions, share ideas or just say `Hello!!`

Expand Down
2 changes: 1 addition & 1 deletion beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from beanie.odm.documents import Document
from beanie.odm.cursor import Cursor

__version__ = "0.4.0b1"
__version__ = "0.4.0"
__all__ = [
# ODM
"Document",
Expand Down
11 changes: 6 additions & 5 deletions beanie/odm/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ async def collection_factory(
Collection factory.
Creates internal CollectionMeta class for the Document on the init step,
:param database: Motor database instance
:param document_class: a class, inherited from Document class
:param allow_index_dropping: if index dropping is allowed
:param collection_class: Collection, which was set up by user
:return: Collection class
:param database: AsyncIOMotorDatabase - Motor database instance
:param document_class: Type - a class, inherited from Document class
:param allow_index_dropping: bool - if index dropping is allowed
:param collection_class: Optional[Type] - Collection, which was set up
by user
:return: Type - Collection class
"""
# parse collection parameters
if collection_class:
Expand Down
17 changes: 15 additions & 2 deletions beanie/odm/cursor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from typing import Type
from typing import Type, Optional, List, Union

from motor.motor_asyncio import AsyncIOMotorCursor
from pydantic.main import BaseModel


class Cursor:
"""
Cursor class. Wrapper over AsyncIOMotorCursor,
which parse result with model
"""

def __init__(
self, motor_cursor: AsyncIOMotorCursor, model: Type[BaseModel] = None
):
Expand All @@ -18,7 +23,15 @@ async def __anext__(self):
next_item = await self.motor_cursor.__anext__()
return self.model.parse_obj(next_item) if self.model else next_item

async def to_list(self, length=None):
async def to_list(
self, length: Optional[int] = None
) -> Union[List["Document"], List[dict]]:
"""
Get list of documents
:param length: Optional[int] - length of the list
:return: Union[List["Document"], List[dict]]
"""
motor_list = await self.motor_cursor.to_list(length)
if self.model:
return [self.model.parse_obj(i) for i in motor_list]
Expand Down
Loading

0 comments on commit d30edf5

Please sign in to comment.