Use proper JSON result type when getting row values

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet 2025-07-31 11:17:53 +02:00
parent 8544e3ba3f
commit 55af1e97d2
Signed by untrusted user who does not match committer: lee
GPG Key ID: 72CBFB5FDA7FE025

View File

@ -1,6 +1,7 @@
use std::time::Duration;
use heromodels_core::Model;
use postgres::types::Json;
use postgres::{Client, NoTls};
use r2d2::Pool;
use r2d2_postgres::PostgresConnectionManager;
@ -178,7 +179,11 @@ where
)
.map_err(Error::from)?
.into_iter()
.map(|row| serde_json::from_str::<M>(row.get("value")).map_err(Error::from))
.map(|row| {
row.try_get::<_, Json<M>>("value")
.map(|v| v.0)
.map_err(Error::from)
})
.collect::<Result<Vec<M>, _>>()?)
}
@ -198,7 +203,7 @@ where
.next()
{
Ok(Some(
serde_json::from_str::<M>(row.get("value")).map_err(Error::from)?,
row.try_get::<_, Json<M>>("value").map_err(Error::from)?.0,
))
} else {
Ok(None)
@ -216,7 +221,7 @@ where
"INSERT INTO {} (value) VALUES ($1) RETURNING key, value;",
Self::collection_name::<M>()
),
&[&postgres::types::Json(value)],
&[&Json(value)],
)
.map_err(Error::from)?
.into_iter()
@ -229,7 +234,7 @@ where
// Get the generated ID
let id = row.get::<_, i64>("key") as u32;
let mut value = row.get::<_, postgres::types::Json<M>>("value").0;
let mut value = row.get::<_, Json<M>>("value").0;
// .map_err(Error::from)?;
value.base_data_mut().id = id;
@ -240,10 +245,7 @@ where
"UPDATE {} SET value = $1 WHERE key = $2;",
Self::collection_name::<M>()
),
&[
&postgres::types::Json(value.clone()),
&(value.get_id() as i64),
],
&[&Json(value.clone()), &(value.get_id() as i64)],
)
.map_err(Error::from)?;
@ -259,10 +261,7 @@ where
"UPDATE {} SET value = $1 WHERE key = $2;",
Self::collection_name::<M>(),
),
&[
&postgres::types::Json(value.clone()),
&(value.get_id() as i64),
],
&[&Json(value.clone()), &(value.get_id() as i64)],
)
.map_err(Error::from)?;
@ -319,7 +318,11 @@ where
)
.map_err(Error::from)?
.into_iter()
.map(|row| serde_json::from_str::<M>(row.get("value")).map_err(Error::from))
.map(|row| {
row.try_get::<_, Json<M>>("value")
.map_err(Error::from)
.map(|v| v.0)
})
.collect::<Result<Vec<M>, _>>()?)
}