From eb186246a8f1fcc28ed0090a4fa988c977ddd266 Mon Sep 17 00:00:00 2001 From: Mouri P Date: Sat, 9 Jul 2022 18:35:46 +0530 Subject: [PATCH] add foreignKey in associations --- src/builders/ModelBuilder.ts | 9 ++++++--- src/dialects/AssociationsParser.ts | 2 ++ src/dialects/utils.ts | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/builders/ModelBuilder.ts b/src/builders/ModelBuilder.ts index 81d0973..f6e8ce9 100644 --- a/src/builders/ModelBuilder.ts +++ b/src/builders/ModelBuilder.ts @@ -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 }), + } ) ] : [ diff --git a/src/dialects/AssociationsParser.ts b/src/dialects/AssociationsParser.ts index 2e862c4..76716f7 100644 --- a/src/dialects/AssociationsParser.ts +++ b/src/dialects/AssociationsParser.ts @@ -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 { @@ -139,6 +140,7 @@ export class AssociationsParser { associationName: rightCardinality === '1' ? 'HasOne' : 'HasMany', targetModel: rightModel, sourceKey: leftKey, + foreignKey: rightKey, }); associationsMetadata[rightModel].associations.push({ diff --git a/src/dialects/utils.ts b/src/dialects/utils.ts index 7fde257..17771a1 100644 --- a/src/dialects/utils.ts +++ b/src/dialects/utils.ts @@ -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; }) },