use serde::Serialize;
use crate::refs::PathSeg;
use crate::typ::Typ;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum CollectionKind {
Entity,
SecondaryIndex,
Denormalized,
Counters,
Standalone,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum OnMissing {
Error,
Warn,
AllowMissing,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum RelationKind {
Fk {
field_path: Vec<PathSeg>,
brand: String,
on_missing: OnMissing,
validated: bool,
},
Index,
Denorm,
Counter { field: String },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum ValueModel {
Zero,
Typed,
VarTyped,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum CollectionShape {
Tree,
Map,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum StorageBackend {
Bitcask,
Fixed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct StorageClass {
pub model: ValueModel,
pub shape: CollectionShape,
pub backend: StorageBackend,
}
#[derive(Debug, Clone, Serialize)]
pub struct CollectionNode {
pub name: String,
pub kind: CollectionKind,
pub ty: Typ,
pub self_brand: Option<String>,
pub storage: Option<StorageClass>,
}
#[derive(Debug, Clone, Serialize)]
pub struct RelationEdge {
pub from: String,
pub to: String,
pub kind: RelationKind,
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct SchemaGraph {
pub collections: Vec<CollectionNode>,
pub relations: Vec<RelationEdge>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::scalar::ScalarTyp;
#[test]
fn graph_serializes_to_json() {
let g = SchemaGraph {
collections: vec![CollectionNode {
name: "users".into(),
kind: CollectionKind::Entity,
ty: Typ::Scalar(ScalarTyp::U64),
self_brand: Some("USR".into()),
storage: None,
}],
relations: vec![RelationEdge {
from: "messages".into(),
to: "users".into(),
kind: RelationKind::Fk {
field_path: vec![PathSeg::Field("sender_id")],
brand: "USR".into(),
on_missing: OnMissing::Error,
validated: true,
},
}],
};
let json = serde_json::to_string(&g).expect("serialize");
assert!(json.contains("\"users\""));
assert!(json.contains("\"sender_id\""));
}
}