1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
mod collection;
mod names;
mod schematic;
mod summary;
/// Types for defining map/reduce-powered `View`s.
pub mod view;
pub use bonsaidb_macros::{Collection, Schema, View, ViewSchema};
pub use self::collection::{
AsyncEntry, AsyncList, Collection, DefaultSerialization, InsertError, List, Nameable,
NamedCollection, NamedReference, SerializedCollection,
};
pub use self::names::{
Authority, CollectionName, InvalidNameError, Name, Qualified, QualifiedName, SchemaName,
ViewName,
};
pub use self::schematic::Schematic;
pub use self::summary::{CollectionSummary, SchemaSummary, ViewSummary};
pub use self::view::map::{Map, MappedValue, ViewMappedValue};
pub use self::view::{
CollectionMapReduce, DefaultViewSerialization, MapReduce, ReduceResult, SerializedView, View,
ViewMapResult, ViewSchema,
};
use crate::Error;
/// Defines a group of collections that are stored into a single database.
///
/// ## Deriving this trait
///
/// This trait can be derived rather than manually implemented:
///
/// ```rust
/// use bonsaidb_core::schema::{Collection, Schema};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Schema)]
/// #[schema(name = "MySchema", collections = [MyCollection])]
/// # #[schema(core = bonsaidb_core)]
/// pub struct MySchema;
///
/// #[derive(Serialize, Deserialize, Default, Collection)]
/// #[collection(name = "MyCollection")]
/// # #[collection(core = bonsaidb_core)]
/// pub struct MyCollection {
/// pub rank: u32,
/// pub score: f32,
/// }
/// ```
///
/// If you're publishing a schema for use in multiple projects, consider giving
/// the schema an `authority`, which gives your schema a namespace:
///
/// ```rust
/// use bonsaidb_core::schema::Schema;
///
/// #[derive(Schema)]
/// #[schema(name = "MySchema", authority = "khonsulabs", collections = [MyCollection])]
/// # #[schema(core = bonsaidb_core)]
/// pub struct MySchema;
///
/// # use serde::{Deserialize, Serialize};
/// # use bonsaidb_core::schema::Collection;
/// # #[derive(Serialize, Deserialize, Default, Collection)]
/// # #[collection(name = "MyCollection")]
/// # #[collection(core = bonsaidb_core)]
/// # pub struct MyCollection {
/// # pub rank: u32,
/// # pub score: f32,
/// # }
/// ```
pub trait Schema: Send + Sync + 'static {
/// Returns the unique [`SchemaName`] for this schema.
fn schema_name() -> SchemaName;
/// Defines the `Collection`s into `schema`.
fn define_collections(schema: &mut Schematic) -> Result<(), Error>;
/// Retrieves the [`Schematic`] for this schema.
fn schematic() -> Result<Schematic, Error> {
Schematic::from_schema::<Self>()
}
}
/// This implementation is for accessing databases when interacting with
/// collections isn't required. For example, accessing only the key-value store
/// or pubsub.
impl Schema for () {
fn schema_name() -> SchemaName {
SchemaName::new("", "")
}
fn define_collections(_schema: &mut Schematic) -> Result<(), Error> {
Ok(())
}
}
impl<T> Schema for T
where
T: Collection + 'static,
{
fn schema_name() -> SchemaName {
SchemaName::from(Self::collection_name())
}
fn define_collections(schema: &mut Schematic) -> Result<(), Error> {
schema.define_collection::<Self>()
}
}