1
mod collection;
2
mod names;
3
mod schematic;
4
mod summary;
5
/// Types for defining map/reduce-powered `View`s.
6
pub mod view;
7

            
8
pub use bonsaidb_macros::{Collection, Schema, View, ViewSchema};
9

            
10
pub use self::collection::{
11
    AsyncEntry, AsyncList, Collection, DefaultSerialization, Entry, EntryInsert, EntryUpdate,
12
    InsertError, List, Nameable, NamedCollection, NamedReference, SerializedCollection,
13
};
14
pub use self::names::{
15
    Authority, CollectionName, InvalidNameError, Name, Qualified, QualifiedName, SchemaName,
16
    ViewName,
17
};
18
pub use self::schematic::Schematic;
19
pub use self::summary::{CollectionSummary, SchemaSummary, ViewSummary};
20
pub use self::view::map::{Map, MappedValue, ViewMappedValue};
21
pub use self::view::{
22
    CollectionMapReduce, DefaultViewSerialization, MapReduce, ReduceResult, SerializedView, View,
23
    ViewMapResult, ViewSchema,
24
};
25
use 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
/// ```
72
pub 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
146026
    fn schematic() -> Result<Schematic, Error> {
81
146026
        Schematic::from_schema::<Self>()
82
146026
    }
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.
88
impl Schema for () {
89
8600
    fn schema_name() -> SchemaName {
90
8600
        SchemaName::new("", "")
91
8600
    }
92

            
93
4640
    fn define_collections(_schema: &mut Schematic) -> Result<(), Error> {
94
4640
        Ok(())
95
4640
    }
96
}
97

            
98
impl<T> Schema for T
99
where
100
    T: Collection + 'static,
101
{
102
317
    fn schema_name() -> SchemaName {
103
317
        SchemaName::from(Self::collection_name())
104
317
    }
105

            
106
202
    fn define_collections(schema: &mut Schematic) -> Result<(), Error> {
107
202
        schema.define_collection::<Self>()
108
202
    }
109
}