pub struct AsyncCollection<'a, Cn, Cl> { /* private fields */ }
Expand description

Interacts with a collection over a Connection.

These examples in this type use this basic collection definition:

use bonsaidb_core::schema::Collection;
use bonsaidb_core::Error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Default, Collection)]
#[collection(name = "MyCollection")]
pub struct MyCollection {
    pub rank: u32,
    pub score: f32,
}

Implementations§

source§

impl<'a, Cn, Cl> AsyncCollection<'a, Cn, Cl>
where Cn: AsyncConnection, Cl: Collection,

source

pub async fn push( &self, item: &<Cl as SerializedCollection>::Contents ) -> Result<CollectionHeader<<Cl as Collection>::PrimaryKey>, Error>

Adds a new Document<Cl> with the contents item.

Automatic ID Assignment

This function calls SerializedCollection::natural_id() to try to retrieve a primary key value from item. If an id is returned, the item is inserted with that id. If an id is not returned, an id will be automatically assigned, if possible, by the storage backend, which uses the Key trait to assign ids.

let inserted_header = db
    .collection::<MyCollection>()
    .push(&MyCollection::default())
    .await?;
println!(
    "Inserted id {} with revision {}",
    inserted_header.id, inserted_header.revision
);
source

pub async fn push_bytes<B>( &self, contents: B ) -> Result<CollectionHeader<<Cl as Collection>::PrimaryKey>, Error>
where B: Into<Bytes> + Send,

Adds a new Document<Cl> with the contents.

Automatic ID Assignment

An id will be automatically assigned, if possible, by the storage backend, which uses the Key trait to assign ids.

let inserted_header = db.collection::<MyCollection>().push_bytes(vec![]).await?;
println!(
    "Inserted id {} with revision {}",
    inserted_header.id, inserted_header.revision
);
source

pub async fn insert<PrimaryKey>( &self, id: &PrimaryKey, item: &<Cl as SerializedCollection>::Contents ) -> Result<CollectionHeader<<Cl as Collection>::PrimaryKey>, Error>
where Cl: SerializedCollection, PrimaryKey: KeyEncoding<<Cl as Collection>::PrimaryKey> + ?Sized,

Adds a new Document<Cl> with the given id and contents item.

let inserted_header = db
    .collection::<MyCollection>()
    .insert(&42, &MyCollection::default())
    .await?;
println!(
    "Inserted id {} with revision {}",
    inserted_header.id, inserted_header.revision
);
source

pub async fn insert_bytes<PrimaryKey, B>( &self, id: &PrimaryKey, contents: B ) -> Result<CollectionHeader<<Cl as Collection>::PrimaryKey>, Error>
where B: Into<Bytes> + Send, PrimaryKey: KeyEncoding<<Cl as Collection>::PrimaryKey> + ?Sized,

Adds a new Document<Cl> with the the given id and contents.

let inserted_header = db
    .collection::<MyCollection>()
    .insert_bytes(&42, vec![])
    .await?;
println!(
    "Inserted id {} with revision {}",
    inserted_header.id, inserted_header.revision
);
source

pub async fn update<D>(&self, doc: &mut D) -> Result<(), Error>
where D: Document<Cl> + Send + Sync,

Updates an existing document. Upon success, doc.revision will be updated with the new revision.

if let Some(mut document) = db.collection::<MyCollection>().get(&42).await? {
    // modify the document
    db.collection::<MyCollection>().update(&mut document);
    println!("Updated revision: {:?}", document.header.revision);
}
source

pub async fn overwrite<D>(&self, doc: &mut D) -> Result<(), Error>
where D: Document<Cl> + Send + Sync,

Overwrites an existing document, or inserts a new document. Upon success, doc.revision will be updated with the new revision information.

if let Some(mut document) = db.collection::<MyCollection>().get(&42).await? {
    // modify the document
    db.collection::<MyCollection>().overwrite(&mut document);
    println!("Updated revision: {:?}", document.header.revision);
}
source

pub async fn get<PrimaryKey>( &self, id: &PrimaryKey ) -> Result<Option<OwnedDocument>, Error>
where PrimaryKey: KeyEncoding<<Cl as Collection>::PrimaryKey> + ?Sized,

Retrieves a Document<Cl> with id from the connection.

