data-modelling-sdk 2.4.0

Shared SDK for model operations across platforms (API, WASM, Native)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
# Architecture Guide

## Table of Contents

1. [What is the Data Modelling SDK?]#what-is-the-data-modelling-sdk
2. [Project Decisions]#project-decisions
3. [When to Use the SDK]#when-to-use-the-sdk
4. [Architecture Overview]#architecture-overview
5. [Design Principles]#design-principles
6. [Component Architecture]#component-architecture
7. [Storage Architecture]#storage-architecture
8. [Database Architecture]#database-architecture
9. [Data Pipeline Architecture]#data-pipeline-architecture
10. [File Organization]#file-organization
11. [Integration Patterns]#integration-patterns
12. [Use Cases]#use-cases

---

## What is the Data Modelling SDK?

The **Data Modelling SDK** is a Rust library that provides unified interfaces for data modeling operations across multiple platforms. It serves as the foundation for data governance, schema management, and data contract operations in modern data platforms.

### Core Purpose

The SDK enables:

- **Multi-format Support**: Import from and export to various data contract formats (ODCS, ODCL, SQL, JSON Schema, AVRO, Protobuf, CADS, ODPS, BPMN, DMN, OpenAPI)
- **Cross-platform Compatibility**: Works seamlessly in native applications, web applications (WASM), and API backends
- **Domain Organization**: Organize data contracts, compute assets, and data products within business domains
- **Validation & Governance**: Validate schemas, detect conflicts, and enforce data governance rules
- **Storage Abstraction**: Abstract storage operations across different environments (file system, browser storage, HTTP API)

### Key Characteristics

- **Language**: Rust (Edition 2024)
- **License**: MIT
- **Platform Support**: Native (Rust), Web (WASM), API (HTTP)
- **Primary Format**: ODCS v3.1.0 (Open Data Contract Standard)
- **Architecture**: Modular, trait-based, feature-gated

---

## Project Decisions

### 1. Rust as the Foundation

**Decision**: Build the SDK in Rust

**Rationale**:
- **Performance**: Rust provides near-native performance with memory safety
- **Cross-platform**: Single codebase compiles to native binaries and WASM
- **Type Safety**: Strong type system prevents common errors
- **Ecosystem**: Excellent serialization, async, and web support
- **WASM Support**: First-class WASM compilation enables web deployment

**Trade-offs**:
- Learning curve for teams unfamiliar with Rust
- Longer compile times compared to interpreted languages
- Mitigated by excellent tooling and documentation

### 2. Storage Backend Abstraction

**Decision**: Abstract storage operations behind a trait (`StorageBackend`)

**Rationale**:
- **Platform Independence**: Same code works on file system, browser storage, and HTTP API
- **Testability**: Easy to mock storage for testing
- **Flexibility**: Applications can choose storage backend based on environment
- **Future-proof**: Easy to add new storage backends (S3, Azure Blob, etc.)

**Implementation**:
```rust
#[async_trait(?Send)]
pub trait StorageBackend: Send + Sync {
    async fn read_file(&self, path: &str) -> Result<Vec<u8>, StorageError>;
    async fn write_file(&self, path: &str, content: &[u8]) -> Result<(), StorageError>;
    // ... more operations
}
```

### 3. ODCS as Primary Format

**Decision**: Use ODCS v3.1.0 as the primary internal format

**Rationale**:
- **Comprehensive**: ODCS provides the most complete metadata model
- **Standard**: Industry-standard format with broad adoption
- **Extensible**: Supports custom properties and extensions
- **Field Preservation**: Maintains all metadata during conversions

**Trade-offs**:
- More verbose than simpler formats
- Requires conversion layer for other formats
- Mitigated by universal converter and format-specific exporters

### 4. Domain-Based File Organization

**Decision**: Organize files by business domain

**Rationale**:
- **Logical Grouping**: Related assets (tables, products, compute) grouped together
- **Scalability**: Easy to manage large numbers of assets
- **Ownership**: Clear ownership boundaries per domain
- **Version Control**: Better Git history and collaboration

**Structure** (Flat File Naming Convention):
```
workspace/
├── workspace.yaml                          # Workspace metadata with assets and relationships
├── myworkspace_domain1_table1.odcs.yaml    # Data contracts
├── myworkspace_domain1_product1.odps.yaml  # Data products
└── myworkspace_domain1_model1.cads.yaml    # Compute assets
```

Files follow the pattern: `{workspace}_{domain}_{system}_{resource}.{type}.yaml`

### 5. Feature-Gated Functionality

**Decision**: Gate optional functionality behind Cargo features

**Rationale**:
- **Minimal Dependencies**: Applications only include what they need
- **WASM Compatibility**: Some features (file system, Git) don't work in WASM
- **Build Performance**: Faster builds with fewer dependencies
- **Binary Size**: Smaller binaries for web deployment

**Features**:
- `default`: API backend (HTTP)
- `native-fs`: File system operations
- `wasm`: Browser storage (IndexedDB/localStorage)
- `git`: Git operations
- `png-export`: PNG diagram generation
- `databricks-dialect`: Databricks SQL support
- `database`: Database backend support (DuckDB/PostgreSQL)
- `duckdb-backend`: DuckDB embedded database
- `postgres-backend`: PostgreSQL database
- `staging`: Data staging with progress reporting
- `s3`: AWS S3 ingestion support
- `databricks`: Databricks Unity Catalog Volumes ingestion
- `iceberg`: Apache Iceberg lakehouse storage
- `cli-full`: Full CLI with all features including database support

