Skip to content

Commit

Permalink
Added README documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareLine committed Feb 2, 2023
1 parent 445a98c commit 51a329e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,46 @@ pub async fn main(message_batch: MessageBatch<MyType>, env: Env, _ctx: Context)
}
```

## D1 Databases

### Enabling D1 databases
As D1 databases are in alpha, you'll need to enable the `d1` feature on the `worker` crate.

```toml
worker = { version = "x.y.z", features = ["d1"] }
```

### Example usage
```rust
use worker::*;

#[derive(Deserialize)]
struct Thing {
thing_id: String,
desc: String,
num: u32,
}

#[event(fetch, respond_with_errors)]
pub async fn main(request: Request, env: Env, _ctx: Context) -> Result<Response> {
Router::new()
.get_async("/:id", |_, ctx| async move {
let id = ctx.param("id").unwrap()?;
let d1 = ctx.env.d1("things-db")?;
let statement = d1.prepare("SELECT * FROM things WHERE thing_id = ?1");
let query = statement.bind(&[id])?;
let result = query.first::<Thing>(None).await?;
match result {
Some(thing) => Response::from_json(&thing),
None => Response::error("Not found", 404),
}
})
.run(request, env)
.await
}
```


# Notes and FAQ

It is exciting to see how much is possible with a framework like this, by expanding the options
Expand Down

0 comments on commit 51a329e

Please sign in to comment.