MCPcopy
hub / github.com/chartdb/chartdb / generateDBMLFromDiagram

Function generateDBMLFromDiagram

src/lib/dbml/dbml-export/dbml-export.ts:1364–1606  ·  view source on GitHub ↗
(diagram: Diagram)

Source from the content-addressed store, hash-verified

1362}
1363
1364export function generateDBMLFromDiagram(diagram: Diagram): DBMLExportResult {
1365 // Filter out fields with empty names
1366 const sanitizedTables =
1367 diagram.tables?.map((table) => {
1368 const validFields = table.fields.filter(
1369 (field) => field.name !== ''
1370 );
1371 return {
1372 ...table,
1373 fields: validFields,
1374 };
1375 }) ?? [];
1376
1377 // Filter out empty tables and duplicates in a single pass for performance
1378 const seenTableIdentifiers = new Set<string>();
1379 const tablesWithFields = sanitizedTables.filter((table) => {
1380 // Skip tables with no fields (empty tables cause DBML export to fail)
1381 if (table.fields.length === 0) {
1382 return false;
1383 }
1384
1385 // Create a unique identifier combining schema and table name
1386 const tableIdentifier = table.schema
1387 ? `${table.schema}.${table.name}`
1388 : table.name;
1389
1390 // Skip duplicate tables
1391 if (seenTableIdentifiers.has(tableIdentifier)) {
1392 return false;
1393 }
1394 seenTableIdentifiers.add(tableIdentifier);
1395 return true; // Keep unique, non-empty table
1396 });
1397
1398 // Create the base filtered diagram structure
1399 const filteredDiagram: Diagram = {
1400 ...diagram,
1401 tables: tablesWithFields,
1402 relationships:
1403 diagram.relationships?.filter((rel) => {
1404 const sourceTable = tablesWithFields.find(
1405 (t) => t.id === rel.sourceTableId
1406 );
1407 const targetTable = tablesWithFields.find(
1408 (t) => t.id === rel.targetTableId
1409 );
1410 const sourceFieldExists = sourceTable?.fields.some(
1411 (f) => f.id === rel.sourceFieldId
1412 );
1413 const targetFieldExists = targetTable?.fields.some(
1414 (f) => f.id === rel.targetFieldId
1415 );
1416
1417 return (
1418 sourceTable &&
1419 targetTable &&
1420 sourceFieldExists &&
1421 targetFieldExists

Calls 15

exportBaseSQLFunction · 0.90
fixProblematicFieldNamesFunction · 0.85
deduplicateRelationshipsFunction · 0.85
generateEnumsDBMLFunction · 0.85
sanitizeSQLforDBMLFunction · 0.85
fixArrayTypesFunction · 0.85
normalizeCharTypeFormatFunction · 0.85
fixMultilineTableNamesFunction · 0.85
fixTableBracketSyntaxFunction · 0.85
restoreTableSchemasFunction · 0.85
restoreCompositePKNamesFunction · 0.85

Tested by 1

testCaseFunction · 0.72