### 6. UUID Strategy

**Decision**: Use UUIDv5 (deterministic) for model/table IDs

**Rationale**:
- **Deterministic**: Same inputs produce same ID (important for WASM without RNG)
- **Collision-resistant**: Very low probability of collisions
- **Reproducible**: Same model always gets same ID
- **No Random Number Generation**: Works in constrained environments

**Trade-offs**:
- Less privacy-friendly than random UUIDs
- Requires namespace and name for generation
- Acceptable for internal model IDs

### 7. Async/Await Architecture

**Decision**: Use async traits for storage operations

**Rationale**:
- **Non-blocking**: Better performance for I/O operations
- **WASM Compatibility**: Browser APIs are async
- **Consistency**: Same API across all platforms
- **Future-proof**: Aligns with Rust async ecosystem

**Trade-offs**:
- More complex than synchronous APIs
- Requires async runtime (Tokio for native, WASM runtime for web)
- Mitigated by excellent async/await syntax

### 8. Enhanced Tag Support

**Decision**: Support three tag formats (Simple, Pair, List)

**Rationale**:
- **Flexibility**: Supports various tagging strategies
- **Backward Compatible**: Simple tags work with existing systems
- **Rich Metadata**: Pair and List tags enable structured metadata
- **Auto-detection**: Automatically detects tag format during parsing

**Formats**:
- **Simple**: `"finance"` - Single word tags
- **Pair**: `"Environment:Dev"` - Key-value pairs
- **List**: `"SecondaryDomains: [XXXXX, PPPP]"` - Key with multiple values

### 9. Consistent camelCase Serialization

**Decision**: Use camelCase for all JSON/YAML serialization

**Rationale**:
- **ODCS Alignment**: Matches ODCS format conventions
- **Consistency**: Same format across all models and schemas
- **Frontend Friendly**: Common convention for JavaScript/TypeScript APIs
- **Standard Practice**: Widely adopted in JSON APIs

**Implementation**:
- All structs use `#[serde(rename_all = "camelCase")]`
- Enum variants serialize as camelCase (e.g., `oneToMany`, `sourceToTarget`)
- Field names: `sourceTableId`, `targetCardinality`, `flowDirection`, etc.

### 10. Crow's Feet Notation for Cardinality

**Decision**: Support standard crow's feet notation for endpoint cardinality

**Rationale**:
- **Industry Standard**: Widely recognized ERD notation
- **Precision**: More precise than simple OneToMany/ManyToMany
- **Data Modeling**: Essential for proper data flow diagrams
- **Bi-directional**: Supports asymmetric cardinality at each endpoint

**Cardinality Values**:
- `zeroOrOne` (0..1): Optional single
- `exactlyOne` (1..1): Required single
- `zeroOrMany` (0..*): Optional multiple
- `oneOrMany` (1..*): Required multiple

**Flow Directions**:
- `sourceToTarget`: Unidirectional from source
- `targetToSource`: Unidirectional from target
- `bidirectional`: Data flows both ways

---

## When to Use the SDK

### ✅ Use the SDK When:

1. **Building Data Governance Tools**
   - Data catalog applications
   - Schema registry systems
   - Data contract management platforms
   - Data lineage tools

2. **Cross-platform Applications**
   - Desktop applications (native)
   - Web applications (WASM)
   - Mobile applications (via API backend)
   - CLI tools

3. **Multi-format Support Required**
   - Need to import from multiple formats (SQL, JSON Schema, AVRO, etc.)
   - Need to export to multiple formats
   - Format conversion workflows

4. **Domain-Driven Data Organization**
   - Organizing data by business domains
   - Managing data products
   - Tracking compute assets (AI/ML models, applications)

5. **Validation & Quality Assurance**
   - Schema validation
   - Conflict detection
   - Circular dependency detection
   - Naming convention enforcement

6. **Storage Abstraction Needed**
   - Applications that need to work across different storage backends
   - Offline-first applications (browser storage)
   - Cloud-native applications (API backend)

### ❌ Don't Use the SDK When:

1. **Simple Single-format Use Cases**
   - If you only need to work with one format and don't need conversion
   - Consider format-specific libraries instead

2. **Non-Rust Applications**
   - The SDK is Rust-only
   - For other languages, consider the HTTP API backend or WASM bindings

3. **Real-time Streaming**
   - The SDK focuses on batch operations
   - Not designed for streaming data processing

4. **Direct Database Operations**
   - The SDK works with schema definitions, not live databases
   - Use database drivers for direct database access

---

## Architecture Overview

