Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models and Schemas, Batch 2: Customers, Products, and Orders #130

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions docs/usage/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,53 @@ Define your SQLAlchemy models and Pydantic schemas for data representation.
--8<--
```

---

??? example "`customer/model.py`"

```python
--8<--
fastcrud/examples/customer/model.py:imports
fastcrud/examples/customer/model.py:model
--8<--
```

??? example "`customer/schemas.py`"

```python
--8<--
fastcrud/examples/customer/schemas.py:imports
fastcrud/examples/customer/schemas.py:readschema
--8<--
```

??? example "`product/model.py`"

```python
--8<--
fastcrud/examples/product/model.py:imports
fastcrud/examples/product/model.py:model
--8<--
```

??? example "`order/model.py`"

```python
--8<--
fastcrud/examples/order/model.py:imports
fastcrud/examples/order/model.py:model
--8<--
```

??? example "`order/schemas.py`"

```python
--8<--
fastcrud/examples/order/schemas.py:imports
fastcrud/examples/order/schemas.py:readschema
--8<--
```

### Step 2: Initialize FastCRUD

Create a `FastCRUD` instance for your model to handle CRUD operations.
Expand All @@ -44,6 +91,7 @@ from fastcrud import FastCRUD

# Creating a FastCRUD instance
item_crud = FastCRUD(Item)
order_crud = FastCRUD(Order)
```

