Permalink
Browse files

feat(SchemaBuilder): Added support for foreign keys in mapping

  • Loading branch information...
RWOverdijk committed Oct 7, 2016
1 parent e5dfe3e commit c0e07749a85342bb73d93445fb673ebe802b465e
Showing with 13 additions and 45 deletions.
  1. +13 −45 src/SchemaBuilder.ts
@@ -283,34 +283,13 @@ export class SchemaBuilder {
});
}, true);
// @todo both foreign keys go on the join table. You moron.
this.addBuilder(entity, joinTable.name, table => {
let foreign = table.foreign(foreignColumnsInverse).references(referenceColumnsInverse).inTable(targetMapping.getTableName());
let cascades = mapping.getField(property).cascades;
let foreignInverse = table.foreign(foreignColumnsInverse).references(referenceColumnsInverse).inTable(targetMapping.getTableName());
let foreign = table.foreign(referenceColumns).references(foreignColumns).inTable(mapping.getTableName());
if (!cascades) {
return;
}
cascades.forEach(cascade => {
if (cascade === Mapping.CASCADE_PERSIST) {
return;
}
if (cascade === Mapping.CASCADE_UPDATE) {
foreign.onUpdate('cascade');
} else if (cascade === Mapping.CASCADE_DELETE) {
foreign.onDelete('cascade');
}
});
this.applyCascades(mapping.getField(property).cascades, foreignInverse);
this.applyCascades(mapping.getField(property).cascades, foreign);
}, true);
foreignKeys.push({
foreign : foreignColumns,
references: referenceColumns,
inTable : joinTable.name,
owning : property
});
});
this.addBuilder(entity, mapping.getTableName(), table => {
@@ -319,33 +298,22 @@ export class SchemaBuilder {
});
foreignKeys.forEach(foreignKey => {
let foreign = table.foreign(foreignKey.foreign).references(foreignKey.references).inTable(foreignKey.inTable);
let cascades = mapping.getField(foreignKey.owning).cascades;
if (!cascades) {
return;
}
cascades.forEach(cascade => {
if (cascade === Mapping.CASCADE_PERSIST) {
return;
}
let foreign = table.foreign(foreignKey.foreign).references(foreignKey.references).inTable(foreignKey.inTable);
if (cascade === Mapping.CASCADE_UPDATE) {
foreign.onUpdate('cascade');
} else if (cascade === Mapping.CASCADE_DELETE) {
foreign.onDelete('cascade');
}
});
this.applyCascades(mapping.getField(foreignKey.owning).cascades, foreign);
});
}, true);
return this;
}
private applyCascades() {
let cascades = mapping.getField(foreignKey.owning).cascades;
/**
* Apply cascades to provided table builder.
*
* @param {string[]} cascades
* @param {Knex.ColumnBuilder} foreign
*/
private applyCascades(cascades: Array<string>, foreign: Knex.ColumnBuilder) {
if (!cascades) {
return;
}

0 comments on commit c0e0774

Please sign in to comment.