### High-Level Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                    Application Layer                          │
│  (Native App / Web App / API Server / CLI Tool)             │
└──────────────────────┬──────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│                    Data Modelling SDK                         │
├─────────────────────────────────────────────────────────────┤
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐      │
│  │   Import     │  │   Export     │  │   Convert    │      │
│  │  (Formats)   │  │  (Formats)   │  │ (Universal)  │      │
│  └──────────────┘  └──────────────┘  └──────────────┘      │
│                                                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐      │
│  │   Models     │  │  Validation  │  │   Domain    │      │
│  │  (Core)      │  │  (Rules)     │  │ (Org)       │      │
│  └──────────────┘  └──────────────┘  └──────────────┘      │
│                                                              │
│  ┌──────────────────────────────────────────────────────┐   │
│  │         Storage Backend Abstraction                   │   │
│  │  (Trait-based, platform-agnostic)                    │   │
│  └──────────────────────────────────────────────────────┘   │
└──────────────────────┬──────────────────────────────────────┘
        ┌───────────────┼───────────────┐
        ▼               ▼               ▼
┌─────────────┐  ┌─────────────┐  ┌─────────────┐
│ File System │  │   Browser   │  │  HTTP API   │
│  Backend    │  │   Backend   │  │   Backend   │
└─────────────┘  └─────────────┘  └─────────────┘
```

### Component Layers

1. **Application Layer**: Your application code
2. **SDK Public API**: High-level operations (import, export, validation)
3. **Core Models**: Data structures (Table, Column, Domain, etc.)
4. **Storage Abstraction**: Platform-independent storage operations
5. **Platform Implementations**: Specific storage backends

---

## Design Principles

### 1. Platform Independence

The SDK abstracts platform-specific operations behind traits, enabling the same code to run on:
- **Native**: File system operations via Tokio
- **Web**: Browser storage (IndexedDB/localStorage) via WASM
- **API**: HTTP operations via Reqwest

### 2. Format Agnosticism

The SDK supports multiple formats but maintains ODCS as the canonical internal format:
- **Import**: Convert any format → ODCS models
- **Export**: Convert ODCS models → any format
- **Universal Converter**: Direct format-to-format conversion

### 3. Domain-Driven Organization

Files and models are organized by business domain:
- **Domain**: Top-level container for related assets
- **Systems**: Physical infrastructure (Kafka, databases, etc.)
- **ODCS Nodes**: Data contracts (tables)
- **CADS Nodes**: Compute assets (AI/ML models, applications)
- **ODPS Products**: Data products linking multiple contracts

### 4. Format Compatibility

The SDK maintains format compatibility:
- ODCL v1.2.1 format supported (last version)
- Migration utilities for DataFlow → Domain
- Flat file naming convention for all assets

### 5. Extensibility

The SDK is designed for extension:
- **Custom Properties**: All models support custom metadata
- **Feature Flags**: Optional functionality gated behind features
- **Trait-based**: Easy to add new storage backends or exporters

### 6. Validation First

Validation is built into the core:
- **Input Validation**: Table/column names, UUIDs
- **Schema Validation**: Naming conflicts, circular dependencies
- **Format Validation**: JSON Schema validation against official schemas

---

## Component Architecture

### Core Components

#### 1. Models (`src/models/`)

Core data structures representing data contracts and domain organization:

- **`Table`**: Data contract with columns, metadata, relationships
- **`Column`**: Column definition with type, constraints, quality rules
- **`Relationship`**: Relationship between tables
- **`Domain`**: Business domain container
- **`System`**: Physical infrastructure entity
- **`CADSAsset`**: Compute asset (AI/ML model, application)
- **`ODPSDataProduct`**: Data product linking contracts
- **`DataModel`**: Container for tables, relationships, domains

#### 2. Import (`src/import/`)

Format-specific importers converting external formats to SDK models:

- **`ODCSImporter`**: ODCS v3.1.0 (primary format)
- **`ODCLImporter`**: ODCL v1.2.1 (legacy, via ODCSImporter)
- **`CADSImporter`**: CADS v1.0 (compute assets, supports BPMN/DMN/OpenAPI references)
- **`ODPSImporter`**: ODPS (data products)
- **`BPMNImporter`**: BPMN 2.0 XML (process models, requires `bpmn` feature)
- **`DMNImporter`**: DMN 1.3 XML (decision models, requires `dmn` feature)
- **`OpenAPIImporter`**: OpenAPI 3.1.1 YAML/JSON (API specs, requires `openapi` feature)
- **`SQLImporter`**: SQL DDL parsing
- **`JSONSchemaImporter`**: JSON Schema conversion
- **`AvroImporter`**: AVRO schema conversion
- **`ProtobufImporter`**: Protobuf .proto parsing

#### 3. Export (`src/export/`)

Format-specific exporters converting SDK models to external formats:

- **`ODCSExporter`**: ODCS v3.1.0 export
- **`ODCLExporter`**: ODCL v1.2.1 export (legacy)
- **`CADSExporter`**: CADS v1.0 export (supports BPMN/DMN/OpenAPI references)
- **`ODPSExporter`**: ODPS export
- **`BPMNExporter`**: BPMN 2.0 XML export (requires `bpmn` feature)
- **`DMNExporter`**: DMN 1.3 XML export (requires `dmn` feature)
- **`OpenAPIExporter`**: OpenAPI 3.1.1 YAML/JSON export with format conversion (requires `openapi` feature)
- **`SQLExporter`**: SQL DDL generation
- **`JSONSchemaExporter`**: JSON Schema generation
- **`AvroExporter`**: AVRO schema generation
- **`ProtobufExporter`**: Protobuf .proto generation

#### 4. Convert (`src/convert/`)

Universal format conversion:

- **`convert_to_odcs()`**: Convert any format → ODCS YAML
- **`auto_detect_format()`**: Detect input format automatically
- **`migrate_dataflow_to_domain()`**: Migrate legacy DataFlow → Domain

#### 5. Validation (`src/validation/`)

Validation logic:

- **`TableValidator`**: Table name validation, conflict detection
- **`RelationshipValidator`**: Circular dependency detection
- **`InputValidator`**: Input sanitization and validation

#### 6. Model Management (`src/model/`)

Loading and saving models:

- **`ModelLoader`**: Load models from storage
- **`ModelSaver`**: Save models to storage
- **`ApiModelLoader`**: Load via HTTP API

#### 7. Storage (`src/storage/`)

Storage backend abstraction:

- **`StorageBackend`**: Trait defining storage operations
- **`FileSystemStorageBackend`**: Native file system (feature-gated)
- **`BrowserStorageBackend`**: Browser storage (WASM, feature-gated)
- **`ApiStorageBackend`**: HTTP API (default)

#### 8. Staging (`src/staging/`) - Feature: `staging`

Data staging and ingestion layer using DuckDB:

- **`StagingDatabase`**: DuckDB-backed storage for staged JSON data
- **`BatchTracker`**: Resume-capable batch tracking with metadata
- **`JsonIngester`**: High-performance JSON file ingestion (glob patterns, partitioning)
- **`QueryEngine`**: SQL query interface for staged data
- **`SchemaExporter`**: Export staged data to various formats
- **`IngestProgress`**: Real-time progress reporting with indicatif (files, records, bytes)
- **`InferenceProgress`**: Progress bar for schema inference operations
- **`Spinner`**: Simple spinner for indeterminate operations
- **`S3Ingester`**: AWS S3 ingestion with streaming download (feature: `s3`)
- **`UnityVolumeIngester`**: Databricks Unity Catalog Volumes ingestion (feature: `databricks`)
- **`SecureCredentials`**: Credential wrapper preventing accidental logging
- **`redact_secrets_in_string()`**: Regex-based secret redaction for logs

**Key Features**:
- Stage raw JSON files before schema inference
- Partition-based organization for large datasets
- Resume interrupted ingestion from last successful batch
- Query staged data with SQL before committing to schema
- Real-time progress reporting with throughput metrics
- S3 ingestion with AWS SDK for Rust (streaming, parallel)
- Databricks Unity Catalog Volumes ingestion via REST API
- Secure credential handling with Display trait redaction
- Automatic secret redaction in error messages and logs

#### 9. Inference (`src/inference/`) - Feature: `inference`

Automatic schema inference from staged data:

- **`SchemaInferer`**: Analyze JSON data to infer column types
- **`TypeDetector`**: Detect 18+ semantic types (email, UUID, URL, phone, etc.)
- **`FormatDetector`**: Identify date/time formats, patterns
- **`StatisticsCollector`**: Gather column statistics (nullability, uniqueness, cardinality)
- **`ConflictResolver`**: Handle type conflicts across records
- **`ConstraintInferer`**: Infer constraints (NOT NULL, UNIQUE, ranges)
- **`SchemaRefiner`**: LLM-enhanced schema refinement

**Type Detection Hierarchy**:
```
Semantic Types (highest priority)
├── uuid, email, url, phone, ip_address
├── currency, percentage, credit_card
├── iso_country, iso_language, iso_currency
└── date, time, datetime, timestamp

