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

Update tokio-postgres example to demonstrate Hyperdrive #569

Merged
merged 2 commits into from
May 7, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/tokio-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ crate-type = ["cdylib"]
[dependencies]
worker = { workspace=true, features=["tokio-postgres"] }
tokio-postgres = { version="0.7", features=['js'], default-features=false }
anyhow="1"
32 changes: 19 additions & 13 deletions examples/tokio-postgres/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
use worker::postgres_tls::PassthroughTls;
use worker::*;
use worker::{postgres_tls::PassthroughTls, *};

#[event(fetch)]
async fn main(_req: Request, _env: Env, _ctx: Context) -> Result<Response> {
let mut config = tokio_postgres::config::Config::new();
// Configure username, password, database as needed.
config.user("postgres");
async fn main(_req: Request, env: Env, _ctx: Context) -> anyhow::Result<Response> {
let hyperdrive = env.hyperdrive("DB")?;

// Connect using Worker Socket
let socket = Socket::builder()
.secure_transport(SecureTransport::StartTls)
.connect("database_url", 5432)?;
let (_client, connection) = config
.connect_raw(socket, PassthroughTls)
.await
.map_err(|e| worker::Error::RustError(format!("tokio-postgres: {:?}", e)))?;
.connect(hyperdrive.host(), hyperdrive.port())?;

let config = hyperdrive
.connection_string()
.parse::<tokio_postgres::Config>()?;

let (client, connection) = config.connect_raw(socket, PassthroughTls).await?;

wasm_bindgen_futures::spawn_local(async move {
if let Err(error) = connection.await {
console_log!("connection error: {:?}", error);
}
});

// Use `client` to make queries.
// Setup table:
// CREATE TABLE IF NOT EXISTS foo (id SERIAL PRIMARY KEY, name TEXT);
// INSERT INTO foo (name) VALUES ('Fred');

// `query` uses a prepared statement which is not supported by Hyperdrive
let result = client.simple_query("SELECT * FROM FOO").await?;

console_log!("{:?}", result);

Response::ok("Hello, World!")
Ok(Response::ok("Hello, World!")?)
}
10 changes: 7 additions & 3 deletions examples/tokio-postgres/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name = "tokio-postgres-on-workers"
name = "hyperdrive-test"
main = "build/worker/shim.mjs"
compatibility_date = "2023-03-22"
compatibility_date = "2024-05-01"

[[hyperdrive]]
binding = "DB"
id = ""

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