From f3eab50e77c619e91021b2d849b774c5509b9e52 Mon Sep 17 00:00:00 2001 From: Yerassyl Zhanymkanov Date: Sun, 28 Aug 2022 17:49:12 +0600 Subject: [PATCH] Enhance wording for SQL-first points --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 087361a..ba04159 100644 --- a/README.md +++ b/README.md @@ -749,8 +749,7 @@ print(type(p.content)) ### 19. SQL-first, Pydantic-second - 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. -- Nested objects like JSON aggregation should be done in DB. -- Pydantic should only serialize data from db with minimum extra normalization. +- It's preferable to aggregate JSONs in DB for responses with nested objects. ```python # src.posts.service from typing import Mapping @@ -835,7 +834,7 @@ class Post(BaseModel): return creator # src.posts.router -@from fastapi import APIRouter, Depends +from fastapi import APIRouter, Depends router = APIRouter() @@ -847,7 +846,7 @@ async def get_creator_posts(creator: Mapping = Depends(valid_creator_id)): 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. ```python from pydantic import BaseModel, Json