Trait bonsaidb_core::schema::SerializedCollection
source · [−]pub trait SerializedCollection: Collection {
type Contents: Send + Sync;
type Format: OwnedDeserializer<Self::Contents>;
Show 16 methods
fn format() -> Self::Format;
fn natural_id(contents: &Self::Contents) -> Option<Self::PrimaryKey>
where
Self: Sized,
{ ... }
fn deserialize(data: &[u8]) -> Result<Self::Contents, Error> { ... }
fn document_contents<D: Document<Self>>(
doc: &D
) -> Result<Self::Contents, Error>
where
Self: Sized,
{ ... }
fn set_document_contents<D: Document<Self>>(
doc: &mut D,
contents: Self::Contents
) -> Result<(), Error>
where
Self: Sized,
{ ... }
fn serialize(item: &Self::Contents) -> Result<Vec<u8>, Error> { ... }
fn get<'life0, 'async_trait, C, PrimaryKey>(
id: PrimaryKey,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Option<CollectionDocument<Self>>, Error>> + Send + 'async_trait>>
where
C: Connection,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send,
Self: Sized,
C: 'async_trait,
PrimaryKey: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn get_multiple<'life0, 'async_trait, C, DocumentIds, PrimaryKey, I>(
ids: DocumentIds,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>>, Error>> + Send + 'async_trait>>
where
C: Connection,
DocumentIds: IntoIterator<Item = PrimaryKey, IntoIter = I> + Send + Sync,
I: Iterator<Item = PrimaryKey> + Send + Sync,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: Sized,
C: 'async_trait,
DocumentIds: 'async_trait,
PrimaryKey: 'async_trait,
I: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn list<R, PrimaryKey, C>(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,
Cl::PrimaryKey: Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>;
where
R: Into<Range<PrimaryKey>>,
C: Connection,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
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,
Cl::PrimaryKey: 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, PrimaryKey, Cn>(
id: PrimaryKey,
contents: Self::Contents,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self::Contents>>> + Send + 'async_trait>>
where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Cn: Connection,
Self: Sized + 'static,
Self::Contents: 'async_trait,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn insert_into<'life0, 'async_trait, PrimaryKey, Cn>(
self,
id: PrimaryKey,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>>
where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Cn: Connection,
Self: SerializedCollection<Contents = Self> + Sized + 'static,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn overwrite<'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>>
where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send,
Cn: Connection,
Self: Sized + 'static,
Self::Contents: 'async_trait,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
{ ... }
fn overwrite_into<'life0, 'async_trait, Cn: Connection, PrimaryKey>(
self,
id: PrimaryKey,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>>
where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
PrimaryKey: '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
fn natural_id(contents: &Self::Contents) -> Option<Self::PrimaryKey> where
Self: Sized,
fn natural_id(contents: &Self::Contents) -> Option<Self::PrimaryKey> where
Self: Sized,
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.
fn get<'life0, 'async_trait, C, PrimaryKey>(
id: PrimaryKey,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Option<CollectionDocument<Self>>, Error>> + Send + 'async_trait>> where
C: Connection,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send,
Self: Sized,
C: 'async_trait,
PrimaryKey: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn get<'life0, 'async_trait, C, PrimaryKey>(
id: PrimaryKey,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Option<CollectionDocument<Self>>, Error>> + Send + 'async_trait>> where
C: Connection,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send,
Self: Sized,
C: 'async_trait,
PrimaryKey: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
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, 'async_trait, C, DocumentIds, PrimaryKey, I>(
ids: DocumentIds,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>>, Error>> + Send + 'async_trait>> where
C: Connection,
DocumentIds: IntoIterator<Item = PrimaryKey, IntoIter = I> + Send + Sync,
I: Iterator<Item = PrimaryKey> + Send + Sync,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: Sized,
C: 'async_trait,
DocumentIds: 'async_trait,
PrimaryKey: 'async_trait,
I: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn get_multiple<'life0, 'async_trait, C, DocumentIds, PrimaryKey, I>(
ids: DocumentIds,
connection: &'life0 C
) -> Pin<Box<dyn Future<Output = Result<Vec<CollectionDocument<Self>>, Error>> + Send + 'async_trait>> where
C: Connection,
DocumentIds: IntoIterator<Item = PrimaryKey, IntoIter = I> + Send + Sync,
I: Iterator<Item = PrimaryKey> + Send + Sync,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: Sized,
C: 'async_trait,
DocumentIds: 'async_trait,
PrimaryKey: 'async_trait,
I: 'async_trait,
'life0: '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, PrimaryKey, C>(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,
Cl::PrimaryKey: Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
R: Into<Range<PrimaryKey>>,
C: Connection,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: Sized,
fn list<R, PrimaryKey, C>(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,
Cl::PrimaryKey: Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
R: Into<Range<PrimaryKey>>,
C: Connection,
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: Sized,
Cl: SerializedCollection + Unpin,
Cl::PrimaryKey: 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,
Cl::PrimaryKey: 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,
Cl::PrimaryKey: Unpin,
Cn: Connection, type Output = Result<Vec<CollectionDocument<Cl>>, Error>; where
Self: Sized,
Cl: SerializedCollection + Unpin,
Cl::PrimaryKey: 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.
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).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.
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).await?;
println!(
"Inserted {:?} with id {} with revision {}",
document.contents, document.header.id, document.header.revision
);fn insert<'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>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Cn: Connection,
Self: Sized + 'static,
Self::Contents: 'async_trait,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn insert<'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>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Cn: Connection,
Self: Sized + 'static,
Self::Contents: 'async_trait,
PrimaryKey: '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, PrimaryKey, Cn>(
self,
id: PrimaryKey,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Cn: Connection,
Self: SerializedCollection<Contents = Self> + Sized + 'static,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn insert_into<'life0, 'async_trait, PrimaryKey, Cn>(
self,
id: PrimaryKey,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Cn: Connection,
Self: SerializedCollection<Contents = Self> + Sized + 'static,
PrimaryKey: 'async_trait,
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
);fn overwrite<'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>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send,
Cn: Connection,
Self: Sized + 'static,
Self::Contents: 'async_trait,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn overwrite<'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>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send,
Cn: Connection,
Self: Sized + 'static,
Self::Contents: 'async_trait,
PrimaryKey: 'async_trait,
Cn: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
Overwrites this value into the collection with the specified id, returning the created or updated document.
let document = MyCollection::overwrite(42, MyCollection::default(), &db).await?;
assert_eq!(document.header.id, 42);
println!(
"Overwrote {:?} with revision {}",
document.contents, document.header.revision
);fn overwrite_into<'life0, 'async_trait, Cn: Connection, PrimaryKey>(
self,
id: PrimaryKey,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
PrimaryKey: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
fn overwrite_into<'life0, 'async_trait, Cn: Connection, PrimaryKey>(
self,
id: PrimaryKey,
connection: &'life0 Cn
) -> Pin<Box<dyn Future<Output = Result<CollectionDocument<Self>, InsertError<Self>>> + Send + 'async_trait>> where
PrimaryKey: Into<AnyDocumentId<Self::PrimaryKey>> + Send + Sync,
Self: SerializedCollection<Contents = Self> + Sized + 'static,
Cn: 'async_trait,
PrimaryKey: 'async_trait,
'life0: 'async_trait,
Self: Send + 'async_trait,
Overwrites this value into the collection with the given id, returning
the created or updated document.
let document = MyCollection::default().overwrite_into(42, &db).await?;
assert_eq!(document.header.id, 42);
println!(
"Overwrote {:?} with revision {}",
document.contents, document.header.revision
);