Lines
98.63 %
Functions
69.57 %
Branches
100 %
use bonsaidb::{
core::{
connection::Connection,
document::{BorrowedDocument, Emit},
schema::{
view::map::ViewMappedValue, Collection, ReduceResult, SerializedCollection, View,
ViewMapResult, ViewSchema,
},
Error,
local::{
config::{Builder, StorageConfiguration},
Database,
};
use serde::{Deserialize, Serialize};
// ANCHOR: struct
#[derive(Serialize, Deserialize, Debug, Collection)]
#[collection(name = "blog-post", views = [BlogPostsByCategory])]
pub struct BlogPost {
pub title: String,
pub body: String,
pub category: Option<String>,
}
// ANCHOR_END: struct
// ANCHOR: view
#[derive(Debug, Clone, View)]
#[view(collection = BlogPost, key = Option<String>, value = u32, name = "by-category")]
pub struct BlogPostsByCategory;
impl ViewSchema for BlogPostsByCategory {
type View = Self;
fn map(&self, document: &BorrowedDocument<'_>) -> ViewMapResult<Self::View> {
let post = BlogPost::document_contents(document)?;
document.header.emit_key_and_value(post.category, 1)
fn reduce(
&self,
mappings: &[ViewMappedValue<Self::View>],
_rereduce: bool,
) -> ReduceResult<Self::View> {
Ok(mappings.iter().map(|mapping| mapping.value).sum())
// ANCHOR_END: view
#[allow(unused_variables)]
#[tokio::test]
async fn example() -> Result<(), Error> {
drop(tokio::fs::remove_dir_all("example.bonsaidb").await);
let db = Database::open::<BlogPost>(StorageConfiguration::new("example.bonsaidb")).await?;
// ANCHOR: insert_data
BlogPost {
title: String::from("New version of BonsaiDb released"),
body: String::from("..."),
category: Some(String::from("Rust")),
.push_into(&db)
.await?;
title: String::from("New Rust version released"),
title: String::from("Check out this great cinnamon roll recipe"),
category: Some(String::from("Cooking")),
// ANCHOR_END: insert_data
// ANCHOR: query_with_docs
let rust_posts = db
.view::<BlogPostsByCategory>()
.with_key(Some(String::from("Rust")))
.query_with_docs()
for mapping in &rust_posts {
let post = BlogPost::document_contents(mapping.document)?;
println!(
"Retrieved post #{} \"{}\"",
mapping.document.header.id, post.title
);
// ANCHOR_END: query_with_docs
assert_eq!(rust_posts.len(), 2);
// ANCHOR: query_with_collection_docs
.query_with_collection_docs()
mapping.document.header.id, mapping.document.contents.title
// ANCHOR_END: query_with_collection_docs
// ANCHOR: reduce_one_key
let rust_post_count = db
.reduce()
assert_eq!(rust_post_count, 2);
// ANCHOR_END: reduce_one_key
// ANCHOR: reduce_multiple_keys
let total_post_count = db.view::<BlogPostsByCategory>().reduce().await?;
assert_eq!(total_post_count, 3);
// ANCHOR_END: reduce_multiple_keys
Ok(())