Skip to content

Commit

Permalink
Merge branch 'cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Aug 3, 2024
2 parents 8e430a3 + da555e4 commit a62a6ba
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
jsonToSQLite,
jsonToMariaDB,
jsonToSQLServer,
jsonToMermaid,
} from "../../utils/exportSQL/generic";
import {
ObjectType,
Expand Down Expand Up @@ -68,6 +67,7 @@ import Modal from "./Modal/Modal";
import { useTranslation } from "react-i18next";
import { exportSQL } from "../../utils/exportSQL";
import { databases } from "../../data/databases";
import { jsonToMermaid } from "../../utils/exportAs/mermaid";

export default function ControlPanel({
diagramId,
Expand Down Expand Up @@ -1052,7 +1052,6 @@ export default function ControlPanel({
database: database,
title: title,
});
// generate .md file
setExportData((prev) => ({
...prev,
data: result,
Expand Down
5 changes: 2 additions & 3 deletions src/pages/LandingPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,10 @@ const features = [
footer: "",
},
{
title: "Import",
title: "Reverse engineer",
content: (
<div>
Already have a diagram? Import a DDL script, or a JSON file to generate
or a diagram.
Already have a schema? Import a DDL script to generate a diagram.
</div>
),
footer: "",
Expand Down
47 changes: 47 additions & 0 deletions src/utils/exportAs/mermaid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { dbToTypes } from "../../data/datatypes";

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 =
field.type +
((dbToTypes[obj.database][field.type].isSized ||
dbToTypes[obj.database][field.type].hasPrecision) &&
field.size &&
field.size !== ""
? "(" + field.size + ")"
: "");
return `\t\t${fieldType} ${field.name}`;
})
.join("\n");
return `\t${table.name} {\n${fields}\n\t}`;
})
.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 `\t${startTable} ${getMermaidRelationship(r.cardinality)} ${endTable} : references`;
})
.join("\n")
: "";

return `erDiagram\n${mermaidRelationships ? `${mermaidRelationships}\n\n` : ""}${mermaidEntities}`;
}
38 changes: 0 additions & 38 deletions src/utils/exportSQL/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,41 +502,3 @@ 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}`;
}
3 changes: 2 additions & 1 deletion src/utils/modalTitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import i18n from "../i18n/i18n";
export const getModalTitle = (modal) => {
switch (modal) {
case MODAL.IMPORT:
case MODAL.IMPORT_SRC:
return i18n.t("import_diagram");
case MODAL.IMPORT_SRC:
return i18n.t("import_from_source");
case MODAL.CODE:
return i18n.t("export");
case MODAL.IMG:
Expand Down

0 comments on commit a62a6ba

Please sign in to comment.