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;
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> Collection<'a, Cn, Cl>where
Cn: Connection,
Cl: Collection,
impl<'a, Cn, Cl> Collection<'a, Cn, Cl>where
Cn: Connection,
Cl: Collection,
Sourcepub fn push(
&self,
item: &<Cl as SerializedCollection>::Contents,
) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>where
Cl: SerializedCollection,
pub fn push(
&self,
item: &<Cl as SerializedCollection>::Contents,
) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>where
Cl: SerializedCollection,
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())?;
println!(
"Inserted id {} with revision {}",
inserted_header.id, inserted_header.revision
);Sourcepub fn push_bytes<B: Into<Bytes> + Send>(
&self,
contents: B,
) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>
pub fn push_bytes<B: Into<Bytes> + Send>( &self, contents: B, ) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>
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![])?;
println!(
"Inserted id {} with revision {}",
inserted_header.id, inserted_header.revision
);Sourcepub fn insert<PrimaryKey>(
&self,
id: &PrimaryKey,
item: &<Cl as SerializedCollection>::Contents,
) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>
pub fn insert<PrimaryKey>( &self, id: &PrimaryKey, item: &<Cl as SerializedCollection>::Contents, ) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>
Adds a new Document<Cl> with the given id and contents item.
let inserted_header = db
.collection::<MyCollection>()
.insert(&42, &MyCollection::default())?;
println!(
"Inserted id {} with revision {}",
inserted_header.id, inserted_header.revision
);Sourcepub fn insert_bytes<PrimaryKey, B: Into<Bytes> + Send>(
&self,
id: &PrimaryKey,
contents: B,
) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>
pub fn insert_bytes<PrimaryKey, B: Into<Bytes> + Send>( &self, id: &PrimaryKey, contents: B, ) -> Result<CollectionHeader<Cl::PrimaryKey>, Error>
Adds a new Document<Cl> with the the given id and contents.
let inserted_header = db.collection::<MyCollection>().insert_bytes(&42, vec![])?;
println!(
"Inserted id {} with revision {}",
inserted_header.id, inserted_header.revision
);Sourcepub fn update<D: Document<Cl> + Send + Sync>(
&self,
doc: &mut D,
) -> Result<(), Error>
pub fn update<D: Document<Cl> + Send + Sync>( &self, doc: &mut D, ) -> Result<(), Error>
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)? {
// modify the document
db.collection::<MyCollection>().update(&mut document);
println!("Updated revision: {:?}", document.header.revision);
}Sourcepub fn overwrite<D: Document<Cl> + Send + Sync>(
&self,
doc: &mut D,
) -> Result<(), Error>
pub fn overwrite<D: Document<Cl> + Send + Sync>( &self, doc: &mut D, ) -> Result<(), Error>
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)? {
// modify the document
db.collection::<MyCollection>().overwrite(&mut document);
println!("Updated revision: {:?}", document.header.revision);
}Sourcepub fn get<PrimaryKey>(
&self,
id: &PrimaryKey,
) -> Result<Option<OwnedDocument>, Error>
pub fn get<PrimaryKey>( &self, id: &PrimaryKey, ) -> Result<Option<OwnedDocument>, Error>
Retrieves a Document<Cl> with id from the connection.
if let Some(doc) = db.collection::<MyCollection>().get(&42)? {
println!(
"Retrieved bytes {:?} with revision {}",
doc.contents, doc.header.revision
);
let deserialized = MyCollection::document_contents(&doc)?;
println!("Deserialized contents: {:?}", deserialized);
}Sourcepub 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::PrimaryKey> + 'id + ?Sized,
pub 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::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])? {
println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
let deserialized = MyCollection::document_contents(&doc)?;
println!("Deserialized contents: {:?}", deserialized);
}Sourcepub fn list<PrimaryKey, R>(&'a self, ids: R) -> List<'a, Cn, Cl, PrimaryKey>where
R: Into<Range<&'a PrimaryKey>>,
PrimaryKey: KeyEncoding<Cl::PrimaryKey> + PartialEq + 'a + ?Sized,
Cl::PrimaryKey: Borrow<PrimaryKey> + PartialEq<PrimaryKey>,
pub fn list<PrimaryKey, R>(&'a self, ids: R) -> List<'a, Cn, Cl, PrimaryKey>where
R: Into<Range<&'a PrimaryKey>>,
PrimaryKey: KeyEncoding<Cl::PrimaryKey> + PartialEq + 'a + ?Sized,
Cl::PrimaryKey: Borrow<PrimaryKey> + PartialEq<PrimaryKey>,
Retrieves all documents matching the range of ids.
for doc in db
.collection::<MyCollection>()
.list(&42..)
.descending()
.limit(20)
.query()?
{
println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
let deserialized = MyCollection::document_contents(&doc)?;
println!("Deserialized contents: {:?}", deserialized);
}Sourcepub fn list_with_prefix<PrimaryKey>(
&'a self,
prefix: &'a PrimaryKey,
) -> List<'a, Cn, Cl, PrimaryKey>where
PrimaryKey: IntoPrefixRange<'a, Cl::PrimaryKey> + KeyEncoding<Cl::PrimaryKey> + PartialEq + ?Sized,
Cl::PrimaryKey: Borrow<PrimaryKey> + PartialEq<PrimaryKey>,
pub fn list_with_prefix<PrimaryKey>(
&'a self,
prefix: &'a PrimaryKey,
) -> List<'a, Cn, Cl, PrimaryKey>where
PrimaryKey: IntoPrefixRange<'a, Cl::PrimaryKey> + KeyEncoding<Cl::PrimaryKey> + PartialEq + ?Sized,
Cl::PrimaryKey: Borrow<PrimaryKey> + PartialEq<PrimaryKey>,
Retrieves all documents with ids that start with prefix.
use bonsaidb_core::connection::Connection;
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;
fn starts_with_a<C: Connection>(db: &C) -> Result<Vec<OwnedDocument>, Error> {
db.collection::<MyCollection>()
.list_with_prefix("a")
.query()
}Sourcepub fn all(&'a self) -> List<'a, Cn, Cl, Cl::PrimaryKey>
pub fn all(&'a self) -> List<'a, Cn, Cl, Cl::PrimaryKey>
Retrieves all documents.
for doc in db.collection::<MyCollection>().all().query()? {
println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
let deserialized = MyCollection::document_contents(&doc)?;
println!("Deserialized contents: {:?}", deserialized);
}Trait Implementations§
Auto Trait Implementations§
impl<'a, Cn, Cl> Freeze for Collection<'a, Cn, Cl>
impl<'a, Cn, Cl> RefUnwindSafe for Collection<'a, Cn, Cl>where
Cn: RefUnwindSafe,
Cl: RefUnwindSafe,
impl<'a, Cn, Cl> Send for Collection<'a, Cn, Cl>
impl<'a, Cn, Cl> Sync for Collection<'a, Cn, Cl>
impl<'a, Cn, Cl> Unpin for Collection<'a, Cn, Cl>where
Cl: Unpin,
impl<'a, Cn, Cl> UnwindSafe for Collection<'a, Cn, Cl>where
Cn: RefUnwindSafe,
Cl: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more