data-modelling-core 2.4.0

Core SDK library for model operations across platforms
Documentation
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Table model for the SDK

use super::column::Column;
use super::enums::{
    DataVaultClassification, DatabaseType, InfrastructureType, MedallionLayer, ModelingLevel,
    SCDPattern,
};
use super::tag::Tag;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json;
use std::collections::HashMap;
use std::str::FromStr;
use uuid::Uuid;

/// Deserialize tags with backward compatibility (supports Vec<String> and Vec<Tag>)
fn deserialize_tags<'de, D>(deserializer: D) -> Result<Vec<Tag>, D::Error>
where
    D: Deserializer<'de>,
{
    // Accept either Vec<String> (backward compatibility) or Vec<Tag>
    struct TagVisitor;

    impl<'de> serde::de::Visitor<'de> for TagVisitor {
        type Value = Vec<Tag>;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("a vector of tags (strings or Tag objects)")
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::SeqAccess<'de>,
        {
            let mut tags = Vec::new();
            while let Some(item) = seq.next_element::<serde_json::Value>()? {
                match item {
                    serde_json::Value::String(s) => {
                        // Backward compatibility: parse string as Tag
                        if let Ok(tag) = Tag::from_str(&s) {
                            tags.push(tag);
                        }
                    }
                    _ => {
                        // Try to deserialize as Tag directly (if it's a string in JSON)
                        if let serde_json::Value::String(s) = item
                            && let Ok(tag) = Tag::from_str(&s)
                        {
                            tags.push(tag);
                        }
                    }
                }
            }
            Ok(tags)
        }
    }

    deserializer.deserialize_seq(TagVisitor)
}

/// Position coordinates for table placement on canvas
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Position {
    /// X coordinate
    pub x: f64,
    /// Y coordinate
    pub y: f64,
}

/// SLA (Service Level Agreement) property following ODCS-inspired structure
///
/// Represents a single SLA property for Data Flow nodes and relationships.
/// Uses a lightweight format inspired by ODCS servicelevels but separate from ODCS.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SlaProperty {
    /// SLA attribute name (e.g., "latency", "availability", "throughput")
    pub property: String,
    /// Metric value (flexible type to support numbers, strings, etc.)
    pub value: serde_json::Value,
    /// Measurement unit (e.g., "hours", "percent", "requests_per_second")
    pub unit: String,
    /// Optional: Data elements this SLA applies to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub element: Option<String>,
    /// Optional: Importance driver (e.g., "regulatory", "analytics", "operational")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub driver: Option<String>,
    /// Optional: Description of the SLA
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional: Scheduler type for monitoring
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheduler: Option<String>,
    /// Optional: Schedule expression (e.g., cron format)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schedule: Option<String>,
}

/// Contact details for Data Flow node/relationship owners/responsible parties
///
/// Structured contact information for operational and governance purposes.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ContactDetails {
    /// Email address
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// Phone number
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone: Option<String>,
    /// Contact name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Role or title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    /// Other contact methods or additional information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub other: Option<String>,
}