Structural Types
├── boolean, integer, float, decimal
├── string, text, json, array
└── object, binary
```

#### 10. Mapping (`src/mapping/`) - Feature: `mapping`

Schema mapping and transformation:

- **`SchemaMatcher`**: Match source fields to target schema
- **`LlmMatcher`**: LLM-enhanced matching with semantic understanding
- **`MappingGenerator`**: Generate field mappings with confidence scores
- **`TransformGenerator`**: Generate transformation scripts (SQL, JQ, Python, PySpark)
- **`TransformValidator`**: Validate transformation correctness
- **`MappingPersistence`**: Save/load mapping configurations

**Matching Strategies**:
- Exact name matching
- Fuzzy name matching (Levenshtein distance)
- Type compatibility analysis
- Semantic matching via LLM
- Pattern-based matching

**Transform Outputs**:
- SQL (standard, Databricks, Snowflake dialects)
- JQ for JSON transformation
- Python/PySpark for complex transformations

#### 11. Pipeline (`src/pipeline/`) - Feature: `pipeline`

End-to-end data modeling pipeline:

- **`Pipeline`**: Orchestrate multi-stage data processing
- **`PipelineStage`**: Individual stage definitions
- **`Checkpoint`**: Save/restore pipeline state
- **`StageExecutor`**: Execute individual stages
- **`PipelineConfig`**: Pipeline configuration and options

**Pipeline Stages**:
```
1. Ingest   → Stage raw JSON files to DuckDB
2. Infer    → Analyze data and infer schema
3. Refine   → LLM-enhanced schema refinement
4. Map      → Match to target schema, generate transforms
5. Export   → Generate ODCS, SQL, or other formats
```

**Checkpoint System**:
- Save state after each stage
- Resume from any checkpoint
- Track stage completion and metadata

#### 12. LLM Integration (`src/llm/`) - Feature: `llm`

LLM provider abstraction for AI-enhanced features:

- **`LlmProvider`**: Trait for LLM provider implementations
- **`OpenAIProvider`**: OpenAI GPT-4/GPT-3.5 integration
- **`OllamaProvider`**: Local Ollama model support
- **`AnthropicProvider`**: Anthropic Claude integration
- **`LlmConfig`**: Provider configuration and API keys
- **`PromptBuilder`**: Build prompts for schema operations

**Feature Flags**:
- `llm`: Core LLM abstraction
- `llm-openai`: OpenAI provider
- `llm-ollama`: Ollama provider
- `llm-anthropic`: Anthropic provider

**LLM-Enhanced Operations**:
- Schema refinement (better names, descriptions, types)
- Semantic field matching
- Transformation hint generation
- Documentation generation

---

## Storage Architecture

### Storage Backend Trait

All storage operations go through the `StorageBackend` trait:

```rust
#[async_trait(?Send)]
pub trait StorageBackend: Send + Sync {
    async fn read_file(&self, path: &str) -> Result<Vec<u8>, StorageError>;
    async fn write_file(&self, path: &str, content: &[u8]) -> Result<(), StorageError>;
    async fn list_files(&self, dir: &str) -> Result<Vec<String>, StorageError>;
    async fn file_exists(&self, path: &str) -> Result<bool, StorageError>;
    async fn delete_file(&self, path: &str) -> Result<(), StorageError>;
    async fn create_dir(&self, path: &str) -> Result<(), StorageError>;
    async fn dir_exists(&self, path: &str) -> Result<bool, StorageError>;
}
```

### Platform Implementations

#### File System Backend (`native-fs` feature)

- **Platform**: Native applications (desktop, CLI, server)
- **Storage**: Local file system
- **Runtime**: Tokio async runtime
- **Use Case**: Desktop applications, CLI tools, server applications

#### Browser Storage Backend (`wasm` feature)

- **Platform**: Web applications (WASM)
- **Storage**: IndexedDB or localStorage
- **Runtime**: WASM runtime (browser-provided)
- **Use Case**: Web applications, offline-first apps

#### API Backend (`api-backend` feature, default)

- **Platform**: Any (HTTP client)
- **Storage**: Remote HTTP API
- **Runtime**: Reqwest HTTP client
- **Use Case**: Cloud-native applications, mobile apps, microservices

---

## Database Architecture

The SDK includes an optional database layer that provides 10-100x performance improvements over file-based operations for large workspaces. The database caches YAML data in an indexed format for fast queries.

### Database Backend Trait

All database operations go through the `DatabaseBackend` trait:

```rust
#[async_trait(?Send)]
pub trait DatabaseBackend: Send + Sync {
    async fn initialize(&self) -> DatabaseResult<()>;
    async fn health_check(&self) -> DatabaseResult<bool>;
    async fn execute_query(&self, sql: &str) -> DatabaseResult<QueryResult>;
    async fn sync_tables(&self, workspace_id: Uuid, tables: &[Table]) -> DatabaseResult<usize>;
    async fn sync_domains(&self, workspace_id: Uuid, domains: &[Domain]) -> DatabaseResult<usize>;
    async fn export_tables(&self, workspace_id: Uuid) -> DatabaseResult<Vec<Table>>;
    // ... more operations
}
```

### Database Backends

#### DuckDB Backend (`duckdb-backend` feature)

- **Type**: Embedded analytical database
- **Use Case**: CLI tools, local development, offline analysis
- **Performance**: Excellent for analytical queries, columnar storage
- **File**: `.data-model.duckdb` in workspace root

#### PostgreSQL Backend (`postgres-backend` feature)

- **Type**: Server-based relational database
- **Use Case**: Team environments, server deployments, shared access
- **Performance**: Excellent for concurrent access, ACID transactions
- **Connection**: Via connection string (e.g., `postgresql://user:pass@localhost/db`)

