use crate::ctx::Context;
use crate::dbs::Statement;
use crate::dbs::{Options, Transaction};
use crate::doc::Document;
use crate::err::Error;
use crate::key::key_req::KeyRequirements;
impl<'a> Document<'a> {
pub async fn store(
&self,
_ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
stm: &Statement<'_>,
) -> Result<(), Error> {
if !opt.force && !self.changed() {
return Ok(());
}
if self.tb(opt, txn).await?.drop {
return Ok(());
}
let mut run = txn.lock().await;
let rid = self.id.as_ref().unwrap();
let key = crate::key::thing::new(opt.ns(), opt.db(), &rid.tb, &rid.id);
match stm {
Statement::Create(_) => match run.put(key.key_category(), key, self).await {
Err(Error::TxKeyAlreadyExistsCategory(_)) => Err(Error::RecordExists {
thing: rid.to_string(),
}),
Err(e) => Err(e),
Ok(v) => Ok(v),
},
_ => run.set(key, self).await,
}?;
Ok(())
}
}