Free, simple, and intuitive online database diagram editor and SQL generator.
JavaScript databasedatabase-designdatabase-diagramdatabase-schemadbmldiagram-editoreditorer-diagramerderdiagramindexeddbmariadbmcporacle-databaseoracle-dbpostgresqlschema-designsqlsql-serversqlite
Suppost postgres
1ilit36ad44f0db15
diff --git a/src/components/control_panel.jsx b/src/components/control_panel.jsxindex e71e3715..b31bbac0 100644--- a/src/components/control_panel.jsx+++ b/src/components/control_panel.jsx@@ -37,7 +37,8 @@ import {exitFullscreen,ddbDiagramIsValid,dataURItoBlob,- jsonToSQL,+ jsonToMySQL,+ jsonToPostgreSQL,} from "../utils";import {AreaContext,@@ -728,7 +729,21 @@ export default function ControlPanel(props) {{MySQL: () => {setVisible(MODAL.CODE);- const src = jsonToSQL({+ const src = jsonToMySQL({+ tables: tables,+ references: relationships,+ });+ setExportData((prev) => ({+ ...prev,+ data: src,+ extension: "sql",+ }));+ },+ },+ {+ PostgreSQL: () => {+ setVisible(MODAL.CODE);+ const src = jsonToPostgreSQL({tables: tables,references: relationships,});@@ -739,7 +754,6 @@ export default function ControlPanel(props) {}));},},- { PostgreSQL: () => {} },{ DBML: () => {} },],function: () => {},diff --git a/src/components/table.jsx b/src/components/table.jsxindex 347b1d50..4ad98bec 100644--- a/src/components/table.jsx+++ b/src/components/table.jsx@@ -382,7 +382,7 @@ export default function Table(props) {updateField(props.tableData.id, j, {type: value,default: "",- values: [],+ values: f.values ? [...f.values] : [],increment: incr,});} else if (isSized(value) || hasPrecision(value)) {diff --git a/src/components/table_overview.jsx b/src/components/table_overview.jsxindex 08ee4e8a..194270de 100644--- a/src/components/table_overview.jsx+++ b/src/components/table_overview.jsx@@ -228,7 +228,7 @@ export default function TableOverview(props) {updateField(i, j, {type: value,default: "",- values: [],+ values: f.values ? [...f.values] : [],increment: incr,});} else if (isSized(value) || hasPrecision(value)) {diff --git a/src/utils/index.js b/src/utils/index.jsindex eb35dfbc..bd338efc 100644--- a/src/utils/index.js+++ b/src/utils/index.js@@ -47,20 +47,57 @@ function dataURItoBlob(dataUrl) {return new Blob([intArray], { type: mimeString });}-function getTypeString(field) {- if (field.type === "UUID") {- return `VARCHAR(36)`;- }- if (isSized(field.type)) {- return `${field.type}(${field.size})`;- }- if (hasPrecision(field.type) && field.size !== "") {- return `${field.type}${field.size}`;- }- if (field.type === "SET" || field.type === "ENUM") {- return `${field.type}(${field.values.map((v) => `"${v}"`).join(", ")})`;+function getTypeString(field, dbms = "mysql") {+ if (dbms === "mysql") {+ if (field.type === "UUID") {+ return `VARCHAR(36)`;+ }+ if (isSized(field.type)) {+ return `${field.type}(${field.size})`;+ }+ if (hasPrecision(field.type) && field.size !== "") {+ return `${field.type}${field.size}`;+ }+ if (field.type === "SET" || field.type === "ENUM") {+ return `${field.type}(${field.values.map((v) => `"${v}"`).join(", ")})`;+ }+ return field.type;+ } else if (dbms === "postgres") {+ if (field.type === "SMALLINT" && field.increment) {+ return "smallserial";+ }+ if (field.type === "INT" && field.increment) {+ return "serial";+ }+ if (field.type === "BIGINT" && field.increment) {+ return "bigserial";+ }+ if (field.type === "ENUM") {+ return `${field.name}_t`;+ }+ if (field.type === "SET") {+ return `${field.name}_t[]`;+ }+ if (field.type === "TIMESTAMP") {+ return "TIMESTAMPTZ";+ }+ if (field.type === "DATETIME") {+ return `timestamp`;+ }+ if (isSized(field.type)) {+ const type =+ field.type === "BINARY"+ ? "bit"+ : field.type === "VARBINARY"+ ? "bit varying"+ : field.type.toLowerCase();+ return `${type}(${field.size})`;+ }+ if (hasPrecision(field.type) && field.size !== "") {+ return `${field.type}${field.size}`;+ }+ return field.type.toLowerCase();}- return field.type;}function hasQuotes(type) {@@ -77,7 +114,7 @@ function hasQuotes(type) {].includes(type);}-function jsonToSQL(obj) {+function jsonToMySQL(obj) {return `${obj.tables.map((table) =>@@ -139,6 +176,76 @@ function jsonToSQL(obj) {.join("\n")}`;}+function jsonToPostgreSQL(obj) {+ return `${obj.tables+ .map(+ (table) =>+ `${table.comment === "" ? "" : `/**\n${table.comment}\n*/\n`}${+ table.fields.filter((f) => f.type === "ENUM" || f.type === "SET")+ .length > 0+ ? `${table.fields+ .filter((f) => f.type === "ENUM" || f.type === "SET")+ .map(+ (f) =>+ `CREATE TYPE "${f.name}_t" AS ENUM (${f.values+ .map((v) => `'${v}'`)+ .join(", ")});\n\n`+ )}`+ : ""+ }CREATE TABLE "${table.name}" (\n${table.fields+ .map(+ (field) =>+ `${field.comment === "" ? "" : `\t-- ${field.comment}\n`}\t"${+ field.name+ }" ${getTypeString(field, "postgres")}${+ field.notNull ? " NOT NULL" : ""+ }${+ field.default !== ""+ ? ` DEFAULT ${+ hasQuotes(field.type) &&+ field.default.toLowerCase() !== "null"+ ? `'${field.default}'`+ : `${field.default}`+ }`+ : ""+ }${+ field.check === "" || !hasCheck(field.type)+ ? ""+ : ` CHECK(${field.check})`+ }`+ )+ .join(",\n")}${+ table.fields.filter((f) => f.primary).length > 0+ ? `,\n\tPRIMARY KEY(${table.fields+ .filter((f) => f.primary)+ .map((f) => `"${f.name}"`)+ .join(", ")})`+ : ""+ }\n);\n${+ table.indices.length > 0+ ? `${table.indices.map(+ (i) =>+ `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX "${+ i.name+ }"\nON "${table.name}" (${i.fields+ .map((f) => `"${f}"`)+ .join(", ")});`+ )}`+ : ""+ }`+ )+ .join("\n")}\n${obj.references+ .map(+ (r) =>+ `ALTER TABLE "${obj.tables[r.startTableId].name}"\nADD FOREIGN KEY("${+ obj.tables[r.startTableId].fields[r.startFieldId].name+ }") REFERENCES "${obj.tables[r.endTableId].name}"("${+ obj.tables[r.endTableId].fields[r.endFieldId].name+ }")\nON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()};`+ )+ .join("\n")}`;+}+function arrayIsEqual(arr1, arr2) {return JSON.stringify(arr1) === JSON.stringify(arr2);}@@ -383,7 +490,8 @@ export {jsonDiagramIsValid,ddbDiagramIsValid,dataURItoBlob,- jsonToSQL,+ jsonToMySQL,+ jsonToPostgreSQL,validateDiagram,arrayIsEqual,isSized,