### Sync Engine

The `SyncEngine` manages bidirectional synchronization between YAML files and the database:

```
YAML Files ←→ SyncEngine ←→ Database
```

**Features**:
- **Incremental Sync**: Only syncs changed files using SHA256 hashes
- **Bidirectional**: YAML → Database (import) and Database → YAML (export)
- **Change Detection**: Tracks file hashes to detect modifications
- **Conflict Resolution**: Database is source of truth during export

### Database Schema

The database schema mirrors the YAML structure:

- **workspaces**: Workspace metadata and configuration
- **domains**: Business domain definitions
- **tables**: Table/data contract definitions
- **columns**: Column definitions with all ODCS properties
- **relationships**: Table relationships and foreign keys
- **file_hashes**: File hash tracking for incremental sync

### Git Hooks Integration

The database layer integrates with Git for automatic synchronization:

**Pre-commit Hook**:
- Exports database changes to YAML files
- Ensures YAML files are up-to-date before commit

**Post-checkout Hook**:
- Syncs YAML files to database after checkout
- Keeps database in sync with branch changes

### CLI Commands

The database functionality is exposed via CLI commands:

```bash
# Initialize database for a workspace
data-modelling-cli db init --workspace ./my-workspace --backend duckdb

# Sync YAML files to database
data-modelling-cli db sync --workspace ./my-workspace

# Check database status
data-modelling-cli db status --workspace ./my-workspace

# Export database to YAML files
data-modelling-cli db export --workspace ./my-workspace

# Query the database directly
data-modelling-cli query "SELECT * FROM tables" --workspace ./my-workspace
```