/// Table model representing a database table or data contract
///
/// A table represents a structured data entity with columns, metadata, and relationships.
/// Tables can be imported from various formats (SQL, ODCS, JSON Schema, etc.) and exported
/// to multiple formats.
///
/// # Example
///
/// ```rust
/// use data_modelling_core::models::{Table, Column};
///
/// let table = Table::new(
///     "users".to_string(),
///     vec![
///         Column::new("id".to_string(), "INT".to_string()),
///         Column::new("name".to_string(), "VARCHAR(100)".to_string()),
///     ],
/// );
/// ```
///
/// # Example with Metadata (Data Flow Node)
///
/// ```rust
/// use data_modelling_core::models::{Table, Column, InfrastructureType, ContactDetails, SlaProperty};
/// use serde_json::json;
///
/// let mut table = Table::new(
///     "user_events".to_string(),
///     vec![Column::new("id".to_string(), "UUID".to_string())],
/// );
/// table.owner = Some("Data Engineering Team".to_string());
/// table.infrastructure_type = Some(InfrastructureType::Kafka);
/// table.contact_details = Some(ContactDetails {
///     email: Some("team@example.com".to_string()),
///     phone: None,
///     name: Some("Data Team".to_string()),
///     role: Some("Data Owner".to_string()),
///     other: None,
/// });
/// table.sla = Some(vec![SlaProperty {
///     property: "latency".to_string(),
///     value: json!(4),
///     unit: "hours".to_string(),
///     description: Some("Data must be available within 4 hours".to_string()),
///     element: None,
///     driver: Some("operational".to_string()),
///     scheduler: None,
///     schedule: None,
/// }]);
/// table.notes = Some("User interaction events from web application".to_string());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Table {
    /// Unique identifier for the table (UUIDv4)
    pub id: Uuid,
    /// Table name (must be unique within database_type/catalog/schema scope)
    pub name: String,
    /// List of columns in the table
    pub columns: Vec<Column>,
    /// Database type (PostgreSQL, MySQL, etc.) if applicable
    #[serde(skip_serializing_if = "Option::is_none", alias = "database_type")]
    pub database_type: Option<DatabaseType>,
    /// Catalog name (database name in some systems)
    #[serde(skip_serializing_if = "Option::is_none", alias = "catalog_name")]
    pub catalog_name: Option<String>,
    /// Schema name (namespace within catalog)
    #[serde(skip_serializing_if = "Option::is_none", alias = "schema_name")]
    pub schema_name: Option<String>,
    /// Medallion architecture layers (Bronze, Silver, Gold)
    #[serde(default, alias = "medallion_layers")]
    pub medallion_layers: Vec<MedallionLayer>,
    /// Slowly Changing Dimension pattern (Type 1, Type 2, etc.)
    #[serde(skip_serializing_if = "Option::is_none", alias = "scd_pattern")]
    pub scd_pattern: Option<SCDPattern>,
    /// Data Vault classification (Hub, Link, Satellite)
    #[serde(
        skip_serializing_if = "Option::is_none",
        alias = "data_vault_classification"
    )]
    pub data_vault_classification: Option<DataVaultClassification>,
    /// Modeling level (Conceptual, Logical, Physical)
    #[serde(skip_serializing_if = "Option::is_none", alias = "modeling_level")]
    pub modeling_level: Option<ModelingLevel>,
    /// Tags for categorization and filtering (supports Simple, Pair, and List formats)
    #[serde(default, deserialize_with = "deserialize_tags")]
    pub tags: Vec<Tag>,
    /// ODCL/ODCS metadata (legacy format support)
    #[serde(default, alias = "odcl_metadata")]
    pub odcl_metadata: HashMap<String, serde_json::Value>,
    /// Owner information (person, team, or organization name) for Data Flow nodes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner: Option<String>,
    /// SLA (Service Level Agreement) information (ODCS-inspired but lightweight format)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sla: Option<Vec<SlaProperty>>,
    /// Contact details for responsible parties
    #[serde(skip_serializing_if = "Option::is_none", alias = "contact_details")]
    pub contact_details: Option<ContactDetails>,
    /// Infrastructure type (hosting platform, service, or tool) for Data Flow nodes
    #[serde(skip_serializing_if = "Option::is_none", alias = "infrastructure_type")]
    pub infrastructure_type: Option<InfrastructureType>,
    /// Additional notes and context for Data Flow nodes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
    /// Canvas position for visual representation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position: Option<Position>,
    /// Path to YAML file if loaded from file system
    #[serde(skip_serializing_if = "Option::is_none", alias = "yaml_file_path")]
    pub yaml_file_path: Option<String>,
    /// Draw.io cell ID for diagram integration
    #[serde(skip_serializing_if = "Option::is_none", alias = "drawio_cell_id")]
    pub drawio_cell_id: Option<String>,
    /// Quality rules and checks
    #[serde(default)]
    pub quality: Vec<HashMap<String, serde_json::Value>>,
    /// Validation errors and warnings
    #[serde(default)]
    pub errors: Vec<HashMap<String, serde_json::Value>>,
    /// Creation timestamp
    #[serde(alias = "created_at")]
    pub created_at: DateTime<Utc>,
    /// Last update timestamp
    #[serde(alias = "updated_at")]
    pub updated_at: DateTime<Utc>,
}

impl Table {
    /// Create a new table with the given name and columns
    ///
    /// # Arguments
    ///
    /// * `name` - The table name (must be valid according to naming conventions)
    /// * `columns` - Vector of columns for the table
    ///
    /// # Returns
    ///
    /// A new `Table` instance with a generated UUIDv4 ID and current timestamps.
    ///
    /// # Example
    ///
    /// ```rust
    /// use data_modelling_core::models::{Table, Column};
    ///
    /// let table = Table::new(
    ///     "users".to_string(),
    ///     vec![Column::new("id".to_string(), "INT".to_string())],
    /// );
    /// ```
    pub fn new(name: String, columns: Vec<Column>) -> Self {
        let now = Utc::now();
        // UUIDv4 everywhere (do not derive ids from natural keys like name).
        let id = Self::generate_id(&name, None, None, None);
        Self {
            id,
            name,
            columns,
            database_type: None,
            catalog_name: None,
            schema_name: None,
            medallion_layers: Vec::new(),
            scd_pattern: None,
            data_vault_classification: None,
            modeling_level: None,
            tags: Vec::new(),
            odcl_metadata: HashMap::new(),
            owner: None,
            sla: None,
            contact_details: None,
            infrastructure_type: None,
            notes: None,
            position: None,
            yaml_file_path: None,
            drawio_cell_id: None,
            quality: Vec::new(),
            errors: Vec::new(),
            created_at: now,
            updated_at: now,
        }
    }