### Step 3: Pick your Method
Expand Down Expand Up @@ -281,14 +329,14 @@ get_joined(
```

**Purpose**: To fetch a single record with one or multiple joins on other models.
**Usage Example**: Fetches order details for a specific order by joining with the `Customer` table, selecting specific columns as defined in `OrderSchema` and `CustomerSchema`.
**Usage Example**: Fetches order details for a specific order by joining with the `Customer` table, selecting specific columns as defined in `ReadOrderSchema` and `ReadCustomerSchema`.

```python
order_details = await order_crud.get_joined(
db,
schema_to_select=ReadOrderSchema,
join_model=Customer,
schema_to_select=OrderSchema,
join_schema_to_select=CustomerSchema,
join_schema_to_select=ReadCustomerSchema,
id=order_id,
)
```
Expand Down Expand Up @@ -325,9 +373,9 @@ get_multi_joined(
```python
orders = await order_crud.get_multi_joined(
db,
schema_to_select=OrderSchema,
schema_to_select=ReadOrderSchema,
join_model=Customer,
join_schema_to_select=CustomerSchema,
join_schema_to_select=ReadCustomerSchema,
offset=0,
limit=5,
)
Expand Down
53 changes: 51 additions & 2 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@ class FastCRUD(
Soft deletes a record if it has an `"is_deleted"` attribute (or other attribute as defined by `is_deleted_column`); otherwise, performs a hard delete.

Examples:
??? example "`customer/model.py`"

```python
--8<--
fastcrud/examples/customer/model.py:imports
fastcrud/examples/customer/model.py:model
--8<--
```

??? example "`product/model.py`"

```python
--8<--
fastcrud/examples/product/model.py:imports
fastcrud/examples/product/model.py:model
--8<--
```

??? example "`product/schemas.py`"

```python
--8<--
fastcrud/examples/product/schemas.py:imports
fastcrud/examples/product/schemas.py:readschema
--8<--
```

??? example "`order/model.py`"

```python
--8<--
fastcrud/examples/order/model.py:imports
fastcrud/examples/order/model.py:model
--8<--
```

??? example "`order/schemas.py`"

```python
--8<--
fastcrud/examples/order/schemas.py:imports
fastcrud/examples/order/schemas.py:readschema
--8<--
```

Example 1: Basic Usage
----------------------
Create a FastCRUD instance for a `User` model and perform basic CRUD operations.
Expand All @@ -129,7 +174,9 @@ class FastCRUD(

Example 2: Advanced Filtering and Pagination
--------------------------------------------

Use advanced filtering, sorting, and pagination for fetching records.

```python
product_crud = FastCRUD(Product)
products = await product_crud.get_multi(
Expand All @@ -143,15 +190,17 @@ class FastCRUD(

Example 3: Join Operations with Custom Schemas
----------------------------------------------

Perform join operations between two models using custom schemas for selection.

```python
order_crud = FastCRUD(Order)
orders = await order_crud.get_multi_joined(
db,
schema_to_select=OrderReadSchema,
schema_to_select=ReadOrderSchema,
join_model=Product,
join_prefix="product_",
join_schema_to_select=ProductReadSchema,
join_schema_to_select=ReadProductSchema,
offset=0,
limit=5,
)
Expand Down
104 changes: 95 additions & 9 deletions fastcrud/endpoint/crud_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,71 @@ def crud_router(
--8<--
```

---

??? example "`customer/model.py`"

```python
--8<--
fastcrud/examples/customer/model.py:imports
fastcrud/examples/customer/model.py:model
--8<--
```

??? example "`customer/schemas.py`"

```python
--8<--
fastcrud/examples/customer/schemas.py:imports
fastcrud/examples/customer/schemas.py:createschema
fastcrud/examples/customer/schemas.py:readschema
fastcrud/examples/customer/schemas.py:updateschema
fastcrud/examples/customer/schemas.py:deleteschema
--8<--
```

??? example "`product/model.py`"

```python
--8<--
fastcrud/examples/product/model.py:imports
fastcrud/examples/product/model.py:model
--8<--
```

??? example "`product/schemas.py`"

```python
--8<--
fastcrud/examples/product/schemas.py:imports
fastcrud/examples/product/schemas.py:createschema
fastcrud/examples/product/schemas.py:readschema
fastcrud/examples/product/schemas.py:updateschema
fastcrud/examples/product/schemas.py:deleteschema
--8<--
```

??? example "`order/model.py`"

```python
--8<--
fastcrud/examples/order/model.py:imports
fastcrud/examples/order/model.py:model
--8<--
```

??? example "`order/schemas.py`"

```python
--8<--
fastcrud/examples/order/schemas.py:imports
fastcrud/examples/order/schemas.py:createschema
fastcrud/examples/order/schemas.py:readschema
fastcrud/examples/order/schemas.py:updateschema
fastcrud/examples/order/schemas.py:deleteschema
--8<--
```

```python
mymodel_router = crud_router(
session=async_session,
Expand Down Expand Up @@ -137,9 +202,9 @@ def get_current_user(token: str = Depends(oauth2_scheme)):

Adding Delete Endpoints:
```python
router = crud_router(
product_router = crud_router(
session=async_session,
model=ProductModel,
model=Product,
create_schema=CreateProductSchema,
update_schema=UpdateProductSchema,
delete_schema=DeleteProductSchema,
Expand All @@ -150,35 +215,56 @@ def get_current_user(token: str = Depends(oauth2_scheme)):

Customizing Path and Tags:
```python
router = crud_router(
OrderCRUD = FastCRUD[
Order,
CreateOrderSchema,
UpdateOrderSchema,
UpdateOrderSchema,
DeleteOrderSchema,
]
order_router = crud_router(
session=async_session,
model=OrderModel,
model=Order,
create_schema=CreateOrderSchema,
update_schema=UpdateOrderSchema,
crud=CRUDOrderModel(OrderModel),
crud=OrderCRUD(Order),
path="/orders",
tags=["Orders", "Sales"],
)
```

Integrating Multiple Models:
```python
ProductCRUD = FastCRUD[
Product,
CreateProductSchema,
UpdateProductSchema,
UpdateProductSchema,
DeleteProductSchema,
]
product_router = crud_router(
session=async_session,
model=ProductModel,
model=Product,
create_schema=CreateProductSchema,
update_schema=UpdateProductSchema,
crud=CRUDProductModel(ProductModel),
crud=ProductCRUD(Product),
path="/products",
tags=["Inventory"],
)

CustomerCRUD = FastCRUD[
Customer,
CreateCustomerSchema,
UpdateCustomerSchema,
UpdateCustomerSchema,
DeleteCustomerSchema,
]
customer_router = crud_router(
session=async_session,
model=CustomerModel,
model=Customer,
create_schema=CreateCustomerSchema,
update_schema=UpdateCustomerSchema,
crud=CRUDCustomerModel(CustomerModel),
crud=CustomerCRUD(Customer),
path="/customers",
tags=["CRM"],
)
Expand Down
18 changes: 18 additions & 0 deletions fastcrud/examples/customer/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# --8<-- [start:imports]
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
pass


# --8<-- [end:imports]
# --8<-- [start:model]
class Customer(Base):
__tablename__ = "customer"
id = Column(Integer, primary_key=True)
name = Column(String)


# --8<-- [end:model]
32 changes: 32 additions & 0 deletions fastcrud/examples/customer/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# --8<-- [start:imports]
from pydantic import BaseModel


# --8<-- [end:imports]
# --8<-- [start:schemas]
# --8<-- [start:createschema]
class CreateCustomerSchema(BaseModel):
name: str | None = None


# --8<-- [end:createschema]
# --8<-- [start:readschema]
class ReadCustomerSchema(BaseModel):
id: int
name: str | None = None


# --8<-- [end:readschema]
# --8<-- [start:updateschema]
class UpdateCustomerSchema(BaseModel):
name: str | None = None


# --8<-- [end:updateschema]
# --8<-- [start:deleteschema]
class DeleteCustomerSchema(BaseModel):
pass


# --8<-- [end:deleteschema]
# --8<-- [end:schemas]
22 changes: 22 additions & 0 deletions fastcrud/examples/order/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# --8<-- [start:imports]
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
pass


# --8<-- [end:imports]


# --8<-- [start:model]
class Order(Base):
__tablename__ = "order"
id = Column(Integer, primary_key=True)
customer_id = Column(Integer, ForeignKey("customer.id"))
product_id = Column(Integer, ForeignKey("product.id"))
quantity = Column(Integer)


# --8<-- [end:model]
Loading
Loading