Now we have a deployed Rust Netlify Function that is querying PlanetScale and responding to users with JSON representations of a subset of the Pokemon data we have stored.
There's a couple hangups that I want to cover before we move on. The first is querying for boolean values, which are stored as TinyInt
in MySQL, which are i8
s that can be either 0 or 1.
First we'll add the legendary_or_mythical
value to our PokemonHp
struct.
#[derive(Debug, sqlx::FromRow, Serialize)]
struct PokemonHp {
name: String,
hp: u16,
legendary_or_mythical: bool,
}
Then we'll add the field in our SQL query as well.
SELECT name, hp, legendary_or_mythical FROM pokemon WHERE slug = ?
With these two in place, if we test our application with cargo test
we'll see a warning expected bool, found i8
.
error[E0308]: mismatched types
--> crates/pokemon-api/src/main.rs:66:26
|
66 | let result = sqlx::query_as!(
| __________________________^
67 | | PokemonHp,
68 | | r#"SELECT name, hp, legendary_or_mythical FROM pokemon WHERE slug = ?"#,
69 | | pokemon_name
70 | | )
| |_________________^ expected `bool`, found `i8`
|
= note: this error originates in the macro `$crate::sqlx_macros::expand_query` (in Nightly builds, run with -Z macro-backtrace for more info)
This is because of the way MySQL stores booleans, which isn't as booleans at all. We have to modify our SQL query to inform sqlx of our intent to return a boolean.
legendary_or_mythical as "legendary_or_mythical!: bool",
This will compile, and we still need to update our handler_handles
test with the additional field.
body: Some(Body::Text(
serde_json::to_string(&PokemonHp {
name: String::from("Bulbasaur"),
hp: 45,
legendary_or_mythical: false
},)
.unwrap()
)),
Altogether we'll now get the legendary_or_mythical
boolean in our API responses.