1use serde::Serialize;
6
7use crate::refs::PathSeg;
8use crate::typ::Typ;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
11pub enum CollectionKind {
12 Entity,
14 SecondaryIndex,
16 Denormalized,
18 Counters,
20 Standalone,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
25pub enum OnMissing {
26 Error,
28 Warn,
30 AllowMissing,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
35pub enum RelationKind {
36 Fk {
37 field_path: Vec<PathSeg>,
39 brand: String,
40 on_missing: OnMissing,
41 validated: bool,
44 },
45 Index,
47 Denorm,
49 Counter { field: String },
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
55pub enum ValueModel {
56 Zero,
58 Typed,
60 VarTyped,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
66pub enum CollectionShape {
67 Tree,
69 Map,
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
75pub enum StorageBackend {
76 Bitcask,
78 Fixed,
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
84pub struct StorageClass {
85 pub model: ValueModel,
86 pub shape: CollectionShape,
87 pub backend: StorageBackend,
88}
89
90#[derive(Debug, Clone, Serialize)]
91pub struct CollectionNode {
92 pub name: String,
93 pub kind: CollectionKind,
94 pub ty: Typ,
96 pub self_brand: Option<String>,
98 pub storage: Option<StorageClass>,
100}
101
102#[derive(Debug, Clone, Serialize)]
103pub struct RelationEdge {
104 pub from: String,
105 pub to: String,
106 pub kind: RelationKind,
107}
108
109#[derive(Debug, Clone, Serialize, Default)]
110pub struct SchemaGraph {
111 pub collections: Vec<CollectionNode>,
112 pub relations: Vec<RelationEdge>,
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118 use crate::scalar::ScalarTyp;
119
120 #[test]
121 fn graph_serializes_to_json() {
122 let g = SchemaGraph {
123 collections: vec![CollectionNode {
124 name: "users".into(),
125 kind: CollectionKind::Entity,
126 ty: Typ::Scalar(ScalarTyp::U64),
127 self_brand: Some("USR".into()),
128 storage: None,
129 }],
130 relations: vec![RelationEdge {
131 from: "messages".into(),
132 to: "users".into(),
133 kind: RelationKind::Fk {
134 field_path: vec![PathSeg::Field("sender_id")],
135 brand: "USR".into(),
136 on_missing: OnMissing::Error,
137 validated: true,
138 },
139 }],
140 };
141 let json = serde_json::to_string(&g).expect("serialize");
142 assert!(json.contains("\"users\""));
143 assert!(json.contains("\"sender_id\""));
144 }
145}