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