Encode/decode InteriorRef struct from jsonb column

This commit is contained in:
Tyler Hallada 2020-07-27 00:09:45 -04:00
parent 29eadabc8a
commit fb99afdc26
2 changed files with 29 additions and 9 deletions

View File

@ -16,8 +16,7 @@ listenfd = "0.3"
log = "0.4"
pretty_env_logger = "0.4"
tokio = { version = "0.2", features = ["macros"] }
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "chrono",
"uuid", "ipnetwork", "json" ] }
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "chrono", "uuid", "ipnetwork", "json" ] }
warp = { version = "0.2", features = ["compression"] }
refinery = { version = "0.3.0", features = [ "tokio-postgres", "tokio" ] }
barrel = { version = "0.6.5", features = [ "pg" ] }

View File

@ -3,15 +3,34 @@ use async_trait::async_trait;
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPool;
use sqlx::types::Json;
use super::ListParams;
use super::Model;
// sqlx queries for this model need to be `query_as_unchecked!` because `query_as!` does not
// support user-defined types (`ref_list` Json field).
// See for more info: https://github.com/thallada/rust_sqlx_bug/blob/master/src/main.rs
// This may be fixed in sqlx 0.4.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct InteriorRef {
pub mod_name: String,
pub local_form_id: i32,
pub position_x: f64,
pub position_y: f64,
pub position_z: f64,
pub angle_x: f64,
pub angle_y: f64,
pub angle_z: f64,
pub scale: f64,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct InteriorRefList {
pub id: Option<i32>,
pub shop_id: i32,
pub ref_list: serde_json::value::Value,
pub ref_list: Json<Vec<InteriorRef>>,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
@ -28,7 +47,8 @@ impl Model for InteriorRefList {
async fn get(db: &PgPool, id: i32) -> Result<Self> {
let timer = std::time::Instant::now();
let result = sqlx::query_as!(Self, "SELECT * FROM interior_ref_lists WHERE id = $1", id)
let result =
sqlx::query_as_unchecked!(Self, "SELECT * FROM interior_ref_lists WHERE id = $1", id)
.fetch_one(db)
.await?;
let elapsed = timer.elapsed();
@ -41,7 +61,8 @@ impl Model for InteriorRefList {
// TODO:
// * Decide if I'll need to make the same changes to merchandise and transactions
// - answer depends on how many rows of each I expect to insert in one go
let result = sqlx::query_as!(
// * should probably omit ref_list from response
let result = sqlx::query_as_unchecked!(
Self,
"INSERT INTO interior_ref_lists
(shop_id, ref_list, created_at, updated_at)
@ -60,7 +81,7 @@ impl Model for InteriorRefList {
async fn list(db: &PgPool, list_params: ListParams) -> Result<Vec<Self>> {
let timer = std::time::Instant::now();
let result = if let Some(order_by) = list_params.get_order_by() {
sqlx::query_as!(
sqlx::query_as_unchecked!(
Self,
"SELECT * FROM interior_ref_lists
ORDER BY $1
@ -73,7 +94,7 @@ impl Model for InteriorRefList {
.fetch_all(db)
.await?
} else {
sqlx::query_as!(
sqlx::query_as_unchecked!(
Self,
"SELECT * FROM interior_ref_lists
LIMIT $1