use crate::prelude::*;
use psl::{parser_database::walkers, schema_ast::ast};
pub type Model = crate::Zipper<ast::ModelId>;
impl Model {
pub fn name(&self) -> &str {
self.walker().name()
}
pub fn primary_identifier(&self) -> FieldSelection {
let fields: Vec<_> = self
.walker()
.required_unique_criterias()
.next()
.unwrap()
.fields()
.map(|f| {
self.dm
.clone()
.zip(ScalarFieldId::InModel(f.as_scalar_field().unwrap().id))
})
.collect();
FieldSelection::from(fields)
}
pub fn fields(&self) -> Fields<'_> {
Fields::new(self)
}
pub fn supports_create_operation(&self) -> bool {
let has_unsupported_field = self
.walker()
.scalar_fields()
.any(|sf| sf.ast_field().arity.is_required() && sf.is_unsupported() && sf.default_value().is_none());
!has_unsupported_field
}
pub fn db_name(&self) -> &str {
self.walker().database_name()
}
pub fn db_name_opt(&self) -> Option<&str> {
self.walker().mapped_name()
}
pub fn unique_indexes(&self) -> impl Iterator<Item = walkers::IndexWalker<'_>> {
self.walker()
.indexes()
.filter(|idx| idx.is_unique())
.filter(|index| !index.fields().any(|f| f.is_unsupported()))
}
}
impl std::fmt::Debug for Model {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Model").field(&self.name()).finish()
}
}