28 lines
1.1 KiB
JavaScript
28 lines
1.1 KiB
JavaScript
import _ from "lodash";
|
|
export default function resolveSchemaForeignKeys({ dbSchema, userId }) {
|
|
var _a;
|
|
let newDbSchema = _.cloneDeep(dbSchema);
|
|
for (let t = 0; t < newDbSchema.tables.length; t++) {
|
|
const tableSchema = newDbSchema.tables[t];
|
|
for (let f = 0; f < tableSchema.fields.length; f++) {
|
|
const fieldSchema = tableSchema.fields[f];
|
|
if ((_a = fieldSchema.foreignKey) === null || _a === void 0 ? void 0 : _a.destinationTableColumnName) {
|
|
const fkDestinationTableIndex = newDbSchema.tables.findIndex((tbl) => {
|
|
var _a;
|
|
return tbl.tableName ==
|
|
((_a = fieldSchema.foreignKey) === null || _a === void 0 ? void 0 : _a.destinationTableName);
|
|
});
|
|
/**
|
|
* Delete current Foreign Key if related table doesn't exist
|
|
* or has been deleted
|
|
*/
|
|
if (fkDestinationTableIndex < 0) {
|
|
delete newDbSchema.tables[t].fields[f].foreignKey;
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return newDbSchema;
|
|
}
|