mvom
Version:
Multivalue Object Mapper
72 lines (67 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
// #region Types
// #endregion
class ForeignKeyDbTransformer {
constructor(foreignKeyDefinition = null) {
this.foreignKeyDefinition = foreignKeyDefinition;
}
/** Transform schema foreign key definitions to the format required by the db server */
transform = value => {
if (typeof value !== 'string' || this.foreignKeyDefinition == null) {
return [];
}
if (this.isCompoundForeignKeyDefinition(this.foreignKeyDefinition)) {
// destructuring here because otherwise the TS compiler does not recognize that this type has been narrowed in the reduce loop
const {
foreignKeyDefinition
} = this;
const {
splitCharacter
} = foreignKeyDefinition;
// If the splitCharacter is not found in the id the foreign key validation will probably fail.
// This error may be picked up by a match validator or could indicate a programming error
const values = value.split(splitCharacter);
return values.reduce((acc, entityId, index) => {
const foreignKeyDefs = foreignKeyDefinition[index];
if (foreignKeyDefs == null) {
return acc;
}
const {
file,
keysToIgnore = [],
entityName
} = foreignKeyDefs;
if (keysToIgnore.includes(entityId)) {
return acc;
}
acc.push({
filename: file,
entityId,
entityName
});
return acc;
}, []);
}
const {
file,
keysToIgnore = [],
entityName
} = this.foreignKeyDefinition;
if (keysToIgnore.includes(value)) {
return [];
}
return [{
filename: file,
entityId: value,
entityName
}];
};
isCompoundForeignKeyDefinition(definition) {
return 'splitCharacter' in definition;
}
}
var _default = exports.default = ForeignKeyDbTransformer;