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

feat: Tokio with D1 example #475

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions examples/tokio-d1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
20 changes: 20 additions & 0 deletions examples/tokio-d1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "tokio-postgres-on-workers"
version = "0.1.0"
edition = "2021"
resolver = "2"

[package.metadata.release]
release = false

# https://github.com/rustwasm/wasm-pack/issues/1247
[package.metadata.wasm-pack.profile.release]
wasm-opt = false

[lib]
crate-type = ["cdylib"]

[dependencies]
worker = { workspace=true, features=["tokio-postgres", "d1"] }
serde_json = "1.0.114"
serde = "1.0.197"
17 changes: 17 additions & 0 deletions examples/tokio-d1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# tokio and d1 on Cloudflare Workers

Demonstration of using `tokio` and `d1` on Cloudflare Workers.

## Setup
1. `npx wrangler d1 create dev-d1-rust`

Now you can add your database to the configuration toml by replacing
`<YOUR_DATABASE_ID>`


2. Insert some records
`npx wrangler d1 execute dev-d1-rust --file=./database/schema.sql`



3. `npm run deploy`
3 changes: 3 additions & 0 deletions examples/tokio-d1/database/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS Customers;
CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY, CompanyName TEXT, ContactName TEXT);
INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (1, 'Alfreds Futterkiste', 'Maria Anders'), (4, 'Around the Horn', 'Thomas Hardy'), (11, 'Bs Beverages', 'Victoria Ashworth'), (13, 'Bs Beverages', 'Random Name');
12 changes: 12 additions & 0 deletions examples/tokio-d1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "tokio-d1",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev --local"
},
"devDependencies": {
"wrangler": "^2.13.0"
}
}
24 changes: 24 additions & 0 deletions examples/tokio-d1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use worker::*;

use serde::{Deserialize,Serialize};


#[allow(non_snake_case)]
#[derive(Deserialize,Serialize)]
struct Customers {
CustomerId: String,
CompanyName: String,
ContactName: String
}

#[event(fetch)]
async fn main(_req: Request, env: Env, _ctx: Context) -> Result<Response> {
let d1 = env.d1("DB")?;
let statement = d1.prepare("SELECT * FROM Customers WHERE id = ?1");
let query = statement.bind(&["1".into()])?;
let result = query.first::<Customers>(None).await?;
match result {
Some(customer) => Response::from_json(&customer),
None => Response::error("Not found", 404),
}
}
11 changes: 11 additions & 0 deletions examples/tokio-d1/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "tokio-postgres-on-workers"
main = "build/worker/shim.mjs"
compatibility_date = "2023-03-22"

[build]
command = "cargo install --path ../../worker-build && worker-build --release"

[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "dev-d1-rust"
database_id = "<YOUR_DATABASE_ID>"