Rework the documentation

This commit is contained in:
François Voron
2021-09-17 15:14:36 +02:00
parent e31a0a99b5
commit fc59cf11ef
33 changed files with 486 additions and 861 deletions

View File

@@ -6,7 +6,7 @@
Let's create a MongoDB connection and instantiate a collection.
```py hl_lines="23 24 25 26"
```py hl_lines="6 7 8 9 10 11"
{!./src/db_mongodb.py!}
```
@@ -17,20 +17,16 @@ You can choose any name for the database and the collection.
## 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.
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. It should be generated by a FastAPI dependency.
```py hl_lines="34"
```py hl_lines="14 15"
{!./src/db_mongodb.py!}
```
Notice that we pass a reference to your [`UserDB` model](../model.md).
Notice that we pass a reference to your [`UserDB` model](../models.md).
!!! info
The database adapter will automatically create a [unique index](https://docs.mongodb.com/manual/core/index-unique/) on `id` and `email`.
!!! warning
**FastAPI Users** will use its defined [`id` UUID](../model.md) as unique identifier for the user, rather than the builtin MongoDB `_id`.
## Next steps
We will now configure an [authentication method](../authentication/index.md).
**FastAPI Users** will use its defined [`id` UUID](../models.md) as unique identifier for the user, rather than the builtin MongoDB `_id`.

View File

@@ -24,7 +24,7 @@ For the sake of this tutorial from now on, we'll use a simple SQLite databse.
Let's declare our User ORM model.
```py hl_lines="29-33"
```py hl_lines="11-15"
{!./src/db_ormar.py!}
```
@@ -35,19 +35,15 @@ 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.
database configuration and the users logic. It should be generated by a FastAPI dependency.
```py hl_lines="40"
```py hl_lines="22-23"
{!./src/db_ormar.py!}
```
Notice that we pass a reference to your [`UserDB` model](../model.md).
Notice that we pass a reference to your [`UserDB` model](../models.md).
!!! warning
In production, it's strongly recommended to setup a migration system to
update your SQL schemas. See
[Alembic](https://alembic.sqlalchemy.org/en/latest/).
## Next steps
We will now configure an [authentication method](../authentication/index.md).

View File

@@ -22,42 +22,38 @@ For the sake of this tutorial from now on, we'll use a simple SQLite databse.
## Setup User table
Let's create a `metadata` object and declare our User table.
Let's declare our SQLAlchemy `User` table.
```py hl_lines="5 32 33"
```py hl_lines="13 14"
{!./src/db_sqlalchemy.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 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!
## Create the tables
We'll now create an SQLAlchemy engine and ask it to create all the defined tables.
```py hl_lines="36 37 38 39 40"
```py hl_lines="17 18 19 20"
{!./src/db_sqlalchemy.py!}
```
!!! warning
In production, it's strongly recommended to setup a migration system to update your SQL schemas. See [Alembic](https://alembic.sqlalchemy.org/en/latest/).
## Create the database adapter
## Create the database adapter dependency
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. Create it like this.
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. It should be generated by a FastAPI dependency.
```py hl_lines="42 43"
```py hl_lines="25 26"
{!./src/db_sqlalchemy.py!}
```
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 reference to your [`UserDB` model](../models.md).
* A `database` instance, which allows us to do asynchronous request to the database.
## Next steps
We will now configure an [authentication method](../authentication/index.md).
* The `users` variable, which is the actual SQLAlchemy table behind the table class.
## What about SQLAlchemy ORM?

View File

@@ -20,35 +20,33 @@ pip install aiosqlite
For the sake of this tutorial from now on, we'll use a simple SQLite databse.
## Setup User table
## Setup User table and model
Let's declare our User ORM model.
```py hl_lines="8 9"
{!./src/db_tortoise.py!}
```py hl_lines="18 19"
{!./src/db_tortoise_model.py!}
```
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!
## Tweak `UserDB` model
In order to make the Pydantic model and the Tortoise ORM model working well together, you'll have to add a mixin and some configuration options to your `UserDB` model. Tortoise ORM provides [utilities to ease the integration with Pydantic](https://tortoise-orm.readthedocs.io/en/latest/contrib/pydantic.html) and we'll use them here.
```py hl_lines="5 24 25 26 27"
{!./src/db_tortoise.py!}
```py hl_lines="22 23 24 25 26"
{!./src/db_tortoise_model.py!}
```
The `PydanticModel` mixin adds methods used internally by Tortoise ORM to the Pydantic model so that it can easily transform it back to an ORM model. It expects then that you provide the property `orig_model` which should point to the **User ORM model we defined just above**.
## 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.
The database adapter of **FastAPI Users** makes the link between your database configuration and the users logic. It should be generated by a FastAPI dependency.
```py hl_lines="32"
{!./src/db_tortoise.py!}
```py hl_lines="8 9"
{!./src/db_tortoise_adapter.py!}
```
Notice that we pass a reference to your [`UserDB` model](../model.md).
Notice that we pass a reference to your [`UserDB` model](../models.md).
## Register Tortoise
@@ -56,13 +54,16 @@ For using Tortoise ORM we must register our models and database.
Tortoise ORM supports integration with FastAPI out-of-the-box. It will automatically bind startup and shutdown events.
```py hl_lines="35 36 37 38 39 40"
{!./src/db_tortoise.py!}
```py
from tortoise.contrib.fastapi import register_tortoise
register_tortoise(
app,
db_url=DATABASE_URL,
modules={"models": ["models"]},
generate_schemas=True,
)
```
!!! warning
In production, it's strongly recommended to setup a migration system to update your SQL schemas. See [https://tortoise-orm.readthedocs.io/en/latest/migration.html](https://tortoise-orm.readthedocs.io/en/latest/migration.html).
## Next steps
We will now configure an [authentication method](../authentication/index.md).