Skip to content

Commit

Permalink
Merge pull request #200 from tasnim0tantawi/mermaid-feature
Browse files Browse the repository at this point in the history
Add export mermaid feature
  • Loading branch information
1ilit authored Aug 2, 2024
2 parents 5565668 + 7dec05c commit ed12e3b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
jsonToSQLite,
jsonToMariaDB,
jsonToSQLServer,
jsonToMermaid,
} from "../../utils/exportSQL/generic";
import {
ObjectType,
Expand Down Expand Up @@ -1040,6 +1041,25 @@ export default function ControlPanel({
saveAs(blob, `${exportData.filename}.ddb`);
},
},
{
MERMAID: () => {
setModal(MODAL.CODE);
const result = jsonToMermaid({
tables: tables,
relationships: relationships,
notes: notes,
subjectAreas: areas,
database: database,
title: title,
});
// generate .md file
setExportData((prev) => ({
...prev,
data: result,
extension: "md",
}));
},
},
],
function: () => {},
},
Expand Down
38 changes: 38 additions & 0 deletions src/utils/exportSQL/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,41 @@ export function jsonToSQLServer(obj) {
)
.join("\n")}`;
}

export function jsonToMermaid(obj) {
function getMermaidRelationship(relationship) {
switch (relationship) {
case "One to one":
return "||--||";
case "One to many":
return "||--o{";
case "Many to one":
return "}o--||";
default:
return "--";
}
}
const mermaidEntities = obj.tables
.map((table) => {
const fields = table.fields
.map((field) => {
const fieldType = getTypeString(field, obj.database, "mssql");
return ` ${fieldType} ${field.name}`;
})
.join("\n");
return ` ${table.name} {\n${fields}\n }`;
})
.join("\n\n");

const mermaidRelationships = obj.relationships?.length
? obj.relationships
.map((r) => {
const startTable = obj.tables[r.startTableId].name;
const endTable = obj.tables[r.endTableId].name;
return ` ${startTable} ${getMermaidRelationship(r.cardinality)} ${endTable} : references`;
})
.join("\n")
: "";

return `erDiagram\n${mermaidRelationships ? `${mermaidRelationships}\n\n` : ""}${mermaidEntities}`;
}

0 comments on commit ed12e3b

Please sign in to comment.