File: getting-started.md

package info (click to toggle)
python-beanie 1.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,544 kB
  • sloc: python: 14,413; makefile: 7; sh: 6
file content (115 lines) | stat: -rw-r--r-- 2,625 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Getting started

## Installing beanie

You can simply install Beanie from the [PyPI](https://pypi.org/project/beanie/):

### PIP

```shell
pip install beanie
```

### Poetry

```shell
poetry add beanie
```

### Optional dependencies

Beanie supports some optional dependencies from Motor (`pip` or `poetry` can be used).

GSSAPI authentication requires `gssapi` extra dependency:

```bash
pip install "beanie[gssapi]"
```

MONGODB-AWS authentication requires `aws` extra dependency:

```bash
pip install "beanie[aws]"
```

Support for mongodb+srv:// URIs requires `srv` extra dependency:

```bash
pip install "beanie[srv]"
```

OCSP requires `ocsp` extra dependency:

```bash
pip install "beanie[ocsp]"
```

Wire protocol compression with snappy requires `snappy` extra
dependency:

```bash
pip install "beanie[snappy]"
```

Wire protocol compression with zstandard requires `zstd` extra
dependency:

```bash
pip install "beanie[zstd]"
```

Client-Side Field Level Encryption requires `encryption` extra
dependency:

```bash
pip install "beanie[encryption]"
```

You can install all dependencies automatically with the following
command:

```bash
pip install "beanie[gssapi,aws,ocsp,snappy,srv,zstd,encryption]"
```

## Initialization

Getting Beanie setup in your code is really easy:

1.  Write your database model as a Pydantic class but use `beanie.Document` instead of `pydantic.BaseModel`.
2.  Initialize Motor, as Beanie uses this as an async database engine under the hood.
3.  Call `beanie.init_beanie` with the Motor client and list of Beanie models

The code below should get you started and shows some of the field types that you can use with beanie.

```python
from typing import Optional

import motor.motor_asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel

from beanie import Document, Indexed, init_beanie


class Category(BaseModel):
    name: str
    description: str


# This is the model that will be saved to the database
class Product(Document):
    name: str                          # You can use normal types just like in pydantic
    description: Optional[str] = None
    price: Indexed(float)              # You can also specify that a field should correspond to an index
    category: Category                 # You can include pydantic models as well


# Call this from within your event loop to get beanie setup.
async def init():
    # Create Motor client
    client = AsyncIOMotorClient("mongodb://user:pass@host:27017")

    # Init beanie with the Product document class
    await init_beanie(database=client.db_name, document_models=[Product])
```