### Configuration

Database configuration is stored in `.data-model.toml`:

```toml
[database]
backend = "duckdb"
path = ".data-model.duckdb"

[sync]
auto_sync = true
watch = false

[git]
hooks_enabled = true

[postgres]
connection_string = "postgresql://localhost/datamodel"
pool_size = 5
```

### Process Flow

```
┌─────────────────────────────────────────────────────────────────┐
│                        Workspace                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                   │
│  ┌─────────────┐     ┌─────────────┐     ┌─────────────┐        │
│  │ YAML Files  │ ←─→ │ SyncEngine  │ ←─→ │  Database   │        │
│  │ (.odcs.yaml)│     │             │     │  (DuckDB/   │        │
│  │ (.odps.yaml)│     │             │     │  PostgreSQL)│        │
│  │ (.cads.yaml)│     │             │     │             │        │
│  └─────────────┘     └─────────────┘     └─────────────┘        │
│         │                   │                   │                 │
│         │                   │                   │                 │
│         ▼                   ▼                   ▼                 │
│  ┌─────────────┐     ┌─────────────┐     ┌─────────────┐        │
│  │ Git Hooks   │     │   CLI       │     │ SQL Queries │        │
│  │ (pre-commit │     │ (db init,   │     │ (query cmd) │        │
│  │  post-      │     │  db sync,   │     │             │        │
│  │  checkout)  │     │  db status) │     │             │        │
│  └─────────────┘     └─────────────┘     └─────────────┘        │
│                                                                   │
└─────────────────────────────────────────────────────────────────┘
```

---

## Data Pipeline Architecture

The SDK includes a comprehensive data pipeline system for end-to-end schema discovery and transformation.

### Pipeline Overview

```
┌─────────────────────────────────────────────────────────────────┐
│                      Data Pipeline                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                   │
│  ┌─────────┐    ┌─────────┐    ┌─────────┐    ┌─────────┐       │
│  │ INGEST  │ →  │ INFER   │ →  │ REFINE  │ →  │   MAP   │ → ... │
│  │         │    │         │    │  (LLM)  │    │         │       │
│  └─────────┘    └─────────┘    └─────────┘    └─────────┘       │
│       │              │              │              │             │
│       ▼              ▼              ▼              ▼             │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │              Checkpoint Storage (.checkpoints/)          │    │
│  └─────────────────────────────────────────────────────────┘    │
│                                                                   │
└─────────────────────────────────────────────────────────────────┘
```

### Stage Details

| Stage | Input | Output | Feature Flag |
|-------|-------|--------|--------------|
| **Ingest** | JSON files (glob pattern) | DuckDB staged_json table | `staging` |
| **Infer** | Staged JSON data | InferredSchema (types, stats) | `inference` |
| **Refine** | InferredSchema | Refined schema (LLM-enhanced) | `llm` |
| **Map** | Source + Target schemas | SchemaMapping + transforms | `mapping` |
| **Export** | Mapping | ODCS, SQL, transforms | core |

### Checkpoint System

Each stage creates a checkpoint that can be used to resume processing:

```
.checkpoints/
├── ingest.json      # Files processed, batch IDs
├── infer.json       # Inferred schema snapshot
├── refine.json      # LLM refinements applied
├── map.json         # Field mappings, transforms
└── export.json      # Output files generated
```

**Checkpoint Contents**:
- Stage name and completion status
- Timestamp of completion
- Stage-specific data (serialized)
- Dependencies and configuration

### Staging Layer

The staging layer uses DuckDB for high-performance JSON ingestion:

```sql
-- Staged JSON table structure
CREATE TABLE staged_json (
    id INTEGER PRIMARY KEY,
    partition VARCHAR,
    file_path VARCHAR,
    line_number INTEGER,
    raw_json JSON,
    ingested_at TIMESTAMP
);

-- Batch tracking
CREATE TABLE batch_metadata (
    batch_id VARCHAR PRIMARY KEY,
    partition VARCHAR,
    file_pattern VARCHAR,
    files_processed INTEGER,
    records_ingested INTEGER,
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    status VARCHAR
);
```

### Schema Inference Pipeline

```
Raw JSON Records
┌─────────────────┐
│  Sample Records │ (configurable sample size)
└────────┬────────┘
┌─────────────────┐
│  Type Detection │ → Semantic types (email, UUID, URL, etc.)
└────────┬────────┘
┌─────────────────┐
│ Statistics      │ → Nullability, uniqueness, cardinality
└────────┬────────┘
┌─────────────────┐
│ Constraint      │ → NOT NULL, UNIQUE, value ranges
│ Inference       │
└────────┬────────┘
┌─────────────────┐
│ LLM Refinement  │ → Better names, descriptions, types
│ (optional)      │
└────────┬────────┘
    InferredSchema
```

### Mapping Pipeline

```
Source Schema          Target Schema
      │                      │
      └──────────┬───────────┘
        ┌─────────────────┐
        │ Field Matching  │
        │ (name + type +  │
        │  semantic/LLM)  │
        └────────┬────────┘
        ┌─────────────────┐
        │ Transform       │ → Type conversions, format changes
        │ Detection       │
        └────────┬────────┘
        ┌─────────────────┐
        │ Script          │ → SQL, JQ, Python, PySpark
        │ Generation      │
        └────────┬────────┘
           SchemaMapping
```

