use crate::identifier::Identifier;
use crate::types::{AnySurrealType, SurrealType};
use vantage_expressions::Expressive;
use super::{SurrealUpdate, UpdateMode};
impl SurrealUpdate {
pub fn table(table: &str) -> Self {
Self {
target: Identifier::new(table).expr(),
mode: UpdateMode::Set,
fields: indexmap::IndexMap::new(),
conditions: Vec::new(),
}
}
pub fn new(target: impl Expressive<AnySurrealType>) -> Self {
Self {
target: target.expr(),
mode: UpdateMode::Set,
fields: indexmap::IndexMap::new(),
conditions: Vec::new(),
}
}
pub fn content(mut self) -> Self {
self.mode = UpdateMode::Content;
self
}
pub fn merge(mut self) -> Self {
self.mode = UpdateMode::Merge;
self
}
pub fn set(mut self) -> Self {
self.mode = UpdateMode::Set;
self
}
pub fn with_field<K: Into<String>, T: SurrealType + 'static>(
mut self,
key: K,
value: T,
) -> Self {
self.fields.insert(key.into(), AnySurrealType::new(value));
self
}
pub fn with_any_field<K: Into<String>>(mut self, key: K, value: AnySurrealType) -> Self {
self.fields.insert(key.into(), value);
self
}
pub fn with_record(mut self, record: &vantage_types::Record<AnySurrealType>) -> Self {
for (k, v) in record.iter() {
self.fields.insert(k.clone(), v.clone());
}
self
}
pub fn with_condition(mut self, condition: impl Expressive<AnySurrealType>) -> Self {
self.conditions.push(condition.expr());
self
}
}