34 lines
1.4 KiB
JavaScript
34 lines
1.4 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = resolveSchemaForeignKeys;
|
|
const lodash_1 = __importDefault(require("lodash"));
|
|
function resolveSchemaForeignKeys({ dbSchema, userId }) {
|
|
var _a;
|
|
let newDbSchema = lodash_1.default.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;
|
|
}
|