Trait bonsaidb::core::schema::SerializedCollection[][src]

pub trait SerializedCollection: Collection {
    type Contents: Send + Sync;
    type Format: OwnedDeserializer<Self::Contents>;
Show 28 methods fn format() -> Self::Format; fn natural_id(contents: &Self::Contents) -> Option<Self::PrimaryKey> { ... }
fn deserialize(data: &[u8]) -> Result<Self::Contents, Error> { ... }
fn document_contents<D>(doc: &D) -> Result<Self::Contents, Error>
    where
        D: Document<Self>
, { ... }
fn set_document_contents<D>(
        doc: &mut D,
        contents: Self::Contents
    ) -> Result<(), Error>
    where
        D: Document<Self>
, { ... }
fn serialize(item: &Self::Contents) -> Result<Vec<u8, Global>, Error> { ... }
fn get<C, PrimaryKey>(
        id: PrimaryKey,
        connection: &C
    ) -> Result<Option<CollectionDocument<Self>>, Error>
    where
        C: Connection,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>
, { ... }
fn get_async<'life0, 'async_trait, C, PrimaryKey>(
        id: PrimaryKey,
        connection: &'life0 C
    ) -> Pin<Box<dyn Future<Output = Result<Option<CollectionDocument<Self>>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        C: AsyncConnection + 'async_trait,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey> + 'async_trait,
        Self: 'async_trait + Send
, { ... }
fn get_multiple<C, DocumentIds, PrimaryKey, I>(
        ids: DocumentIds,
        connection: &C
    ) -> Result<Vec<CollectionDocument<Self>, Global>, Error>
    where
        C: Connection,
        DocumentIds: IntoIterator<Item = PrimaryKey, IntoIter = I> + Send + Sync,
        I: Iterator<Item = PrimaryKey> + Send + Sync,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>
, { ... }
fn get_multiple_async<'life0, 'async_trait, C, DocumentIds, PrimaryKey, I>(
        ids: DocumentIds,
        connection: &'life0 C
    ) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>, Global>, Error>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        C: AsyncConnection + 'async_trait,
        DocumentIds: IntoIterator<Item = PrimaryKey, IntoIter = I> + Send + Sync + 'async_trait,
        I: Iterator<Item = PrimaryKey> + Send + Sync + 'async_trait,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey> + 'async_trait,
        Self: 'async_trait + Send
, { ... }
fn list<R, PrimaryKey, C>(
        ids: R,
        connection: &C
    ) -> List<'_, C, Self, PrimaryKey>
    where
        R: Into<Range<PrimaryKey>>,
        C: Connection,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>
, { ... }
fn list_async<R, PrimaryKey, C>(
        ids: R,
        connection: &C
    ) -> AsyncList<'_, C, Self, PrimaryKey>
Notable traits for AsyncList<'a, Cn, Cl, PrimaryKey>
impl<'a, Cn, Cl, PrimaryKey> Future for AsyncList<'a, Cn, Cl, PrimaryKey> where
    Cl: SerializedCollection + Unpin,
    Cn: AsyncConnection,
    PrimaryKey: 'a + for<'k> KeyEncoding<'k, <Cl as Collection>::PrimaryKey> + Unpin,
    <Cl as Collection>::PrimaryKey: Unpin
type Output = Result<Vec<CollectionDocument<Cl>, Global>, Error>;

    where
        R: Into<Range<PrimaryKey>>,
        C: AsyncConnection,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>
, { ... }
fn list_with_prefix<C>(
        prefix: Self::PrimaryKey,
        connection: &C
    ) -> List<'_, C, Self, Self::PrimaryKey>
    where
        C: Connection,
        Self::PrimaryKey: IntoPrefixRange
, { ... }
fn list_with_prefix_async<C>(
        prefix: Self::PrimaryKey,
        connection: &C
    ) -> AsyncList<'_, C, Self, Self::PrimaryKey>
Notable traits for AsyncList<'a, Cn, Cl, PrimaryKey>
impl<'a, Cn, Cl, PrimaryKey> Future for AsyncList<'a, Cn, Cl, PrimaryKey> where
    Cl: SerializedCollection + Unpin,
    Cn: AsyncConnection,
    PrimaryKey: 'a + for<'k> KeyEncoding<'k, <Cl as Collection>::PrimaryKey> + Unpin,
    <Cl as Collection>::PrimaryKey: Unpin
type Output = Result<Vec<CollectionDocument<Cl>, Global>, Error>;

