Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/builders/ModelBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,22 @@ export class ModelBuilder extends Builder {
* @param {IAssociationMetadata} association
*/
private static buildAssociationPropertyDecl(association: IAssociationMetadata): ts.PropertyDeclaration {
const { associationName, targetModel, joinModel } = association;
const { associationName, targetModel, joinModel, sourceKey, foreignKey } = association;

const targetModels = [ targetModel ];
joinModel && targetModels.push(joinModel);

return ts.createProperty(
[
...(association.sourceKey ?
...(sourceKey ?
[
generateArrowDecorator(
associationName,
targetModels,
{ sourceKey: association.sourceKey }
{
sourceKey: sourceKey,
...(foreignKey && { foreignKey: foreignKey }),
}
)
]
: [
Expand Down
2 changes: 2 additions & 0 deletions src/dialects/AssociationsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IAssociationMetadata {
targetModel: string;
joinModel?: string;
sourceKey?: string; // Left table key for HasOne and HasMany associations
foreignKey?: string; // Right table key for HasOne and HasMany associations
}

export interface IForeignKey {
Expand Down Expand Up @@ -139,6 +140,7 @@ export class AssociationsParser {
associationName: rightCardinality === '1' ? 'HasOne' : 'HasMany',
targetModel: rightModel,
sourceKey: leftKey,
foreignKey: rightKey,
});

associationsMetadata[rightModel].associations.push({
Expand Down
4 changes: 4 additions & 0 deletions src/dialects/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export const caseTransformer = (
a.sourceKey = transformer(a.sourceKey, TransformTarget.COLUMN);
}

if (a.foreignKey) {
a.foreignKey = transformer(a.foreignKey, TransformTarget.COLUMN);
}

return a;
})
},
Expand Down