    /// Get the unique key tuple for this table
    ///
    /// Returns a tuple of (database_type, name, catalog_name, schema_name) that uniquely
    /// identifies this table within its scope. Used for detecting naming conflicts.
    ///
    /// # Returns
    ///
    /// A tuple containing the database type (as string), name, catalog name, and schema name.
    pub fn get_unique_key(&self) -> (Option<String>, String, Option<String>, Option<String>) {
        (
            self.database_type.as_ref().map(|dt| format!("{:?}", dt)),
            self.name.clone(),
            self.catalog_name.clone(),
            self.schema_name.clone(),
        )
    }

    /// Generate a UUIDv4 for a new table id.
    ///
    /// Note: params are retained for backward-compatibility with previous deterministic-v5 API.
    pub fn generate_id(
        _name: &str,
        _database_type: Option<&DatabaseType>,
        _catalog_name: Option<&str>,
        _schema_name: Option<&str>,
    ) -> Uuid {
        Uuid::new_v4()
    }

    /// Create a Table from imported TableData.
    ///
    /// Converts the import format (TableData) to the internal Table model.
    /// This is used when exporting ODCS YAML directly to PDF/Markdown.
    ///
    /// # Arguments
    ///
    /// * `table_data` - The imported table data from ODCS parser
    ///
    /// # Returns
    ///
    /// A new Table instance populated from the import data
    pub fn from_table_data(table_data: &crate::import::TableData) -> Self {
        use crate::models::Column;

        let table_name = table_data
            .name
            .clone()
            .unwrap_or_else(|| format!("table_{}", table_data.table_index));

        let columns: Vec<Column> = table_data
            .columns
            .iter()
            .map(|col_data| Column {
                id: col_data.id.clone(),
                name: col_data.name.clone(),
                business_name: col_data.business_name.clone(),
                description: col_data.description.clone().unwrap_or_default(),
                data_type: col_data.data_type.clone(),
                physical_type: col_data.physical_type.clone(),
                physical_name: col_data.physical_name.clone(),
                logical_type_options: col_data.logical_type_options.clone(),
                primary_key: col_data.primary_key,
                primary_key_position: col_data.primary_key_position,
                unique: col_data.unique,
                nullable: col_data.nullable,
                partitioned: col_data.partitioned,
                partition_key_position: col_data.partition_key_position,
                clustered: col_data.clustered,
                classification: col_data.classification.clone(),
                critical_data_element: col_data.critical_data_element,
                encrypted_name: col_data.encrypted_name.clone(),
                transform_source_objects: col_data.transform_source_objects.clone(),
                transform_logic: col_data.transform_logic.clone(),
                transform_description: col_data.transform_description.clone(),
                examples: col_data.examples.clone(),
                default_value: col_data.default_value.clone(),
                relationships: col_data.relationships.clone(),
                authoritative_definitions: col_data.authoritative_definitions.clone(),
                quality: col_data.quality.clone().unwrap_or_default(),
                enum_values: col_data.enum_values.clone().unwrap_or_default(),
                tags: col_data.tags.clone(),
                custom_properties: col_data.custom_properties.clone(),
                ..Default::default()
            })
            .collect();

        let mut table = Self::new(table_name, columns);

        // Preserve ODCS metadata
        if let Some(ref domain) = table_data.domain {
            table
                .odcl_metadata
                .insert("domain".to_string(), serde_json::json!(domain));
        }
        if let Some(ref version) = table_data.version {
            table
                .odcl_metadata
                .insert("version".to_string(), serde_json::json!(version));
        }
        if let Some(ref status) = table_data.status {
            table
                .odcl_metadata
                .insert("status".to_string(), serde_json::json!(status));
        }
        if let Some(ref description) = table_data.description {
            table
                .odcl_metadata
                .insert("description".to_string(), serde_json::json!(description));
        }
        if let Some(ref team) = table_data.team {
            table.odcl_metadata.insert(
                "team".to_string(),
                serde_json::to_value(team).unwrap_or_default(),
            );
        }
        if let Some(ref support) = table_data.support {
            table.odcl_metadata.insert(
                "support".to_string(),
                serde_json::to_value(support).unwrap_or_default(),
            );
        }

        table
    }
}