Update Beanie docs

This commit is contained in:
François Voron
2023-04-14 15:46:11 +02:00
parent 5c559330b4
commit 4bd618503b
6 changed files with 11 additions and 28 deletions

View File

@@ -22,23 +22,6 @@ As for any Beanie ODM model, we'll create a `User` model.
As you can see, **FastAPI Users** provides a base class that will include base fields for our `User` table. You can of course add you own fields there to fit to your needs!
!!! tip "Document ID is a MongoDB ObjectID"
Beanie [automatically manages document ID](https://roman-right.github.io/beanie/tutorial/defining-a-document/#id) by encoding/decoding MongoDB ObjectID.
If you want to use another type, like UUID, you can override the `id` field:
```py
import uuid
from pydantic import Field
class User(BeanieBaseUser[uuid.UUID]):
id: uuid.UUID = Field(default_factory=uuid.uuid4)
```
Notice that `BeanieBaseUser` expects a generic type to define the actual type of ID you use.
!!! info
The base class is configured to automatically create a [unique index](https://roman-right.github.io/beanie/tutorial/defining-a-document/#indexes) on `id` and `email`.

View File

@@ -1,5 +1,5 @@
import motor.motor_asyncio
from beanie import PydanticObjectId
from beanie import Document
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
DATABASE_URL = "mongodb://localhost:27017"
@@ -9,7 +9,7 @@ client = motor.motor_asyncio.AsyncIOMotorClient(
db = client["database_name"]
class User(BeanieBaseUser[PydanticObjectId]):
class User(BeanieBaseUser, Document):
pass

View File

@@ -1,5 +1,5 @@
import motor.motor_asyncio
from beanie import PydanticObjectId
from beanie import Document
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
from fastapi_users_db_beanie.access_token import (
BeanieAccessTokenDatabase,
@@ -13,11 +13,11 @@ client = motor.motor_asyncio.AsyncIOMotorClient(
db = client["database_name"]
class User(BeanieBaseUser):
class User(BeanieBaseUser, Document):
pass
class AccessToken(BeanieBaseAccessToken[PydanticObjectId]): # (1)!
class AccessToken(BeanieBaseAccessToken, Document): # (1)!
pass

View File

@@ -1,7 +1,7 @@
from typing import List
import motor.motor_asyncio
from beanie import PydanticObjectId
from beanie import Document
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
from pydantic import Field
@@ -16,7 +16,7 @@ class OAuthAccount(BaseOAuthAccount):
pass
class User(BeanieBaseUser[PydanticObjectId]):
class User(BeanieBaseUser, Document):
oauth_accounts: List[OAuthAccount] = Field(default_factory=list)

View File

@@ -1,7 +1,7 @@
from typing import List
import motor.motor_asyncio
from beanie import PydanticObjectId
from beanie import Document
from fastapi_users.db import BaseOAuthAccount, BeanieBaseUser, BeanieUserDatabase
from pydantic import Field
@@ -16,7 +16,7 @@ class OAuthAccount(BaseOAuthAccount):
pass
class User(BeanieBaseUser[PydanticObjectId]):
class User(BeanieBaseUser, Document):
oauth_accounts: List[OAuthAccount] = Field(default_factory=list)

View File

@@ -1,5 +1,5 @@
import motor.motor_asyncio
from beanie import PydanticObjectId
from beanie import Document
from fastapi_users.db import BeanieBaseUser, BeanieUserDatabase
DATABASE_URL = "mongodb://localhost:27017"
@@ -9,7 +9,7 @@ client = motor.motor_asyncio.AsyncIOMotorClient(
db = client["database_name"]
class User(BeanieBaseUser[PydanticObjectId]):
class User(BeanieBaseUser, Document):
pass