Inject every models variations and DB model in DB adapters (#84)

* Inject every model variations in router and DB model in DB adapters

* Update documentation and import Tortoise in db module

* Use path operation decorator dependencies for superuser routes
This commit is contained in:
François Voron
2020-01-04 15:36:34 +01:00
committed by GitHub
parent c903b30161
commit 104a6c6bf5
29 changed files with 501 additions and 269 deletions

View File

@ -6,7 +6,7 @@
Let's create a MongoDB connection and instantiate a collection.
```py hl_lines="5 6 7 8"
```py hl_lines="23 24 25 26"
{!./src/db_mongodb.py!}
```
@ -16,10 +16,12 @@ You can choose any name for the database and the collection.
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. Create it like this.
```py hl_lines="14"
```py hl_lines="32"
{!./src/db_mongodb.py!}
```
Notice that we pass a reference to your [`UserDB` model](../model.md).
!!! info
The database adapter will automatically create a [unique index](https://docs.mongodb.com/manual/core/index-unique/) on `id` and `email`.

View File

@ -24,7 +24,7 @@ For the sake of this tutorial from now on, we'll use a simple SQLite databse.
Let's create a `metadata` object and declare our User table.
```py hl_lines="4 14 15"
```py hl_lines="5 32 33"
{!./src/db_sqlalchemy.py!}
```
@ -34,7 +34,7 @@ As you can see, **FastAPI Users** provides a mixin that will include base fields
We'll now create an SQLAlchemy enigne and ask it to create all the defined tables.
```py hl_lines="18 19 20 21 22"
```py hl_lines="36 37 38 39 40"
{!./src/db_sqlalchemy.py!}
```
@ -45,11 +45,15 @@ We'll now create an SQLAlchemy enigne and ask it to create all the defined table
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. Create it like this.
```py hl_lines="24 25"
```py hl_lines="42 43"
{!./src/db_sqlalchemy.py!}
```
Notice that we declare the `users` variable, which is the actual SQLAlchemy table behind the table class. We also use our `database` instance, which allows us to do asynchronous request to the database.
Notice that we pass it three things:
* A reference to your [`UserDB` model](../model.md).
* The `users` variable, which is the actual SQLAlchemy table behind the table class.
* A `database` instance, which allows us to do asynchronous request to the database.
## Next steps

View File

@ -22,29 +22,31 @@ For the sake of this tutorial from now on, we'll use a simple SQLite databse.
## Setup User table
Let's declare our User model.
Let's declare our User ORM model.
```py hl_lines="9 10"
```py hl_lines="26 27"
{!./src/db_tortoise.py!}
```
As you can see, **FastAPI Users** provides a mixin that will include base fields for our User table. You can of course add you own fields there to fit to your needs!
As you can see, **FastAPI Users** provides an abstract model that will include base fields for our User table. You can of course add you own fields there to fit to your needs!
## Create the database adapter
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. Create it like this.
```py hl_lines="13"
```py hl_lines="30"
{!./src/db_tortoise.py!}
```
Notice that we pass a reference to your [`UserDB` model](../model.md).
## Register Tortoise
For using Tortoise ORM we must register our models and database.
Tortoise ORM supports integration with Starlette/FastAPI out-of-the-box. It will automatically bind startup and shutdown events.
```py hl_lines="16"
```py hl_lines="33"
{!./src/db_tortoise.py!}
```

View File

@ -7,15 +7,34 @@
* `is_active` (`bool`) Whether or not the user is active. If not, login and forgot password requests will be denied. Default to `True`.
* `is_active` (`bool`) Whether or not the user is a superuser. Useful to implement administration logic. Default to `False`.
## Use the model
## Define your models
The model is exposed as a Pydantic model mixin.
There are four Pydantic models variations provided as mixins:
* `BaseUser`, which provides the basic fields and validation ;
* `BaseCreateUser`, dedicated to user registration, which makes the `email` compulsory and adds a compulsory `password` field ;
* `BaseUpdateUser`, dedicated to user profile update, which adds an optional `password` field ;
* `BaseUserDB`, which is a representation of the user in database, adding a `hashed_password` field.
You should define each of those variations, inheriting from each mixin:
```py
from fastapi_users import BaseUser
from fastapi_users import models
class User(BaseUser):
class User(models.BaseUser):
pass
class UserCreate(User, models.BaseUserCreate):
pass
class UserUpdate(User, models.BaseUserUpdate):
pass
class UserDB(User, models.BaseUserDB):
pass
```

View File

@ -1,6 +1,6 @@
# Router
We're almost there! The last step is to configure the `FastAPIUsers` object that will wire the database adapter, the authentication class and the user model to expose the FastAPI router.
We're almost there! The last step is to configure the `FastAPIUsers` object that will wire the database adapter, the authentication class and the user models to expose the FastAPI router.
## Configure `FastAPIUsers`
@ -9,6 +9,9 @@ Configure `FastAPIUsers` object with all the elements we defined before. More pr
* `db`: Database adapter instance.
* `auth_backends`: List of authentication backends. See [Authentication](./authentication/index.md).
* `user_model`: Pydantic model of a user.
* `user_create_model`: Pydantic model for creating a user.
* `user_update_model`: Pydantic model for updating a user.
* `user_db_model`: Pydantic model of a DB representation of a user.
* `reset_password_token_secret`: Secret to encode reset password token.
* `reset_password_token_lifetime_seconds`: Lifetime of reset password token in seconds. Default to one hour.
@ -19,6 +22,9 @@ fastapi_users = FastAPIUsers(
user_db,
auth_backends,
User,
UserCreate,
UserUpdate,
UserDB,
SECRET,
)
```