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
        Collection, DefaultSerialization, Entry, InsertError, List, Nameable, NamedCollection,
13
        NamedReference, SerializedCollection,
14
    },
15
    names::{Authority, CollectionName, InvalidNameError, Name, SchemaName, ViewName},
16
    schematic::Schematic,
17
    view::{
18
        map::{Map, MappedValue, ViewMappedValue},
19
        CollectionViewSchema, DefaultViewSerialization, ReduceResult, SerializedView, View,
20
        ViewMapResult, ViewSchema,
21
    },
22
};
23
use crate::Error;
24

            
25
/// Defines a group of collections that are stored into a single database.
26
///
27
/// ## Deriving this trait
28
///
29
/// This trait can be derived rather than manually implemented:
30
///
31
/// ```rust
32
/// use bonsaidb_core::schema::{Collection, Schema};
33
/// use serde::{Deserialize, Serialize};
34
///
35
/// #[derive(Debug, Schema)]
36
/// #[schema(name = "MySchema", collections = [MyCollection])]
37
/// # #[schema(core = bonsaidb_core)]
38
/// pub struct MySchema;
39
///
40
/// #[derive(Debug, Serialize, Deserialize, Default, Collection)]
41
/// #[collection(name = "MyCollection")]
42
/// # #[collection(core = bonsaidb_core)]
43
/// pub struct MyCollection {
44
///     pub rank: u32,
45
///     pub score: f32,
46
/// }
47
/// ```
48
///
49
/// If you're publishing a schema for use in multiple projects, consider giving
50
/// the schema an `authority`, which gives your schema a namespace:
51
///
52
/// ```rust
53
/// use bonsaidb_core::schema::Schema;
54
///
55
/// #[derive(Debug, Schema)]
56
/// #[schema(name = "MySchema", authority = "khonsulabs", collections = [MyCollection])]
57
/// # #[schema(core = bonsaidb_core)]
58
/// pub struct MySchema;
59
///
60
/// # use serde::{Deserialize, Serialize};
61
/// # use bonsaidb_core::schema::Collection;
62
/// # #[derive(Debug, Serialize, Deserialize, Default, Collection)]
63
/// # #[collection(name = "MyCollection")]
64
/// # #[collection(core = bonsaidb_core)]
65
/// # pub struct MyCollection {
66
/// #    pub rank: u32,
67
/// #    pub score: f32,
68
/// # }
69
/// ```
70
pub trait Schema: Send + Sync + Debug + 'static {
71
    /// Returns the unique [`SchemaName`] for this schema.
72
    fn schema_name() -> SchemaName;
73

            
74
    /// Defines the `Collection`s into `schema`.
75
    fn define_collections(schema: &mut Schematic) -> Result<(), Error>;
76

            
77
    /// Retrieves the [`Schematic`] for this schema.
78
92674
    fn schematic() -> Result<Schematic, Error> {
79
92674
        Schematic::from_schema::<Self>()
80
92674
    }
81
}
82

            
83
/// This implementation is for accessing databases when interacting with
84
/// collections isn't required. For example, accessing only the key-value store
85
/// or pubsub.
86
impl Schema for () {
87
3744
    fn schema_name() -> SchemaName {
88
3744
        SchemaName::new("", "")
89
3744
    }
90

            
91
2158
    fn define_collections(_schema: &mut Schematic) -> Result<(), Error> {
92
2158
        Ok(())
93
2158
    }
94
}
95

            
96
impl<T> Schema for T
97
where
98
    T: Collection + 'static,
99
{
100
266
    fn schema_name() -> SchemaName {
101
266
        let CollectionName { authority, name } = Self::collection_name();
102
266
        SchemaName { authority, name }
103
266
    }
104

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