---

## File Organization

### Flat File Structure

Files are organized using a flat naming convention within a workspace:

```
workspace/
├── schemas/                                        # Schema reference (JSON Schema files)
│   ├── odcs-json-schema-v3.1.0.json
│   ├── odcl-json-schema-1.2.1.json
│   ├── odps-json-schema-latest.json
│   └── cads.schema.json
├── workspace.yaml                                  # Workspace metadata with assets and relationships
├── myworkspace_customer-service_customers.odcs.yaml    # ODCS table
├── myworkspace_customer-service_orders.odcs.yaml       # ODCS table
├── myworkspace_customer-service_customer-product.odps.yaml  # ODPS product
├── myworkspace_customer-service_recommendation-model.cads.yaml  # CADS asset
├── myworkspace_order-processing_shipments.odcs.yaml    # Another domain's table
└── myworkspace_order-processing_tracking.odcs.yaml
```

### File Naming Convention

Pattern: `{workspace}_{domain}_{system}_{resource}.{type}.yaml`

- **Workspace file**: `workspace.yaml` (contains domains, systems, assets, relationships)
- **ODCS tables**: `{workspace}_{domain}_{resource}.odcs.yaml`
- **ODPS products**: `{workspace}_{domain}_{resource}.odps.yaml`
- **CADS assets**: `{workspace}_{domain}_{resource}.cads.yaml`
- **With system**: `{workspace}_{domain}_{system}_{resource}.{type}.yaml`

### Benefits

1. **Logical Grouping**: Domain/system encoded in filename
2. **Scalability**: Easy to manage large numbers of assets
3. **Ownership**: Clear ownership via naming convention
4. **Version Control**: Better Git history with flat structure
5. **Discovery**: Easy to find assets by filename pattern

---

## Integration Patterns

### Pattern 1: Native Application

```rust
use data_modelling_sdk::storage::filesystem::FileSystemStorageBackend;
use data_modelling_sdk::model::{ModelLoader, ModelSaver};
use data_modelling_sdk::import::ODCSImporter;

// Initialize storage
let storage = FileSystemStorageBackend::new("/path/to/workspace");
let loader = ModelLoader::new(storage.clone());
let saver = ModelSaver::new(storage);

// Load domains
let result = loader.load_domains("workspace").await?;

// Import new table
let mut importer = ODCSImporter::new();
let (table, _) = importer.parse_table(odcs_yaml)?;

// Save to domain
saver.save_domain("workspace", &domain, &tables, &products, &assets).await?;
```

### Pattern 2: Web Application (WASM)

```rust
use data_modelling_sdk::storage::browser::BrowserStorageBackend;
use data_modelling_sdk::model::ModelLoader;

// Initialize browser storage
let storage = BrowserStorageBackend::new("db_name", "store_name");
let loader = ModelLoader::new(storage);

// Load domains
let result = loader.load_domains("workspace").await?;

// Use in JavaScript
#[wasm_bindgen]
pub fn load_domains() -> Promise {
    // WASM bindings handle async
}
```

### Pattern 3: API Server

```rust
use data_modelling_sdk::storage::api::ApiStorageBackend;
use data_modelling_sdk::model::ApiModelLoader;

// Initialize API backend
let storage = ApiStorageBackend::new("https://api.example.com", Some("session_id"));
let loader = ApiModelLoader::new(storage);

// Load via API
let result = loader.load_model("domain-name").await?;
```

### Pattern 4: Format Conversion

```rust
use data_modelling_sdk::convert::convert_to_odcs;

// Convert any format to ODCS
let odcs_yaml = convert_to_odcs(input_yaml, None)?;
```

### Pattern 5: Data Pipeline (Staging → Schema → Mapping)

```rust
use data_modelling_sdk::pipeline::{Pipeline, PipelineConfig, PipelineStage};
use data_modelling_sdk::staging::StagingDatabase;
use data_modelling_sdk::inference::SchemaInferer;
use data_modelling_sdk::mapping::SchemaMatcher;

// Initialize pipeline with checkpointing
let config = PipelineConfig {
    checkpoint_dir: ".checkpoints".into(),
    source_pattern: "data/**/*.json".into(),
    target_schema: Some("target_schema.odcs.yaml".into()),
    ..Default::default()
};
let mut pipeline = Pipeline::new(config)?;

// Run full pipeline (resumes from checkpoint if available)
let result = pipeline.run().await?;

// Or run individual stages
pipeline.run_stage(PipelineStage::Ingest).await?;
pipeline.run_stage(PipelineStage::Infer).await?;
pipeline.run_stage(PipelineStage::Map).await?;

// Access results
let schema = pipeline.get_inferred_schema()?;
let mapping = pipeline.get_schema_mapping()?;
```

### Pattern 6: LLM-Enhanced Schema Refinement

```rust
use data_modelling_sdk::llm::{LlmProvider, OpenAIProvider};
use data_modelling_sdk::inference::SchemaRefiner;

// Initialize LLM provider
let provider = OpenAIProvider::new(api_key, "gpt-4")?;

// Refine inferred schema with LLM
let refiner = SchemaRefiner::new(Box::new(provider));
let refined_schema = refiner.refine(&inferred_schema).await?;

// Refinements include:
// - Better column names (snake_case normalization)
// - Semantic type detection (email, phone, URL)
// - Business-friendly descriptions
// - Suggested constraints
```

