2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2023-06-20 22:50:26 +00:00
|
|
|
use crate::dbs::Notification;
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Statement;
|
2023-07-06 14:57:42 +00:00
|
|
|
use crate::dbs::{Action, Transaction};
|
2022-02-06 01:14:56 +00:00
|
|
|
use crate::doc::Document;
|
|
|
|
use crate::err::Error;
|
2023-06-20 22:50:26 +00:00
|
|
|
use crate::sql::Value;
|
2021-03-29 15:43:37 +00:00
|
|
|
|
2022-02-13 19:03:00 +00:00
|
|
|
impl<'a> Document<'a> {
|
2022-02-06 01:14:56 +00:00
|
|
|
pub async fn lives(
|
|
|
|
&self,
|
2023-02-22 18:04:20 +00:00
|
|
|
ctx: &Context<'_>,
|
|
|
|
opt: &Options,
|
2023-07-06 14:57:42 +00:00
|
|
|
txn: &Transaction,
|
2023-06-20 22:50:26 +00:00
|
|
|
stm: &Statement<'_>,
|
2022-02-06 01:14:56 +00:00
|
|
|
) -> Result<(), Error> {
|
2023-02-22 18:04:20 +00:00
|
|
|
// Check if forced
|
|
|
|
if !opt.force && !self.changed() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
// Get the record id
|
2023-07-05 21:26:13 +00:00
|
|
|
let rid = self.id.as_ref().unwrap();
|
|
|
|
// Check if we can send notifications
|
|
|
|
if let Some(chn) = &opt.sender {
|
|
|
|
// Clone the sending channel
|
|
|
|
let chn = chn.clone();
|
|
|
|
// Loop through all index statements
|
2023-07-06 14:57:42 +00:00
|
|
|
for lv in self.lv(opt, txn).await?.iter() {
|
2023-07-05 21:26:13 +00:00
|
|
|
// Create a new statement
|
|
|
|
let lq = Statement::from(lv);
|
|
|
|
// Check LIVE SELECT where condition
|
2023-07-06 14:57:42 +00:00
|
|
|
if self.check(ctx, opt, txn, &lq).await.is_err() {
|
2023-07-05 21:26:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Check what type of data change this is
|
|
|
|
if stm.is_delete() {
|
|
|
|
// Send a DELETE notification
|
|
|
|
if opt.id()? == lv.node.0 {
|
|
|
|
let thing = (*rid).clone();
|
|
|
|
chn.send(Notification {
|
2023-06-20 22:50:26 +00:00
|
|
|
id: lv.id.0,
|
|
|
|
action: Action::Delete,
|
|
|
|
result: Value::Thing(thing),
|
|
|
|
})
|
|
|
|
.await?;
|
2023-07-05 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Send to storage
|
|
|
|
}
|
|
|
|
} else if self.is_new() {
|
|
|
|
// Send a CREATE notification
|
|
|
|
if opt.id()? == lv.node.0 {
|
|
|
|
chn.send(Notification {
|
2023-06-20 22:50:26 +00:00
|
|
|
id: lv.id.0,
|
|
|
|
action: Action::Create,
|
2023-07-06 14:57:42 +00:00
|
|
|
result: self.pluck(ctx, opt, txn, &lq).await?,
|
2023-06-20 22:50:26 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2023-07-05 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Send to storage
|
|
|
|
}
|
2023-06-20 22:50:26 +00:00
|
|
|
} else {
|
2023-07-05 21:26:13 +00:00
|
|
|
// Send a UPDATE notification
|
|
|
|
if opt.id()? == lv.node.0 {
|
|
|
|
chn.send(Notification {
|
2023-06-20 22:50:26 +00:00
|
|
|
id: lv.id.0,
|
|
|
|
action: Action::Update,
|
2023-07-06 14:57:42 +00:00
|
|
|
result: self.pluck(ctx, opt, txn, &lq).await?,
|
2023-06-20 22:50:26 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2023-07-05 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Send to storage
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-02-22 18:04:20 +00:00
|
|
|
}
|
|
|
|
// Carry on
|
2022-02-06 01:14:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|