use crate::Expr;
use crate::identifier::Identifier;
use crate::types::{AnySurrealType, SurrealType};
use vantage_expressions::Expressive;
use super::SurrealInsert;
impl SurrealInsert {
pub fn new(table: &str) -> Self {
Self {
table: Identifier::new(table),
id: None,
fields: indexmap::IndexMap::new(),
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(Identifier::new(id.into()));
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(crate) fn target_expr(&self) -> Expr {
match &self.id {
Some(id) => crate::surreal_expr!("{}:{}", (self.table), (id)),
None => self.table.expr(),
}
}
}