Export enums to sql

This commit is contained in:
1ilit 2024-07-02 23:26:45 +03:00
parent dd2aafe80b
commit 3c1e72fbdd
3 changed files with 16 additions and 14 deletions

View File

@ -55,6 +55,7 @@ import {
useTypes, useTypes,
useNotes, useNotes,
useAreas, useAreas,
useEnums,
} from "../../hooks"; } from "../../hooks";
import { enterFullscreen } from "../../utils/fullscreen"; import { enterFullscreen } from "../../utils/fullscreen";
import { dataURItoBlob } from "../../utils/utils"; import { dataURItoBlob } from "../../utils/utils";
@ -99,6 +100,7 @@ export default function ControlPanel({
deleteRelationship, deleteRelationship,
database, database,
} = useTables(); } = useTables();
const { enums } = useEnums();
const { types, addType, deleteType, updateType, setTypes } = useTypes(); const { types, addType, deleteType, updateType, setTypes } = useTypes();
const { notes, setNotes, updateNote, addNote, deleteNote } = useNotes(); const { notes, setNotes, updateNote, addNote, deleteNote } = useNotes();
const { areas, setAreas, updateArea, addArea, deleteArea } = useAreas(); const { areas, setAreas, updateArea, addArea, deleteArea } = useAreas();
@ -871,6 +873,7 @@ export default function ControlPanel({
references: relationships, references: relationships,
types: types, types: types,
database: database, database: database,
enums: enums,
}); });
setExportData((prev) => ({ setExportData((prev) => ({
...prev, ...prev,

View File

@ -268,7 +268,7 @@ export const defaultTypes = new Proxy(defaultTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}), get: (target, prop) => (prop in target ? target[prop] : {}),
}); });
export const mysqlTypesBase = { const mysqlTypesBase = {
TINYINT: { TINYINT: {
type: "TINYINT", type: "TINYINT",
checkDefault: (field) => { checkDefault: (field) => {
@ -687,7 +687,7 @@ export const mysqlTypes = new Proxy(mysqlTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}), get: (target, prop) => (prop in target ? target[prop] : {}),
}); });
export const postgresTypesBase = { const postgresTypesBase = {
SMALLINT: { SMALLINT: {
type: "SMALLINT", type: "SMALLINT",
checkDefault: (field) => { checkDefault: (field) => {
@ -912,14 +912,6 @@ export const postgresTypesBase = {
hasPrecision: false, hasPrecision: false,
hasQuotes: false, hasQuotes: false,
}, },
ENUM: {
type: "ENUM",
checkDefault: (field) => true,
hasCheck: false,
isSized: false,
hasPrecision: false,
hasQuotes: true,
},
POINT: { POINT: {
type: "POINT", type: "POINT",
checkDefault: (field) => /^\(\d+,\d+\)$/.test(field.default), checkDefault: (field) => /^\(\d+,\d+\)$/.test(field.default),
@ -1110,7 +1102,7 @@ export const postgresTypes = new Proxy(postgresTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}), get: (target, prop) => (prop in target ? target[prop] : {}),
}); });
export const sqliteTypesBase = { const sqliteTypesBase = {
INTEGER: { INTEGER: {
type: "INTEGER", type: "INTEGER",
checkDefault: (field) => { checkDefault: (field) => {
@ -1246,7 +1238,7 @@ export const sqliteTypes = new Proxy(sqliteTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}), get: (target, prop) => (prop in target ? target[prop] : {}),
}); });
export const mssqlTypesBase = { const mssqlTypesBase = {
BIGINT: { type: "", checkDefault: (field) => {} }, BIGINT: { type: "", checkDefault: (field) => {} },
INTEGER: { type: "", checkDefault: (field) => {} }, INTEGER: { type: "", checkDefault: (field) => {} },
SMALLINT: { type: "", checkDefault: (field) => {} }, SMALLINT: { type: "", checkDefault: (field) => {} },
@ -1296,5 +1288,5 @@ const dbToTypesBase = {
}; };
export const dbToTypes = new Proxy(dbToTypesBase, { export const dbToTypes = new Proxy(dbToTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : []), get: (target, prop) => (prop in target ? target[prop] : {}),
}); });

View File

@ -2,7 +2,14 @@ import { dbToTypes } from "../../data/datatypes";
import { parseDefault } from "./shared"; import { parseDefault } from "./shared";
export function toPostgres(diagram) { export function toPostgres(diagram) {
return `${diagram.types.map((type) => { const enumStatements = diagram.enums
.map(
(e) =>
`CREATE TYPE "${e.name}" AS ENUM (\n${e.values.map((v) => `\t'${v}'`).join("\n")}\n);`,
)
.join("\n");
return `${enumStatements}\n${diagram.types.map((type) => {
const typeStatements = type.fields const typeStatements = type.fields
.filter((f) => f.type === "ENUM" || f.type === "SET") .filter((f) => f.type === "ENUM" || f.type === "SET")
.map( .map(