Trait bonsaidb_core::schema::SerializedCollection
source · [−]pub trait SerializedCollection: Collection {
type Contents: Send + Sync;
type Format: OwnedDeserializer<Self::Contents>;
fn format() -> Self::Format;
fn deserialize(data: &[u8]) -> Result<Self::Contents, Error> { ... }
fn serialize(item: &Self::Contents) -> Result<Vec<u8>, Error> { ... }
fn get<'life0, 'async_trait, C: Connection>(
id: u64,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Option<CollectionDocument<Self>>, Error>> + Send + 'async_trait>>
where
Self: Sized,
C: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn get_multiple<'life0, 'life1, 'async_trait, C: Connection>(
ids: &'life0 [u64],
connection: &'life1 C
) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>>, Error>> + Send + 'async_trait>>
where
Self: Sized,
C: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn list<R: Into<Range<u64>>, C: Connection>(
ids: R,
connection: &C
) -> List<'_, C, Self>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>;
where
Self: Sized,
{ ... }
fn all<C: Connection>(connection: &C) -> List<'_, C, Self>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>;
where
Self: Sized,
{ ... }
fn push<'life0, 'async_trait, Cn: Connection>(
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>>
where
Self: Sized + 'static,
Self::Contents: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn push_into<'life0, 'async_trait, Cn: Connection>(
self,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>>
where
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn insert<'life0, 'async_trait, Cn: Connection>(
id: u64,
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>>
where
Self: Sized + 'static,
Self::Contents: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn insert_into<'life0, 'async_trait, Cn: Connection>(
self,
id: u64,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>>
where
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
}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, CollectionName, 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
type Format: OwnedDeserializer<Self::Contents>
type Format: OwnedDeserializer<Self::Contents>
The serialization format for this collection.
Required methods
Returns the configured instance of Self::Format.
Provided methods
Deserialize data as Self::Contents using this collection’s format.
Serialize item using this collection’s format.
Gets a CollectionDocument with id from connection.
if let Some(doc) = MyCollection::get(42, &db).await? {
println!(
"Retrieved revision {} with deserialized contents: {:?}",
doc.header.revision, doc.contents
);
}fn get_multiple<'life0, 'life1, 'async_trait, C: Connection>(
ids: &'life0 [u64],
connection: &'life1 C
) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>>, Error>> + Send + 'async_trait>> where
Self: Sized,
C: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: Send + 'async_trait,
fn get_multiple<'life0, 'life1, 'async_trait, C: Connection>(
ids: &'life0 [u64],
connection: &'life1 C
) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>>, Error>> + Send + 'async_trait>> where
Self: Sized,
C: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: Send + 'async_trait,
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).await? {
println!(
"Retrieved #{} with deserialized contents: {:?}",
doc.header.id, doc.contents
);
}fn list<R: Into<Range<u64>>, C: Connection>(
ids: R,
connection: &C
) -> List<'_, C, Self>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
Self: Sized,
fn list<R: Into<Range<u64>>, C: Connection>(
ids: R,
connection: &C
) -> List<'_, C, Self>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
Self: Sized,
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>;
Retrieves all documents matching the range of ids.
for doc in MyCollection::list(42.., &db).descending().limit(20).await? {
println!(
"Retrieved #{} with deserialized contents: {:?}",
doc.header.id, doc.contents
);
}fn all<C: Connection>(connection: &C) -> List<'_, C, Self>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
Self: Sized,
fn all<C: Connection>(connection: &C) -> List<'_, C, Self>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
Self: Sized,
Cl: SerializedCollection + Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>;
Retrieves all documents.
for doc in MyCollection::all(&db).await? {
println!(
"Retrieved #{} with deserialized contents: {:?}",
doc.header.id, doc.contents
);
}fn push<'life0, 'async_trait, Cn: Connection>(
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>> where
Self: Sized + 'static,
Self::Contents: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn push<'life0, 'async_trait, Cn: Connection>(
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>> where
Self: Sized + 'static,
Self::Contents: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
Pushes this value into the collection, returning the created document.
This function is useful when Self != Self::Contents.
let document = MyCollection::push(MyCollection::default(), &db).await?;
println!(
"Inserted {:?} with id {} with revision {}",
document.contents, document.header.id, document.header.revision
);fn push_into<'life0, 'async_trait, Cn: Connection>(
self,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn push_into<'life0, 'async_trait, Cn: Connection>(
self,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
Pushes this value into the collection, returning the created document.
let document = MyCollection::default().push_into(&db).await?;
println!(
"Inserted {:?} with id {} with revision {}",
document.contents, document.header.id, document.header.revision
);fn insert<'life0, 'async_trait, Cn: Connection>(
id: u64,
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>> where
Self: Sized + 'static,
Self::Contents: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn insert<'life0, 'async_trait, Cn: Connection>(
id: u64,
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>> where
Self: Sized + 'static,
Self::Contents: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
Inserts this value into the collection with the specified id, returning the created document.
let document = MyCollection::insert(42, MyCollection::default(), &db).await?;
assert_eq!(document.header.id, 42);
println!(
"Inserted {:?} with revision {}",
document.contents, document.header.revision
);fn insert_into<'life0, 'async_trait, Cn: Connection>(
self,
id: u64,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn insert_into<'life0, 'async_trait, Cn: Connection>(
self,
id: u64,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
Inserts this value into the collection with the given id, returning
the created document.
let document = MyCollection::default().insert_into(42, &db).await?;
assert_eq!(document.header.id, 42);
println!(
"Inserted {:?} with revision {}",
document.contents, document.header.revision
);