Parse types for postgres

This commit is contained in:
1ilit 2023-09-19 15:51:36 +03:00
parent 9a1c086870
commit 6172d48bf5
2 changed files with 16 additions and 2 deletions

View File

@ -775,7 +775,7 @@ export default function ControlPanel(props) {
relationships: relationships, relationships: relationships,
notes: notes, notes: notes,
subjectAreas: areas, subjectAreas: areas,
types: types types: types,
}, },
null, null,
2 2
@ -797,6 +797,7 @@ export default function ControlPanel(props) {
const src = jsonToMySQL({ const src = jsonToMySQL({
tables: tables, tables: tables,
references: relationships, references: relationships,
types: types,
}); });
setExportData((prev) => ({ setExportData((prev) => ({
...prev, ...prev,
@ -811,6 +812,7 @@ export default function ControlPanel(props) {
const src = jsonToPostgreSQL({ const src = jsonToPostgreSQL({
tables: tables, tables: tables,
references: relationships, references: relationships,
types: types,
}); });
setExportData((prev) => ({ setExportData((prev) => ({
...prev, ...prev,

View File

@ -177,7 +177,19 @@ function jsonToMySQL(obj) {
} }
function jsonToPostgreSQL(obj) { function jsonToPostgreSQL(obj) {
return `${obj.tables return `${obj.types.map(
(type) =>
`${type.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`
)}CREATE TYPE ${type.name} AS (\n${type.fields
.map((f) => `\t${f.name} ${getTypeString(f, "postgres")}`)
.join("\n")}\n);`
)}\n${obj.tables
.map( .map(
(table) => (table) =>
`${table.comment === "" ? "" : `/**\n${table.comment}\n*/\n`}${ `${table.comment === "" ? "" : `/**\n${table.comment}\n*/\n`}${