Import implicit foreign keys

This commit is contained in:
1ilit 2024-07-03 03:08:53 +03:00
parent c1f651cb54
commit 61d97e8143
2 changed files with 60 additions and 7 deletions

View File

@ -265,7 +265,7 @@ const defaultTypesBase = {
};
export const defaultTypes = new Proxy(defaultTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}),
get: (target, prop) => (prop in target ? target[prop] : false),
});
const mysqlTypesBase = {
@ -684,7 +684,7 @@ const mysqlTypesBase = {
};
export const mysqlTypes = new Proxy(mysqlTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}),
get: (target, prop) => (prop in target ? target[prop] : false),
});
const postgresTypesBase = {
@ -1099,7 +1099,7 @@ const postgresTypesBase = {
};
export const postgresTypes = new Proxy(postgresTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}),
get: (target, prop) => (prop in target ? target[prop] : false),
});
const sqliteTypesBase = {
@ -1235,7 +1235,7 @@ const sqliteTypesBase = {
};
export const sqliteTypes = new Proxy(sqliteTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}),
get: (target, prop) => (prop in target ? target[prop] : false),
});
const mssqlTypesBase = {
@ -1275,7 +1275,7 @@ const mssqlTypesBase = {
};
export const mssqlTypes = new Proxy(mssqlTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}),
get: (target, prop) => (prop in target ? target[prop] : false),
});
const dbToTypesBase = {
@ -1288,5 +1288,5 @@ const dbToTypesBase = {
};
export const dbToTypes = new Proxy(dbToTypesBase, {
get: (target, prop) => (prop in target ? target[prop] : {}),
get: (target, prop) => (prop in target ? target[prop] : false),
});

View File

@ -34,8 +34,8 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
table.indices = [];
table.id = tables.length;
e.create_definitions.forEach((d) => {
const field = {};
if (d.resource === "column") {
const field = {};
field.name = d.column.column.expr.value;
let type = d.definition.dataType;
@ -154,6 +154,59 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
relationships.push(relationship);
}
}
if (d.reference_definition) {
console.log(d);
const relationship = {};
const startTable = table.name;
const startField = field.name;
const endTable = d.reference_definition.table[0].table;
const endField =
d.reference_definition.definition[0].column.expr.value;
let updateConstraint = "No action";
let deleteConstraint = "No action";
d.reference_definition.on_action.forEach((c) => {
if (c.type === "on update") {
updateConstraint = c.value.value;
updateConstraint =
updateConstraint[0].toUpperCase() +
updateConstraint.substring(1);
} else if (c.type === "on delete") {
deleteConstraint = c.value.value;
deleteConstraint =
deleteConstraint[0].toUpperCase() +
deleteConstraint.substring(1);
}
});
const startTableId = tables.length;
const endTableId = tables.findIndex((t) => t.name === endTable);
if (endTableId === -1) return;
const endFieldId = tables[endTableId].fields.findIndex(
(f) => f.name === endField,
);
if (endField === -1) return;
const startFieldId = table.fields.findIndex(
(f) => f.name === startField,
);
if (startFieldId === -1) return;
relationship.name = startTable + "_" + startField + "_fk";
relationship.startTableId = startTableId;
relationship.startFieldId = startFieldId;
relationship.endTableId = endTableId;
relationship.endFieldId = endFieldId;
relationship.updateConstraint = updateConstraint;
relationship.deleteConstraint = deleteConstraint;
relationship.cardinality = Cardinality.ONE_TO_ONE;
relationships.push(relationship);
relationships.forEach((r, i) => (r.id = i));
console.log(relationship);
}
});
table.fields.forEach((f, j) => {
f.id = j;