### Pattern 7: Schema Mapping with Transform Generation

```rust
use data_modelling_sdk::mapping::{SchemaMatcher, TransformGenerator, TransformFormat};

// Match source to target schema
let matcher = SchemaMatcher::new();
let mapping = matcher.match_schemas(&source_schema, &target_schema)?;

// Generate transformation scripts
let generator = TransformGenerator::new();

// SQL transformation
let sql = generator.generate(&mapping, TransformFormat::Sql)?;

// JQ transformation
let jq = generator.generate(&mapping, TransformFormat::Jq)?;

// PySpark transformation
let pyspark = generator.generate(&mapping, TransformFormat::PySpark)?;
```

---

## Use Cases

### Use Case 1: Data Catalog Application

**Scenario**: Build a data catalog that allows users to discover, document, and manage data assets.

**SDK Usage**:
- Import schemas from various sources (SQL, JSON Schema, AVRO)
- Organize by business domain
- Export to multiple formats for different consumers
- Validate schemas and detect conflicts

**Architecture**:
```
Web Frontend (WASM)
Data Modelling SDK (Browser Storage)
IndexedDB (Browser)
```

### Use Case 2: Schema Registry

**Scenario**: Centralized schema registry for microservices.

**SDK Usage**:
- Store schemas in domain-based structure
- Validate schemas against JSON Schema definitions
- Provide format conversion (AVRO → JSON Schema → ODCS)
- Track schema versions and relationships

**Architecture**:
```
Microservices
HTTP API
Data Modelling SDK (API Backend)
File System / Object Storage
```

### Use Case 3: Data Governance Platform

**Scenario**: Platform for data governance, lineage, and quality.

**SDK Usage**:
- Import data contracts from various sources
- Organize by business domain
- Track compute assets (AI/ML models, ETL pipelines)
- Validate schemas and detect conflicts
- Export governance reports

**Architecture**:
```
Governance UI
Data Modelling SDK (File System)
Git Repository (Version Control)
```

### Use Case 4: CLI Tool

**Scenario**: Command-line tool for schema management.

**SDK Usage**:
- Import schemas from files
- Convert between formats
- Validate schemas
- Generate documentation

**Architecture**:
```
CLI Tool
Data Modelling SDK (File System)
Local File System
```

### Use Case 5: Data Product Management

**Scenario**: Manage data products linking multiple data contracts.

**SDK Usage**:
- Define ODPS data products
- Link to ODCS tables
- Organize by domain
- Track product versions and status

**Architecture**:
```
Product Management UI
Data Modelling SDK (API Backend)
Cloud Storage (S3, Azure Blob)
```

### Use Case 6: Schema Discovery Pipeline

**Scenario**: Automatically discover and document schemas from raw JSON data files.

**SDK Usage**:
- Stage JSON files with batch tracking and resume support
- Infer schema with type detection (18+ semantic types)
- Refine schema with LLM for better names/descriptions
- Map to existing target schema
- Generate transformation scripts (SQL, JQ, PySpark)
- Export to ODCS data contracts

**Architecture**:
```
JSON Files (S3/Local)
Staging Layer (DuckDB)
Schema Inference
LLM Refinement (OpenAI/Ollama)
Schema Mapping
ODCS Export + Transform Scripts
```

### Use Case 7: Data Migration with Schema Mapping

**Scenario**: Migrate data between systems with different schemas.

**SDK Usage**:
- Import source schema (SQL DDL, JSON Schema, etc.)
- Import target schema (ODCS, SQL DDL, etc.)
- Generate field mappings with confidence scores
- Review and adjust mappings
- Generate transformation scripts for ETL tools
- Validate transformations

**Architecture**:
```
Source System Schema
Schema Matcher (+ LLM)
Mapping Configuration
Transform Generator
ETL Pipeline (SQL/Spark/JQ)
Target System
```

---

## Summary

The Data Modelling SDK provides a robust, cross-platform foundation for data modeling operations. Key strengths:

- **Multi-platform**: Works in native, web, and API environments
- **Multi-format**: Supports all major data contract formats
- **Domain-driven**: Organizes assets by business domain
- **Validation**: Built-in validation and conflict detection
- **Extensible**: Easy to extend with new formats or backends
- **Schema Discovery**: Automatic schema inference from raw data
- **LLM-Enhanced**: AI-powered schema refinement and matching
- **Transform Generation**: Automatic SQL, JQ, PySpark script generation
- **Pipeline Support**: End-to-end data modeling with checkpointing

Use the SDK when building data governance tools, schema registries, data catalogs, data migration pipelines, or any application that needs to work with data contracts across multiple formats and platforms.

For more information:
- [Schema Overview Guide]SCHEMA_OVERVIEW.md - Detailed schema documentation
- [Pipeline Tutorial]PIPELINE_TUTORIAL.md - End-to-end pipeline guide
- [CLI Reference]CLI.md - Command-line interface documentation
- [README]../README.md - Quick start and usage examples
- [LLM.txt]../LLM.txt - Technical reference