@@ -35,19 +35,68 @@ class Customer(SQLModel, table=True):
3535- FastAPI example: ` example_app_fastapi/ `
3636- Flask example: ` example_app_flask/ `
3737
38+ ### FastAPI Example Snippet
39+
40+ ``` python
41+ from contextlib import asynccontextmanager
42+
43+ from fastapi import Depends, FastAPI
44+ from sqlmodel import Session, select
45+
46+ from example_app_fastapi.database import get_session, init_db
47+ from example_app_fastapi.models import Customer
48+
49+ @asynccontextmanager
50+ async def lifespan (_ : FastAPI):
51+ init_db()
52+ yield
53+
54+ app = FastAPI(title = " SQLModel Encrypted Fields Example" , lifespan = lifespan)
55+
56+ @app.post (" /customers" , response_model = Customer)
57+ def create_customer (customer : Customer, session : Session = Depends(get_session)) -> Customer:
58+ session.add(customer)
59+ session.commit()
60+ session.refresh(customer)
61+ return customer
62+
63+ @app.get (" /customers/by-email/{email} " , response_model = Customer)
64+ def get_customer_by_email (email : str , session : Session = Depends(get_session)) -> Customer:
65+ statement = select(Customer).where(Customer.email_lookup == email)
66+ return session.exec(statement).first()
67+ ```
68+
3869### Flask Example Snippet
3970
4071``` python
41- from flask import Flask
72+ from flask import Flask, jsonify, request
73+ from sqlmodel import select
4274
43- from example_app_flask.database import init_db
75+ from example_app_flask.database import get_session, init_db
4476from example_app_flask.models import Customer
4577
4678app = Flask(__name__ )
4779
4880@app.before_first_request
4981def _init_db () -> None :
5082 init_db()
83+
84+ @app.post (" /customers" )
85+ def create_customer ():
86+ payload = request.get_json(force = True )
87+ customer = Customer(** payload)
88+ with get_session() as session:
89+ session.add(customer)
90+ session.commit()
91+ session.refresh(customer)
92+ return jsonify(customer.model_dump())
93+
94+ @app.get (" /customers/by-email/<string:email>" )
95+ def get_customer_by_email (email : str ):
96+ with get_session() as session:
97+ statement = select(Customer).where(Customer.email_lookup == email)
98+ customer = session.exec(statement).first()
99+ return jsonify(customer.model_dump())
51100```
52101
53102## Notes
0 commit comments