Skip to content

Add D1 query example to FastAPI worker sample#77

Open
joumaico wants to merge 2 commits intocloudflare:mainfrom
joumaico:main
Open

Add D1 query example to FastAPI worker sample#77
joumaico wants to merge 2 commits intocloudflare:mainfrom
joumaico:main

Conversation

@joumaico
Copy link
Copy Markdown

This PR updates the 03-fastapi example to show how to access a Cloudflare D1 database from within a FastAPI route in Workers Python.

What changed

  • Extended the /env endpoint to include a D1 query example
  • Added a sample query using env.DB.prepare("SELECT * FROM users;").all()
  • Demonstrated how to iterate through results.results
  • Added an example of converting returned JsProxy rows into standard Python dictionaries

Why

The current example shows how to read an environment variable, but it does not demonstrate how to interact with other Worker bindings such as D1. Since D1 is a common Cloudflare use case, this makes the FastAPI example more practical and useful for users.

Notes

  • This assumes a D1 binding named DB
  • The example expects a users table with fields like id, name, and age

Copy link
Copy Markdown
Contributor

@dom96 dom96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! Since the fastapi example doesn't currently use a D1 binding and adding its use here would mean the example doesn't work as-is, I wonder if instead you could create a new example in 04-query-d1 which shows how to use fastapi with D1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants