I have a trait Definition thus:
#[async_trait]
pub trait SqlQueryBuilder {
/// allow generic use of the query builder for multiple models
type Entity;
type Attributes;
type UpdatedAttribute;
/// save a new record in the database
async fn save(
fields: Self::Attributes,
db_connection: &Pool<Postgres>,
) -> Result<Self::Entity, sqlx::Error>;
/// update a field e.g user password
async fn update_field(
field: &str,
value: Self::UpdatedAttribute,
db_connection: &Pool<Postgres>,
) -> Result<Self::Entity, sqlx::Error>;
/// find record by id
async fn find_by_id(
id: &str,
db_connection: &Pool<Postgres>,
) -> Result<Self::Entity, sqlx::Error>;
}
I want to be able to Implement not necessarily all methods defined in the trait for a struct as seen in the case below:
impl SqlQueryBuilder for UserModel {
type Entity = UserModel;
type Attributes = UserInformation;
/// save a new record in the database
async fn save(
fields: Self::Attributes,
db_connection: &Pool<Postgres>,
) -> Result<Self::Entity, sqlx::Error> {
...
}
/// find user by id
async fn find_by_id(
id: &str,
db_connection: &Pool<Postgres>,
) -> Result<Self::Entity, sqlx::Error> {
sqlx::query_as::<_, UserModel>("SELECT * FROM user_information WHERE id = $1")
.bind(sqlx::types::Uuid::parse_str(id).unwrap())
.fetch_one(db_connection)
.await
}
}
How can I achieve this?