pub struct Collection<'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, Error};
use serde::{Deserialize, Serialize};

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

Implementations

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
);

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
);

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
);

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
);

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);
}

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);
}

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);
}

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);
}

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);
}

Retrieves all documents with ids that start with prefix.

use bonsaidb_core::{
    connection::Connection,
    document::OwnedDocument,
    schema::{Collection, Schematic, SerializedCollection},
    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: Connection>(db: &C) -> Result<Vec<OwnedDocument>, Error> {
    db.collection::<MyCollection>()
        .list_with_prefix(String::from("a"))
        .await
}

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);
}

Removes a Document from the database.

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

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

Calls U::from(self).

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

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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