bonsaidb_core/schema/
summary.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::key::KeyDescription;
6use crate::schema::view::ViewUpdatePolicy;
7use crate::schema::{CollectionName, SchemaName, Schematic, ViewName};
8
9/// A summary of a [`Schema`](crate::schema::Schema)/[`Schematic`].
10///
11/// This type is a serializable summary of a [`Schematic`] and is the result of
12/// [`StorageConnection::list_available_schemas`](crate::connection::StorageConnection::list_available_schemas)/[`AsyncStorageConnection::list_available_schemas`](crate::connection::AsyncStorageConnection::list_available_schemas).
13/// It can be used to query information stored in BonsaiDb without needing
14/// access to the Rust types.
15#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
16pub struct SchemaSummary {
17    /// The name of the [`Schema`](crate::schema::Schema) this summary is of.
18    pub name: SchemaName,
19    collections: HashMap<CollectionName, CollectionSummary>,
20}
21
22impl SchemaSummary {
23    /// Returns the summary of named collection, if the schema contains it.
24    #[must_use]
25    pub fn collection(&self, name: &CollectionName) -> Option<&CollectionSummary> {
26        self.collections.get(name)
27    }
28
29    /// Returns an iterator over all collections contained in this schema.
30    pub fn collections(&self) -> impl Iterator<Item = &CollectionSummary> {
31        self.collections.values()
32    }
33}
34
35impl<'a> From<&'a Schematic> for SchemaSummary {
36    fn from(schematic: &'a Schematic) -> Self {
37        let mut summary = Self {
38            name: schematic.name.clone(),
39            collections: HashMap::new(),
40        };
41
42        for collection_name in schematic.collections() {
43            let collection = summary
44                .collections
45                .entry(collection_name.clone())
46                .or_insert_with(|| CollectionSummary {
47                    name: collection_name.clone(),
48                    primary_key: schematic
49                        .collection_primary_key_description(collection_name)
50                        .expect("invalid schematic")
51                        .clone(),
52                    views: HashMap::new(),
53                });
54            for view in schematic.views_in_collection(collection_name) {
55                let name = view.view_name();
56                collection.views.insert(
57                    name.clone(),
58                    ViewSummary {
59                        name,
60                        key: view.key_description(),
61                        policy: view.update_policy(),
62                        version: view.version(),
63                    },
64                );
65            }
66        }
67
68        summary
69    }
70}
71
72/// A summary of a [`Collection`](crate::schema::Collection).
73#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
74pub struct CollectionSummary {
75    /// The name of the [`Collection`](crate::schema::Collection) this is a summary of.
76    pub name: CollectionName,
77    /// The description of [`Collection::PrimaryKey`](crate::schema::Collection::PrimaryKey).
78    pub primary_key: KeyDescription,
79    views: HashMap<ViewName, ViewSummary>,
80}
81
82impl CollectionSummary {
83    /// Returns the summary of the named view, if it is contained in this collection.
84    #[must_use]
85    pub fn view(&self, name: &ViewName) -> Option<&ViewSummary> {
86        self.views.get(name)
87    }
88
89    /// Returns an iterator over all summaries of views in this collection.
90    pub fn views(&self) -> impl Iterator<Item = &ViewSummary> {
91        self.views.values()
92    }
93}
94
95/// A summary of a [`ViewSchema`](crate::schema::ViewSchema).
96#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
97pub struct ViewSummary {
98    /// The name of the [`ViewSchema`](crate::schema::ViewSchema) this is a
99    /// summary of.
100    pub name: ViewName,
101    /// The description of [`View::Key`](crate::schema::View::Key).
102    pub key: KeyDescription,
103    /// The result of
104    /// [`ViewSchema::update_policy()`](crate::schema::ViewSchema::update_policy)
105    /// for this view.
106    pub policy: ViewUpdatePolicy,
107    /// The result of
108    /// [`ViewSchema::version()`](crate::schema::ViewSchema::version) for this
109    /// view.
110    pub version: u64,
111}