if let Some(doc) = db.collection::<MyCollection>().get(&42).await? {
    println!(
        "Retrieved bytes {:?} with revision {}",
        doc.contents, doc.header.revision
    );
    let deserialized = MyCollection::document_contents(&doc)?;
    println!("Deserialized contents: {:?}", deserialized);
}
source

pub async fn get_multiple<'id, DocumentIds, PrimaryKey, I>( &self, ids: DocumentIds ) -> Result<Vec<OwnedDocument>, Error>
where DocumentIds: IntoIterator<Item = &'id PrimaryKey, IntoIter = I> + Send + Sync, I: Iterator<Item = &'id PrimaryKey> + Send + Sync, PrimaryKey: KeyEncoding<<Cl as Collection>::PrimaryKey> + 'id + ?Sized,

Retrieves all documents matching ids. Documents that are not found are not returned, but no error will be generated.

for doc in db
    .collection::<MyCollection>()
    .get_multiple(&[42, 43])
    .await?
{
    println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
    let deserialized = MyCollection::document_contents(&doc)?;
    println!("Deserialized contents: {:?}", deserialized);
}
source

pub fn list<PrimaryKey, R>( &'a self, ids: R ) -> AsyncList<'a, Cn, Cl, PrimaryKey>
where R: Into<RangeRef<'a, <Cl as Collection>::PrimaryKey, PrimaryKey>>, PrimaryKey: KeyEncoding<<Cl as Collection>::PrimaryKey> + PartialEq + ?Sized, <Cl as Collection>::PrimaryKey: Borrow<PrimaryKey> + PartialEq<PrimaryKey>,

Retrieves all documents matching the range of ids.

for doc in db
    .collection::<MyCollection>()
    .list(42..)
    .descending()
    .limit(20)
    .await?
{
    println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
    let deserialized = MyCollection::document_contents(&doc)?;
    println!("Deserialized contents: {:?}", deserialized);
}
source

pub fn list_with_prefix<PrimaryKey>( &'a self, prefix: &'a PrimaryKey ) -> AsyncList<'a, Cn, Cl, PrimaryKey>
where PrimaryKey: IntoPrefixRange<'a, <Cl as Collection>::PrimaryKey> + KeyEncoding<<Cl as Collection>::PrimaryKey> + PartialEq + ?Sized, <Cl as Collection>::PrimaryKey: Borrow<PrimaryKey> + PartialEq<PrimaryKey>,

Retrieves all documents with ids that start with prefix.

use bonsaidb_core::connection::AsyncConnection;
use bonsaidb_core::document::OwnedDocument;
use bonsaidb_core::schema::{Collection, Schematic, SerializedCollection};
use bonsaidb_core::Error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Default, Collection)]
#[collection(name = "MyCollection", primary_key = String)]
pub struct MyCollection;

async fn starts_with_a<C: AsyncConnection>(db: &C) -> Result<Vec<OwnedDocument>, Error> {
    db.collection::<MyCollection>().list_with_prefix("a").await
}
source

pub fn all(&'a self) -> AsyncList<'a, Cn, Cl, <Cl as Collection>::PrimaryKey>

Retrieves all documents.

for doc in db.collection::<MyCollection>().all().await? {
    println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
    let deserialized = MyCollection::document_contents(&doc)?;
    println!("Deserialized contents: {:?}", deserialized);
}
source

pub async fn delete<H>(&self, doc: &H) -> Result<(), Error>
where H: HasHeader + Send + Sync,

Removes a Document from the database.

if let Some(doc) = db.collection::<MyCollection>().get(&42).await? {
    db.collection::<MyCollection>().delete(&doc).await?;
}

Trait Implementations§

source§

impl<'a, Cn, Cl> Clone for AsyncCollection<'a, Cn, Cl>

source§

fn clone(&self) -> AsyncCollection<'a, Cn, Cl>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<'a, Cn, Cl> RefUnwindSafe for AsyncCollection<'a, Cn, Cl>

§

impl<'a, Cn, Cl> Send for AsyncCollection<'a, Cn, Cl>
where Cl: Send, Cn: Sync,

§

impl<'a, Cn, Cl> Sync for AsyncCollection<'a, Cn, Cl>
where Cl: Sync, Cn: Sync,

§

impl<'a, Cn, Cl> Unpin for AsyncCollection<'a, Cn, Cl>
where Cl: Unpin,

§

impl<'a, Cn, Cl> UnwindSafe for AsyncCollection<'a, Cn, Cl>
where Cl: UnwindSafe, Cn: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more