use crate::ctx::Context;
use crate::dbs::Statement;
use crate::dbs::{Options, Transaction};
use crate::doc::Document;
use crate::err::Error;
use crate::value::Value;
use std::ops::Deref;
impl<'a> Document<'a> {
pub async fn event(
&self,
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
stm: &Statement<'_>,
) -> Result<(), Error> {
if !opt.events {
return Ok(());
}
if !opt.force && !self.changed() {
return Ok(());
}
let opt = &opt.new_with_perms(false);
for ev in self.ev(opt, txn).await?.iter() {
let met = if stm.is_delete() {
Value::from("DELETE")
} else if self.is_new() {
Value::from("CREATE")
} else {
Value::from("UPDATE")
};
let mut ctx = Context::new(ctx);
ctx.add_value("event", met);
ctx.add_value("value", self.current.doc.deref());
ctx.add_value("after", self.current.doc.deref());
ctx.add_value("before", self.initial.doc.deref());
let val = ev.when.compute(&ctx, opt, txn, Some(&self.current)).await?;
if val.is_truthy() {
for v in ev.then.iter() {
v.compute(&ctx, opt, txn, Some(&self.current)).await?;
}
}
}
Ok(())
}
}