    where
        C: AsyncConnection,
        Self::PrimaryKey: IntoPrefixRange
, { ... }
fn all<C>(connection: &C) -> List<'_, C, Self, Self::PrimaryKey>
    where
        C: Connection
, { ... }
fn all_async<C>(connection: &C) -> AsyncList<'_, C, Self, Self::PrimaryKey>
Notable traits for AsyncList<'a, Cn, Cl, PrimaryKey>
impl<'a, Cn, Cl, PrimaryKey> Future for AsyncList<'a, Cn, Cl, PrimaryKey> where
    Cl: SerializedCollection + Unpin,
    Cn: AsyncConnection,
    PrimaryKey: 'a + for<'k> KeyEncoding<'k, <Cl as Collection>::PrimaryKey> + Unpin,
    <Cl as Collection>::PrimaryKey: Unpin
type Output = Result<Vec<CollectionDocument<Cl>, Global>, Error>;

    where
        C: AsyncConnection
, { ... }
fn push<Cn>(
        contents: Self::Contents,
        connection: &Cn
    ) -> Result<CollectionDocument<Self>, InsertError<Self::Contents>>
    where
        Cn: Connection,
        Self: 'static
, { ... }
fn push_async<'life0, 'async_trait, Cn>(
        contents: Self::Contents,
        connection: &'life0 Cn
    ) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'static + 'async_trait + Send,
        Cn: 'async_trait + AsyncConnection,
        Self::Contents: 'async_trait
, { ... }
fn push_into<Cn>(
        self,
        connection: &Cn
    ) -> Result<CollectionDocument<Self>, InsertError<Self>>
    where
        Cn: Connection,
        Self: 'static + SerializedCollection<Contents = Self>
, { ... }
fn push_into_async<'life0, 'async_trait, Cn>(
        self,
        connection: &'life0 Cn
    ) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        Self: 'static + SerializedCollection<Contents = Self> + 'async_trait + Send,
        Cn: 'async_trait + AsyncConnection
, { ... }
fn insert<PrimaryKey, Cn>(
        id: PrimaryKey,
        contents: Self::Contents,
        connection: &Cn
    ) -> Result<CollectionDocument<Self>, InsertError<Self::Contents>>
    where
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>,
        Cn: Connection,
        Self: 'static
, { ... }
fn insert_async<'life0, 'async_trait, PrimaryKey, Cn>(
        id: PrimaryKey,
        contents: Self::Contents,
        connection: &'life0 Cn
    ) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey> + 'async_trait,
        Cn: AsyncConnection + 'async_trait,
        Self: 'static + 'async_trait + Send,
        Self::Contents: 'async_trait
, { ... }
fn insert_into<PrimaryKey, Cn>(
        self,
        id: PrimaryKey,
        connection: &Cn
    ) -> Result<CollectionDocument<Self>, InsertError<Self>>
    where
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>,
        Cn: Connection,
        Self: 'static + SerializedCollection<Contents = Self>
, { ... }
fn insert_into_async<'life0, 'async_trait, PrimaryKey, Cn>(
        self,
        id: PrimaryKey,
        connection: &'life0 Cn
    ) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey> + 'async_trait,
        Cn: AsyncConnection + 'async_trait,
        Self: 'static + SerializedCollection<Contents = Self> + 'async_trait + Send
, { ... }
fn overwrite<PrimaryKey, Cn>(
        id: PrimaryKey,
        contents: Self::Contents,
        connection: &Cn
    ) -> Result<CollectionDocument<Self>, InsertError<Self::Contents>>
    where
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>,
        Cn: Connection,
        Self: 'static
, { ... }
fn overwrite_async<'life0, 'async_trait, PrimaryKey, Cn>(
        id: PrimaryKey,
        contents: Self::Contents,
        connection: &'life0 Cn
    ) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey> + 'async_trait,
        Cn: AsyncConnection + 'async_trait,
        Self: 'static + 'async_trait + Send,
        Self::Contents: 'async_trait
, { ... }
fn overwrite_into<Cn, PrimaryKey>(
        self,
        id: PrimaryKey,
        connection: &Cn
    ) -> Result<CollectionDocument<Self>, InsertError<Self>>
    where
        Cn: Connection,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey>,
        Self: 'static + SerializedCollection<Contents = Self>
, { ... }
fn overwrite_into_async<'life0, 'async_trait, Cn, PrimaryKey>(
        self,
        id: PrimaryKey,
        connection: &'life0 Cn
    ) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait, Global>>
    where
        'life0: 'async_trait,
        PrimaryKey: for<'k> KeyEncoding<'k, Self::PrimaryKey> + 'async_trait,
        Self: 'static + SerializedCollection<Contents = Self> + 'async_trait + Send,
        Cn: 'async_trait + AsyncConnection
, { ... }
}
Expand description

