-
-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update ActiveModel by JSON #492
Conversation
But instead of setting columns one by one, why don't we convert a custom deserializable Model into ActiveModel? |
7ce941b
to
33a87d7
Compare
This reverts commit 28ab378.
Hi @billy1624 Thanks for your suggestion. And Sorry for the delay |
Hey @qyihua, if you wanna do it dynamically. I guess you need to do it via SeaQuery. let mut stmt = Query::insert();
stmt.into_table(entity::Entity)
.columns(cols); // List of columns
for node_json in node_json_batch.into_iter() { // Iterate array of JSON object
let mut vals = vec![];
for attribute in attributes.iter() { // Iterate attributes in JSON object
let val = node_json.get(attribute).unwrap();
vals.push(val);
}
stmt.values_panic(vals);
}
let builder = db.get_database_backend();
let mut stmt = builder.build(&stmt);
db.execute(stmt).await?; |
@billy1624 Thank you stmt.into_table(entity::Entity)
.columns(cols); // List of columns But pub trait SetFromJson
where
Self: ActiveModelTrait,
{
fn set_from_json(&mut self, json: &mut JsonValue) -> Result<(), ConvertError>;
}
impl<T> SetFromJson for T
where
T: ActiveModelTrait,
{
fn set_from_json(&mut self, json: &mut JsonValue) -> Result<(), ConvertError> {
for col in <<Self::Entity as EntityTrait>::Column>::iter() {
if let Some(json_val) = json.get_mut(col.as_str()) {
self.set_col_from_json(col, json_val.take())
.map_err(|err| {
ConvertError::Other(format!(
"col: `{}`, err: {}",
col.as_str(),
err.to_string()
))
})?;
}
}
Ok(())
}
} |
PR Info
Added
ActiveModel::set_from_json
methodDbErr::Json
, representing error of converting JSON value into Model