Skip to content

Commit

Permalink
Merge pull request #178 from EduardoTT/main
Browse files Browse the repository at this point in the history
chore: fix quick-start documentation
  • Loading branch information
igorbenav authored Oct 21, 2024
2 parents 9ae2402 + 9758bbb commit 647005d
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ Assuming you have your SQLAlchemy model, Pydantic schemas and database connectio

Define your SQLAlchemy model

```python title="setup.py" hl_lines="11-19"
from sqlalchemy import Column, Integer, String
```python title="setup.py" hl_lines="12-20"
import datetime
from sqlalchemy import Column, DateTime, Integer, Numeric, String, func
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from pydantic import BaseModel


Expand All @@ -30,10 +31,11 @@ async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False

Then your Pydantic schemas

```python title="setup.py" hl_lines="22-27"
from sqlalchemy import Column, Integer, String
```python title="setup.py" hl_lines="23-28"
import datetime
from sqlalchemy import Column, DateTime, Integer, Numeric, String, func
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from pydantic import BaseModel


Expand All @@ -52,10 +54,11 @@ async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False

And, finally, your database connection

```python title="setup.py" hl_lines="30-32"
from sqlalchemy import Column, Integer, String
```python title="setup.py" hl_lines="31-33"
import datetime
from sqlalchemy import Column, DateTime, Integer, Numeric, String, func
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from pydantic import BaseModel


Expand All @@ -76,28 +79,34 @@ async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False

Use `crud_router` and include it in your `FastAPI` application

```python title="main.py" hl_lines="17-24 26"
```python title="main.py" hl_lines="23-30 32"
from typing import AsyncGenerator

from fastapi import FastAPI
from sqlalchemy.ext.asyncio import AsyncSession

from fastcrud import crud_router
import setup

# Database session dependency
async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with async_session() as session:
async with setup.async_session() as session:
yield session

# Create tables before the app start
async def lifespan(app: FastAPI):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with setup.engine.begin() as conn:
await conn.run_sync(setup.Base.metadata.create_all)
yield

# FastAPI app
app = FastAPI(lifespan=lifespan)

item_router = crud_router(
session=session,
model=Item,
create_schema=ItemSchema,
update_schema=ItemSchema,
session=get_session,
model=setup.Item,
create_schema=setup.ItemSchema,
update_schema=setup.ItemSchema,
path="/items",
tags=["Items"]
)
Expand Down

0 comments on commit 647005d

Please sign in to comment.