Struct bonsaidb_core::document::CollectionDocument
source · pub struct CollectionDocument<C>where
C: SerializedCollection,{
pub header: CollectionHeader<C::PrimaryKey>,
pub contents: C::Contents,
}
Expand description
A document with serializable contents.
Fields§
§header: CollectionHeader<C::PrimaryKey>
The header of the document, which contains the id and Revision
.
contents: C::Contents
The document’s contents.
Implementations§
source§impl<C> CollectionDocument<C>where
C: SerializedCollection,
impl<C> CollectionDocument<C>where C: SerializedCollection,
sourcepub fn update<Cn: Connection>(&mut self, connection: &Cn) -> Result<(), Error>
pub fn update<Cn: Connection>(&mut self, connection: &Cn) -> Result<(), Error>
Updates the document stored in the database with the contents of this collection document.
if let Some(mut document) = MyCollection::get(&42, &db)? {
// ... do something `document`
document.update(&db)?;
println!(
"The document has been updated: {:?}",
document.header.revision
);
}
sourcepub fn update_in_transaction(
&self,
transaction: &mut Transaction
) -> Result<(), Error>
pub fn update_in_transaction( &self, transaction: &mut Transaction ) -> Result<(), Error>
Pushes an update Operation
to the transaction for this document.
The changes will happen once the transaction is applied.
sourcepub async fn update_async<Cn: AsyncConnection>(
&mut self,
connection: &Cn
) -> Result<(), Error>
pub async fn update_async<Cn: AsyncConnection>( &mut self, connection: &Cn ) -> Result<(), Error>
Stores the new value of contents
in the document.
if let Some(mut document) = MyCollection::get_async(&42, &db).await? {
// modify the document
document.update_async(&db).await?;
println!("Updated revision: {:?}", document.header.revision);
}
sourcepub fn modify<Cn: Connection, Modifier: FnMut(&mut Self) + Send + Sync>(
&mut self,
connection: &Cn,
modifier: Modifier
) -> Result<(), Error>where
C::Contents: Clone,
pub fn modify<Cn: Connection, Modifier: FnMut(&mut Self) + Send + Sync>( &mut self, connection: &Cn, modifier: Modifier ) -> Result<(), Error>where C::Contents: Clone,
Modifies self
, automatically retrying the modification if the document
has been updated on the server.
Data loss warning
If you’ve modified self
before calling this function and a conflict
occurs, all changes to self will be lost when the current document is
fetched before retrying the process again. When you use this function,
you should limit the edits to the value to within the modifier
callback.
sourcepub async fn modify_async<Cn: AsyncConnection, Modifier: FnMut(&mut Self) + Send + Sync>(
&mut self,
connection: &Cn,
modifier: Modifier
) -> Result<(), Error>where
C::Contents: Clone,
pub async fn modify_async<Cn: AsyncConnection, Modifier: FnMut(&mut Self) + Send + Sync>( &mut self, connection: &Cn, modifier: Modifier ) -> Result<(), Error>where C::Contents: Clone,
Modifies self
, automatically retrying the modification if the document
has been updated on the server.
Data loss warning
If you’ve modified self
before calling this function and a conflict
occurs, all changes to self will be lost when the current document is
fetched before retrying the process again. When you use this function,
you should limit the edits to the value to within the modifier
callback.
sourcepub fn delete<Cn: Connection>(&self, connection: &Cn) -> Result<(), Error>
pub fn delete<Cn: Connection>(&self, connection: &Cn) -> Result<(), Error>
Removes the document from the collection.
if let Some(document) = MyCollection::get(&42, &db)? {
document.delete(&db)?;
}
sourcepub async fn delete_async<Cn: AsyncConnection>(
&self,
connection: &Cn
) -> Result<(), Error>
pub async fn delete_async<Cn: AsyncConnection>( &self, connection: &Cn ) -> Result<(), Error>
Removes the document from the collection.
if let Some(document) = MyCollection::get_async(&42, &db).await? {
document.delete_async(&db).await?;
}
sourcepub fn delete_in_transaction(
&self,
transaction: &mut Transaction
) -> Result<(), Error>
pub fn delete_in_transaction( &self, transaction: &mut Transaction ) -> Result<(), Error>
Pushes a delete Operation
to the transaction for this document.
The document will be deleted once the transaction is applied.
sourcepub fn refresh<Cn: Connection>(&mut self, connection: &Cn) -> Result<(), Error>
pub fn refresh<Cn: Connection>(&mut self, connection: &Cn) -> Result<(), Error>
Refreshes this instance from connection
. If the document is no longer
present, Error::DocumentNotFound
will be returned.
sourcepub async fn refresh_async<Cn: AsyncConnection>(
&mut self,
connection: &Cn
) -> Result<(), Error>
pub async fn refresh_async<Cn: AsyncConnection>( &mut self, connection: &Cn ) -> Result<(), Error>
Refreshes this instance from connection
. If the document is no longer
present, Error::DocumentNotFound
will be returned.
sourcepub fn to_document(&self) -> Result<OwnedDocument, Error>
pub fn to_document(&self) -> Result<OwnedDocument, Error>
Converts this value to a serialized Document
.
Trait Implementations§
source§impl<C> Clone for CollectionDocument<C>where
C: SerializedCollection + Clone,
C::PrimaryKey: Clone,
C::Contents: Clone,
impl<C> Clone for CollectionDocument<C>where C: SerializedCollection + Clone, C::PrimaryKey: Clone, C::Contents: Clone,
source§fn clone(&self) -> CollectionDocument<C>
fn clone(&self) -> CollectionDocument<C>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<C> Debug for CollectionDocument<C>where
C: SerializedCollection + Debug,
C::PrimaryKey: Debug,
C::Contents: Debug,
impl<C> Debug for CollectionDocument<C>where C: SerializedCollection + Debug, C::PrimaryKey: Debug, C::Contents: Debug,
source§impl<'de, C> Deserialize<'de> for CollectionDocument<C>where
C: SerializedCollection,
C::PrimaryKey: Deserialize<'de>,
C::Contents: Deserialize<'de>,
impl<'de, C> Deserialize<'de> for CollectionDocument<C>where C: SerializedCollection, C::PrimaryKey: Deserialize<'de>, C::Contents: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<C> HasHeader for CollectionDocument<C>where
C: SerializedCollection,
impl<C> HasHeader for CollectionDocument<C>where C: SerializedCollection,
source§impl<'a, C> Nameable<'a, <C as Collection>::PrimaryKey> for &'a CollectionDocument<C>where
C: SerializedCollection,
impl<'a, C> Nameable<'a, <C as Collection>::PrimaryKey> for &'a CollectionDocument<C>where C: SerializedCollection,
source§fn name(self) -> Result<NamedReference<'a, C::PrimaryKey>, Error>
fn name(self) -> Result<NamedReference<'a, C::PrimaryKey>, Error>
NamedReference
.source§impl<C> PartialEq<CollectionDocument<C>> for CollectionDocument<C>where
C: SerializedCollection + PartialEq,
C::PrimaryKey: PartialEq,
C::Contents: PartialEq,
impl<C> PartialEq<CollectionDocument<C>> for CollectionDocument<C>where C: SerializedCollection + PartialEq, C::PrimaryKey: PartialEq, C::Contents: PartialEq,
source§fn eq(&self, other: &CollectionDocument<C>) -> bool
fn eq(&self, other: &CollectionDocument<C>) -> bool
self
and other
values to be equal, and is used
by ==
.