Enhance wording for SQL-first points

This commit is contained in:
Yerassyl Zhanymkanov
2022-08-28 17:49:12 +06:00
parent 5924edcdbb
commit f3eab50e77

View File

@@ -749,8 +749,7 @@ print(type(p.content))
### 19. SQL-first, Pydantic-second ### 19. SQL-first, Pydantic-second
- Usually, database handles data processing much faster and cleaner than CPython will ever do. - Usually, database handles data processing much faster and cleaner than CPython will ever do.
- It's preferable to do all the complex joins and simple data manipulations with SQL. - It's preferable to do all the complex joins and simple data manipulations with SQL.
- Nested objects like JSON aggregation should be done in DB. - It's preferable to aggregate JSONs in DB for responses with nested objects.
- Pydantic should only serialize data from db with minimum extra normalization.
```python ```python
# src.posts.service # src.posts.service
from typing import Mapping from typing import Mapping
@@ -835,7 +834,7 @@ class Post(BaseModel):
return creator return creator
# src.posts.router # src.posts.router
@from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
router = APIRouter() router = APIRouter()
@@ -847,7 +846,7 @@ async def get_creator_posts(creator: Mapping = Depends(valid_creator_id)):
return posts return posts
``` ```
If an aggregated data form DB is a simple JSON, then take a look Json field type of Pydantic, If an aggregated data form DB is a simple JSON, then take a look at Pydantic's `Json` field type,
which will load raw JSON first. which will load raw JSON first.
```python ```python
from pydantic import BaseModel, Json from pydantic import BaseModel, Json