diff --git a/heromodels/examples/postgres_model_example.rs b/heromodels/examples/postgres_model_example.rs index 437173b..fcc5a9a 100644 --- a/heromodels/examples/postgres_model_example.rs +++ b/heromodels/examples/postgres_model_example.rs @@ -179,6 +179,12 @@ fn main() { print_user_details(inactive_user); } + // Delete a user based on an index for good measure + db.collection::() + .expect("can open user collection") + .delete::("janesmith") + .expect("can delete existing user"); + println!("\n--- User Model Information ---"); println!("User DB Prefix: {}", User::db_prefix()); diff --git a/heromodels/src/db.rs b/heromodels/src/db.rs index 1576c2f..ed8c3fa 100644 --- a/heromodels/src/db.rs +++ b/heromodels/src/db.rs @@ -28,7 +28,7 @@ where where I: Index, I::Key: Borrow, - Q: ToString + ?Sized; + Q: ToString + Serialize + core::fmt::Debug + Sync + ?Sized; /// Get an object from its ID. This does not use an index lookup fn get_by_id(&self, id: u32) -> Result, Error>; @@ -49,7 +49,7 @@ where where I: Index, I::Key: Borrow, - Q: ToString + ?Sized; + Q: ToString + Serialize + core::fmt::Debug + Sync + ?Sized; /// Delete an object with a given ID fn delete_by_id(&self, id: u32) -> Result<(), Error>; diff --git a/heromodels/src/db/hero.rs b/heromodels/src/db/hero.rs index 61beb2f..1bff959 100644 --- a/heromodels/src/db/hero.rs +++ b/heromodels/src/db/hero.rs @@ -8,8 +8,8 @@ use std::{ collections::HashSet, path::PathBuf, sync::{ - Arc, Mutex, atomic::{AtomicU32, Ordering}, + Arc, Mutex, }, }; diff --git a/heromodels/src/db/postgres.rs b/heromodels/src/db/postgres.rs index b807986..ca466cc 100644 --- a/heromodels/src/db/postgres.rs +++ b/heromodels/src/db/postgres.rs @@ -5,6 +5,7 @@ use postgres::types::Json; use postgres::{Client, NoTls}; use r2d2::Pool; use r2d2_postgres::PostgresConnectionManager; +use serde::Serialize; #[derive(Clone)] pub struct Postgres { @@ -165,7 +166,7 @@ where where I: heromodels_core::Index, I::Key: std::borrow::Borrow, - Q: ToString + ?Sized, + Q: ToString + Serialize + core::fmt::Debug + Sync + ?Sized, { let mut con = self.pool.get().map_err(Error::from)?; @@ -175,7 +176,7 @@ where "SELECT (value) FROM {} WHERE value->$1 = $2;", Self::collection_name::(), ), - &[&I::key(), &key.to_string()], + &[&I::field_name(), &Json(key)], ) .map_err(Error::from)? .into_iter() @@ -277,7 +278,7 @@ where where I: heromodels_core::Index, I::Key: std::borrow::Borrow, - Q: ToString + ?Sized, + Q: ToString + Serialize + core::fmt::Debug + Sync + ?Sized, { let mut con = self.pool.get().map_err(Error::from)?; @@ -286,7 +287,7 @@ where "DELETE FROM {} WHERE value->$1 = $2;", Self::collection_name::() ), - &[&I::key(), &key.to_string()], + &[&I::field_name(), &Json(key)], ) .map_err(Error::from)?;