-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* query d1 first commit * changes for merge from comments * deleted files
- Loading branch information
1 parent
1e9aa95
commit 660337a
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Query a D1 Database with Cloudflare Workers Example | ||
|
||
Warning: Python support in Workers is experimental and things will break. This demo is meant for reference only right now; you should be prepared to update your code between now and official release time as APIs may change. | ||
|
||
Currently, Python Workers using packages cannot be deployed and will only work in local development for the time being. | ||
|
||
### How to Run | ||
Download this quotes file from [Hugging Face in SQL form](https://huggingface.co/datasets/lizziepika/quotes_sql/blob/main/data.sql) | ||
|
||
Create a new D1 database by running on the command line `npx wrangler d1 create {D1-NAME}`. Copy and paste the output into your `wrangler.toml` to bind your D1 database to your Python Worker. | ||
|
||
Ensure that your Wrangler version is up to date (3.30.0 and above). | ||
|
||
``` | ||
$ wrangler -v | ||
⛅️ wrangler 3.30.0 | ||
``` | ||
Now, if you run `wrangler dev` within this directory, it should use the config in`wrangler.toml` to run the demo. | ||
|
||
You can also run `wrangler deploy` to deploy the demo. | ||
|
||
Finally, get a random quote from the database by visiting your deployed worker in the browser!<img width="1421" alt="deployed app" src="https://github.com/user-attachments/assets/131a2836-2305-4b73-a54a-50dac039108f"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from js import Response | ||
|
||
async def on_fetch(request, env): | ||
query = """ | ||
SELECT quote, author | ||
FROM qtable | ||
ORDER BY RANDOM() | ||
LIMIT 1; | ||
""" | ||
results = await env.DB.prepare(query).all() | ||
data = results.results[0] | ||
|
||
# Return a JSON response | ||
return Response.json(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#:schema node_modules/wrangler/config-schema.json | ||
compatibility_date = "2024-08-06" | ||
|
||
[ai] | ||
binding = "AI" | ||
|
||
[[d1_databases]] | ||
binding = "DB" # i.e. available in your Worker on env.DB | ||
database_name = "quotes" # REPLACE WITH YOUR DB NAME | ||
database_id = "408cddb7-e3f7-40d7-a4f7-33136a7fd3fa" # REPLACE WITH YOUR DB ID |