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

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

            
10
pub use self::{
11
    collection::{
12
        AsyncEntry, AsyncList, Collection, DefaultSerialization, InsertError, List, Nameable,
13
        NamedCollection, NamedReference, SerializedCollection,
14
    },
15
    names::{
16
        ApiName, Authority, CollectionName, InvalidNameError, Name, Qualified, SchemaName, ViewName,
17
    },
18
    schematic::Schematic,
19
    view::{
20
        map::{Map, MappedValue, ViewMappedValue},
21
        CollectionViewSchema, DefaultViewSerialization, ReduceResult, SerializedView, View,
22
        ViewMapResult, ViewSchema,
23
    },
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(Debug, Schema)]
38
/// #[schema(name = "MySchema", collections = [MyCollection])]
39
/// # #[schema(core = bonsaidb_core)]
40
/// pub struct MySchema;
41
///
42
/// #[derive(Debug, 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(Debug, 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(Debug, 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 + Debug + '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
126499
    fn schematic() -> Result<Schematic, Error> {
81
126499
        Schematic::from_schema::<Self>()
82
126499
    }
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
6355
    fn schema_name() -> SchemaName {
90
6355
        SchemaName::new("", "")
91
6355
    }
92

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

            
98
impl<T> Schema for T
99
where
100
    T: Collection + 'static,
101
{
102
301
    fn schema_name() -> SchemaName {
103
301
        let CollectionName(qualified) = Self::collection_name();
104
301
        SchemaName(qualified)
105
301
    }
106

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