From e39e732552fd056a1ce064f4d85d8e4c29fcb2f8 Mon Sep 17 00:00:00 2001 From: Igor Magalhaes Date: Sun, 11 Feb 2024 18:30:37 -0300 Subject: [PATCH] docs updated, version updated to 0.6.0 --- docs/advanced/endpoint.md | 35 +++++++++++++++++++++++++++++++++-- docs/index.md | 6 ++---- docs/quick-start.md | 5 ++--- docs/sqlmodel.md | 5 ++--- docs/usage/endpoint.md | 7 +------ pyproject.toml | 2 +- 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/docs/advanced/endpoint.md b/docs/advanced/endpoint.md index 55a1b9f..675e20a 100644 --- a/docs/advanced/endpoint.md +++ b/docs/advanced/endpoint.md @@ -192,13 +192,13 @@ class MyModel(Base): archived_at = Column(DateTime) # Custom timestamp column for soft delete ``` -### Using `EndpointCreator` and `crud_router` with Custom Soft Delete Columns +### Using `EndpointCreator` and `crud_router` with Custom Soft Delete or Update Columns When initializing `crud_router` or creating a custom `EndpointCreator`, you can pass the names of your custom soft delete columns through the `FastCRUD` initialization. This informs FastCRUD which columns to check and update for soft deletion operations. Here's an example of using `crud_router` with custom soft delete columns: -```python +```python hl_lines="11-14 20" from fastapi import FastAPI from fastcrud import FastCRUD, crud_router from sqlalchemy.ext.asyncio import AsyncSession @@ -227,6 +227,37 @@ app.include_router(crud_router( )) ``` +You may also directly pass the names of the columns to crud_router or EndpointCreator: + +```python hl_lines="9 10" +app.include_router(endpoint_creator( + session=async_session, + model=MyModel, + create_schema=CreateMyModelSchema, + update_schema=UpdateMyModelSchema, + delete_schema=DeleteMyModelSchema, + path="/mymodel", + tags=["MyModel"], + is_deleted_column='archived', + deleted_at_column='archived_at' +)) +``` + +You can also customize your `updated_at` column: + +```python hl_lines="9" +app.include_router(endpoint_creator( + session=async_session, + model=MyModel, + create_schema=CreateMyModelSchema, + update_schema=UpdateMyModelSchema, + delete_schema=DeleteMyModelSchema, + path="/mymodel", + tags=["MyModel"], + updated_at_column='date_updated' +)) +``` + This setup ensures that the soft delete functionality within your application utilizes the `archived` and `archived_at` columns for marking records as deleted, rather than the default `is_deleted` and `deleted_at` fields. By specifying custom column names for soft deletion, you can adapt FastCRUD to fit the design of your database models, providing a flexible solution for handling deleted records in a way that best suits your application's needs. diff --git a/docs/index.md b/docs/index.md index b6b80a7..6ffad43 100644 --- a/docs/index.md +++ b/docs/index.md @@ -69,7 +69,7 @@ async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False Use `crud_router` and include it in your `FastAPI` application ```python -from fastcrud import FastCRUD, crud_router +from fastcrud import crud_router async def get_session() -> AsyncGenerator[AsyncSession, None]: async with async_session() as session: @@ -86,7 +86,6 @@ app = FastAPI(lifespan=lifespan) item_router = crud_router( session=get_session, model=Item, - crud=FastCRUD(Item), create_schema=ItemSchema, update_schema=ItemSchema, path="/items", @@ -171,7 +170,7 @@ class ItemUpdateSchema(BaseModel): from typing import AsyncGenerator from fastapi import FastAPI -from fastcrud import FastCRUD, crud_router +from fastcrud import crud_router from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker @@ -201,7 +200,6 @@ crud = FastCRUD(Item) item_router = crud_router( session=get_session, model=Item, - crud=crud, create_schema=ItemCreateSchema, update_schema=ItemUpdateSchema, path="/items", diff --git a/docs/quick-start.md b/docs/quick-start.md index 86939af..4f8ad05 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -82,8 +82,8 @@ 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-25 27" -from fastcrud import FastCRUD, crud_router +```python title="main.py" hl_lines="17-24 26" +from fastcrud import crud_router # Database session dependency async def get_session() -> AsyncGenerator[AsyncSession, None]: @@ -102,7 +102,6 @@ app = FastAPI(lifespan=lifespan) item_router = crud_router( session=session, model=Item, - crud=FastCRUD(Item), create_schema=ItemSchema, update_schema=ItemSchema, path="/items", diff --git a/docs/sqlmodel.md b/docs/sqlmodel.md index 00e4677..8ee1024 100644 --- a/docs/sqlmodel.md +++ b/docs/sqlmodel.md @@ -66,8 +66,8 @@ 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-25 27" -from fastcrud import FastCRUD, crud_router +```python title="main.py" hl_lines="17-24 26" +from fastcrud import crud_router # Database session dependency async def get_session() -> AsyncGenerator[AsyncSession, None]: @@ -86,7 +86,6 @@ app = FastAPI(lifespan=lifespan) item_router = crud_router( session=get_session, model=Item, - crud=FastCRUD(Item), create_schema=ItemSchema, update_schema=ItemSchema, path="/items", diff --git a/docs/usage/endpoint.md b/docs/usage/endpoint.md index 5ab9815..14d0b8b 100644 --- a/docs/usage/endpoint.md +++ b/docs/usage/endpoint.md @@ -43,7 +43,7 @@ class ItemUpdateSchema(BaseModel): ### Step 2: Set Up FastAPI and FastCRUD -Next, set up your FastAPI application and FastCRUD instances. This involves configuring the database connection and creating a CRUD instance for your model. +Next, set up your FastAPI application, you can optionally set up a custom FastCRUD instance as well. This involves configuring the database connection. ```python from typing import AsyncGenerator @@ -71,9 +71,6 @@ async def lifespan(app: FastAPI): # FastAPI app app = FastAPI(lifespan=lifespan) - -# CRUD operations setup -crud = FastCRUD(Item) ``` ### Step 3: Use `crud_router` to Create Endpoints @@ -83,7 +80,6 @@ crud = FastCRUD(Item) item_router = crud_router( session=get_session, model=Item, - crud=crud, create_schema=ItemCreateSchema, update_schema=ItemUpdateSchema, path="/items", @@ -176,7 +172,6 @@ from fastcrud import EndpointCreator endpoint_creator = EndpointCreator( session=get_session, model=YourModel, - crud=your_crud_instance, create_schema=YourCreateSchema, update_schema=YourUpdateSchema, delete_schema=YourDeleteSchema, diff --git a/pyproject.toml b/pyproject.toml index 38bf27d..a732b0c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fastcrud" -version = "0.5.0" +version = "0.6.0" description = "FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities." authors = ["Igor Benav "] license = "MIT"