Add function for checking if record already exists

This commit is contained in:
Tobie Morgan Hitchcock 2022-04-01 00:35:35 +01:00
parent 9f7527c01a
commit c57c313c47
3 changed files with 30 additions and 0 deletions

View file

@ -16,6 +16,8 @@ impl<'a> Document<'a> {
) -> Result<Value, Error> {
// Check value type
self.admit(ctx, opt, txn, stm).await?;
// Check if exists
self.exist(ctx, opt, txn, stm).await?;
// Merge record data
self.merge(ctx, opt, txn, stm).await?;
// Check if allowed

27
lib/src/doc/exist.rs Normal file
View file

@ -0,0 +1,27 @@
use crate::dbs::Options;
use crate::dbs::Runtime;
use crate::dbs::Statement;
use crate::dbs::Transaction;
use crate::doc::Document;
use crate::err::Error;
impl<'a> Document<'a> {
pub async fn exist(
&self,
_ctx: &Runtime,
_opt: &Options,
_txn: &Transaction,
_stm: &Statement,
) -> Result<(), Error> {
// Check if this record exists
if let Some(id) = &self.id {
if self.current.is_some() {
return Err(Error::RecordExists {
thing: id.clone(),
});
}
}
// Carry on
Ok(())
}
}

View file

@ -10,6 +10,7 @@ mod document;
mod empty;
mod erase;
mod event;
mod exist;
mod grant;
mod index;
mod insert;