A collection that knows how to serialize and deserialize documents to an associated type.

These examples for this type use this basic collection definition:

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

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

Associated Types

The type of the contents stored in documents in this collection.

The serialization format for this collection.

Required methods

Returns the configured instance of Self::Format.

Provided methods

Returns the natural identifier of contents. This is called when pushing values into a collection, before attempting to automatically assign a unique id.

Deserialize data as Self::Contents using this collection’s format.

Returns the deserialized contents of doc.

Sets the contents of doc to contents.

Serialize item using this collection’s format.

Gets a CollectionDocument with id from connection.

if let Some(doc) = MyCollection::get(42, &db)? {
    println!(
        "Retrieved revision {} with deserialized contents: {:?}",
        doc.header.revision, doc.contents
    );
}

Gets a CollectionDocument with id from connection.

if let Some(doc) = MyCollection::get_async(42, &db).await? {
    println!(
        "Retrieved revision {} with deserialized contents: {:?}",
        doc.header.revision, doc.contents
    );
}

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

for doc in MyCollection::get_multiple([42, 43], &db)? {
    println!(
        "Retrieved #{} with deserialized contents: {:?}",
        doc.header.id, doc.contents
    );
}

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

for doc in MyCollection::get_multiple_async([42, 43], &db).await? {
    println!(
        "Retrieved #{} with deserialized contents: {:?}",
        doc.header.id, doc.contents
    );
}

Retrieves all documents matching the range of ids.

for doc in MyCollection::list(42.., &db)
    .descending()
    .limit(20)
    .query()?
{
    println!(
        "Retrieved #{} with deserialized contents: {:?}",
        doc.header.id, doc.contents
    );
}

Retrieves all documents matching the range of ids.

for doc in MyCollection::list_async(42.., &db)
    .descending()
    .limit(20)
    .await?
{
    println!(
        "Retrieved #{} with deserialized contents: {:?}",
        doc.header.id, doc.contents
    );
}

Retrieves all documents with ids that start with prefix.

