Struct bonsaidb_core::connection::Collection
source · [−]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
sourceimpl<'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 async fn push(
&self,
item: &<Cl as SerializedCollection>::Contents
) -> Result<Header, Error> where
Cl: SerializedCollection,
pub async fn push(
&self,
item: &<Cl as SerializedCollection>::Contents
) -> Result<Header, Error> where
Cl: SerializedCollection,
Adds a new Document<Cl> with the contents item.
let inserted_header = db
.collection::<MyCollection>()
.push(&MyCollection::default())
.await?;
println!(
"Inserted id {} with revision {}",
inserted_header.id, inserted_header.revision
);sourcepub async fn push_bytes<B: Into<Bytes> + Send>(
&self,
contents: B
) -> Result<Header, Error> where
Cl: SerializedCollection,
pub async fn push_bytes<B: Into<Bytes> + Send>(
&self,
contents: B
) -> Result<Header, Error> where
Cl: SerializedCollection,
Adds a new Document<Cl> with the contents.
let inserted_header = db.collection::<MyCollection>().push_bytes(vec![]).await?;
println!(
"Inserted id {} with revision {}",
inserted_header.id, inserted_header.revision
);sourcepub async fn insert(
&self,
id: u64,
item: &<Cl as SerializedCollection>::Contents
) -> Result<Header, Error> where
Cl: SerializedCollection,
pub async fn insert(
&self,
id: u64,
item: &<Cl as SerializedCollection>::Contents
) -> Result<Header, Error> where
Cl: SerializedCollection,
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
);sourcepub async fn insert_bytes<B: Into<Bytes> + Send>(
&self,
id: u64,
contents: B
) -> Result<Header, Error> where
Cl: SerializedCollection,
pub async fn insert_bytes<B: Into<Bytes> + Send>(
&self,
id: u64,
contents: B
) -> Result<Header, Error> where
Cl: SerializedCollection,
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
);sourcepub async fn update<'d, D: Document<'d> + Send + Sync>(
&self,
doc: &mut D
) -> Result<(), Error>
pub async fn update<'d, D: Document<'d> + 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).await? {
// modify the document
db.collection::<MyCollection>().update(&mut document);
println!("Updated revision: {:?}", document.header.revision);
}sourcepub async fn get(&self, id: u64) -> Result<Option<OwnedDocument>, Error>
pub async fn get(&self, id: u64) -> Result<Option<OwnedDocument>, Error>
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 = doc.contents::<MyCollection>()?;
println!("Deserialized contents: {:?}", deserialized);
}sourcepub async fn get_multiple(
&self,
ids: &[u64]
) -> Result<Vec<OwnedDocument>, Error>
pub async fn get_multiple(
&self,
ids: &[u64]
) -> Result<Vec<OwnedDocument>, Error>
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 = doc.contents::<MyCollection>()?;
println!("Deserialized contents: {:?}", deserialized);
}sourcepub fn list<R: Into<Range<u64>>>(&'a self, ids: R) -> List<'a, Cn, Cl>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cn: Connection,
Cl: Collection + Unpin, type Output = Result<Vec<OwnedDocument>, Error>;
pub fn list<R: Into<Range<u64>>>(&'a self, ids: R) -> List<'a, Cn, Cl>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cn: Connection,
Cl: Collection + Unpin, type Output = Result<Vec<OwnedDocument>, Error>;
Cn: Connection,
Cl: Collection + Unpin, type Output = Result<Vec<OwnedDocument>, Error>;
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 = doc.contents::<MyCollection>()?;
println!("Deserialized contents: {:?}", deserialized);
}sourcepub fn all(&'a self) -> List<'a, Cn, Cl>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cn: Connection,
Cl: Collection + Unpin, type Output = Result<Vec<OwnedDocument>, Error>;
pub fn all(&'a self) -> List<'a, Cn, Cl>ⓘNotable traits for List<'a, Cn, Cl>impl<'a, Cn, Cl> Future for List<'a, Cn, Cl> where
Cn: Connection,
Cl: Collection + Unpin, type Output = Result<Vec<OwnedDocument>, Error>;
Cn: Connection,
Cl: Collection + Unpin, type Output = Result<Vec<OwnedDocument>, Error>;
Retrieves all documents.
for doc in db.collection::<MyCollection>().all().await? {
println!("Retrieved #{} with bytes {:?}", doc.header.id, doc.contents);
let deserialized = doc.contents::<MyCollection>()?;
println!("Deserialized contents: {:?}", deserialized);
}Trait Implementations
sourceimpl<'a, Cn, Cl> Clone for Collection<'a, Cn, Cl>
impl<'a, Cn, Cl> Clone for Collection<'a, Cn, Cl>
Auto Trait Implementations
impl<'a, Cn, Cl> RefUnwindSafe for Collection<'a, Cn, Cl> where
Cl: RefUnwindSafe,
Cn: RefUnwindSafe,
impl<'a, Cn, Cl> Send for Collection<'a, Cn, Cl> where
Cl: Send,
Cn: Sync,
impl<'a, Cn, Cl> Sync for Collection<'a, Cn, Cl> where
Cl: Sync,
Cn: Sync,
impl<'a, Cn, Cl> Unpin for Collection<'a, Cn, Cl> where
Cl: Unpin,
impl<'a, Cn, Cl> UnwindSafe for Collection<'a, Cn, Cl> where
Cl: UnwindSafe,
Cn: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more