1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use serde::{Deserialize, Serialize};

use crate::define_basic_unique_mapped_view;
use crate::document::{CollectionDocument, Emit};
use crate::schema::{Collection, NamedCollection, SchemaName};

/// A database stored in BonsaiDb.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, Collection)]
#[collection(authority = "bonsaidb", name = "databases", views = [ByName], core = crate)]
pub struct Database {
    /// The name of the database.
    pub name: String,
    /// The schema defining the database.
    pub schema: SchemaName,
}

define_basic_unique_mapped_view!(
    ByName,
    Database,
    1,
    "by-name",
    String,
    SchemaName,
    |document: CollectionDocument<Database>| {
        document
            .header
            .emit_key_and_value(document.contents.name, document.contents.schema)
    },
);

impl NamedCollection for Database {
    type ByNameView = ByName;
}