use bonsaidb_core::{
    connection::Connection,
    document::CollectionDocument,
    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<CollectionDocument<MyCollection>>, Error> {
    MyCollection::list_with_prefix(String::from("a"), db).query()
}

Retrieves all documents with ids that start with prefix.

use bonsaidb_core::{
    connection::AsyncConnection,
    document::CollectionDocument,
    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: AsyncConnection>(
    db: &C,
) -> Result<Vec<CollectionDocument<MyCollection>>, Error> {
    MyCollection::list_with_prefix_async(String::from("a"), db).await
}

Retrieves all documents.

for doc in MyCollection::all(&db).query()? {
    println!(
        "Retrieved #{} with deserialized contents: {:?}",
        doc.header.id, doc.contents
    );
}

Retrieves all documents.

for doc in MyCollection::all_async(&db).await? {
    println!(
        "Retrieved #{} with deserialized contents: {:?}",
        doc.header.id, doc.contents
    );
}

Pushes this value into the collection, returning the created document. This function is useful when Self != Self::Contents.

Automatic ID Assignment

This function calls Self::natural_id() to try to retrieve a primary key value from contents. 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 document = MyCollection::push(MyCollection::default(), &db)?;
println!(
    "Inserted {:?} with id {} with revision {}",
    document.contents, document.header.id, document.header.revision
);

Pushes this value into the collection, returning the created document. This function is useful when Self != Self::Contents.

Automatic ID Assignment

This function calls Self::natural_id() to try to retrieve a primary key value from contents. 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 document = MyCollection::push_async(MyCollection::default(), &db).await?;
println!(
    "Inserted {:?} with id {} with revision {}",
    document.contents, document.header.id, document.header.revision
);

Pushes this value into the collection, returning the created document.

Automatic ID Assignment

This function calls Self::natural_id() to try to retrieve a primary key value from self. 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 document = MyCollection::default().push_into(&db)?;
println!(
    "Inserted {:?} with id {} with revision {}",
    document.contents, document.header.id, document.header.revision
);

Pushes this value into the collection, returning the created document.

Automatic ID Assignment

This function calls Self::natural_id() to try to retrieve a primary key value from self. 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 document = MyCollection::default().push_into_async(&db).await?;
println!(
    "Inserted {:?} with id {} with revision {}",
    document.contents, document.header.id, document.header.revision
);

Inserts this value into the collection with the specified id, returning the created document.

let document = MyCollection::insert(42, MyCollection::default(), &db)?;
assert_eq!(document.header.id, 42);
println!(
    "Inserted {:?} with revision {}",
    document.contents, document.header.revision
);

Inserts this value into the collection with the specified id, returning the created document.

let document = MyCollection::insert_async(42, MyCollection::default(), &db).await?;
assert_eq!(document.header.id, 42);
println!(
    "Inserted {:?} with revision {}",
    document.contents, document.header.revision
);

Inserts this value into the collection with the given id, returning the created document.

let document = MyCollection::default().insert_into(42, &db)?;
assert_eq!(document.header.id, 42);
println!(
    "Inserted {:?} with revision {}",
    document.contents, document.header.revision
);

Inserts this value into the collection with the given id, returning the created document.

let document = MyCollection::default().insert_into_async(42, &db).await?;
assert_eq!(document.header.id, 42);
println!(
    "Inserted {:?} with revision {}",
    document.contents, document.header.revision
);

Overwrites this value into the collection with the specified id, returning the created or updated document.

let document = MyCollection::overwrite(42, MyCollection::default(), &db)?;
assert_eq!(document.header.id, 42);
println!(
    "Overwrote {:?} with revision {}",
    document.contents, document.header.revision
);

Overwrites this value into the collection with the specified id, returning the created or updated document.

let document = MyCollection::overwrite_async(42, MyCollection::default(), &db).await?;
assert_eq!(document.header.id, 42);
println!(
    "Overwrote {:?} with revision {}",
    document.contents, document.header.revision
);

Overwrites this value into the collection with the given id, returning the created or updated document.

let document = MyCollection::default().overwrite_into(42, &db)?;
assert_eq!(document.header.id, 42);
println!(
    "Overwrote {:?} with revision {}",
    document.contents, document.header.revision
);

Overwrites this value into the collection with the given id, returning the created or updated document.

let document = MyCollection::default()
    .overwrite_into_async(42, &db)
    .await?;
assert_eq!(document.header.id, 42);
println!(
    "Overwrote {:?} with revision {}",
    document.contents, document.header.revision
);

Implementors