-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/twenty-server/src/engine/twenty-orm/utils/get-join-column.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { WorkspaceJoinColumnsMetadataArgs } from 'src/engine/twenty-orm/interfaces/workspace-join-columns-metadata-args.interface'; | ||
import { WorkspaceRelationMetadataArgs } from 'src/engine/twenty-orm/interfaces/workspace-relation-metadata-args.interface'; | ||
|
||
import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity'; | ||
import { metadataArgsStorage } from 'src/engine/twenty-orm/storage/metadata-args.storage'; | ||
|
||
export const getJoinColumn = ( | ||
joinColumnsMetadataArgsCollection: WorkspaceJoinColumnsMetadataArgs[], | ||
relationMetadataArgs: WorkspaceRelationMetadataArgs, | ||
): string | null => { | ||
if ( | ||
relationMetadataArgs.type === RelationMetadataType.ONE_TO_MANY || | ||
relationMetadataArgs.type === RelationMetadataType.MANY_TO_MANY | ||
) { | ||
return null; | ||
} | ||
|
||
const filteredJoinColumnsMetadataArgsCollection = | ||
joinColumnsMetadataArgsCollection.filter( | ||
(joinColumnsMetadataArgs) => | ||
joinColumnsMetadataArgs.relationName === relationMetadataArgs.name, | ||
); | ||
|
||
// If we're in a ONE_TO_ONE relation and there are no join columns, we need to find the join column on the inverse side | ||
if ( | ||
relationMetadataArgs.type === RelationMetadataType.ONE_TO_ONE && | ||
filteredJoinColumnsMetadataArgsCollection.length === 0 | ||
) { | ||
const inverseSideTarget = relationMetadataArgs.inverseSideTarget(); | ||
const inverseSideJoinColumnsMetadataArgsCollection = | ||
metadataArgsStorage.filterJoinColumns(inverseSideTarget); | ||
const inverseSideRelationMetadataArgsCollection = | ||
metadataArgsStorage.filterRelations(inverseSideTarget); | ||
const inverseSideRelationMetadataArgs = | ||
inverseSideRelationMetadataArgsCollection.find( | ||
(inverseSideRelationMetadataArgs) => | ||
inverseSideRelationMetadataArgs.inverseSideFieldKey === | ||
relationMetadataArgs.name, | ||
); | ||
|
||
if (!inverseSideRelationMetadataArgs) { | ||
throw new Error( | ||
`Inverse side relation metadata args are missing for relation ${relationMetadataArgs.name}`, | ||
); | ||
} | ||
|
||
return getJoinColumn( | ||
inverseSideJoinColumnsMetadataArgsCollection, | ||
inverseSideRelationMetadataArgs, | ||
); | ||
} | ||
|
||
// Check if there are multiple join columns for the relation | ||
if (filteredJoinColumnsMetadataArgsCollection.length > 1) { | ||
throw new Error( | ||
`Multiple join columns metadata args found for relation ${relationMetadataArgs.name}`, | ||
); | ||
} | ||
|
||
const joinColumnsMetadataArgs = filteredJoinColumnsMetadataArgsCollection[0]; | ||
|
||
if (!joinColumnsMetadataArgs) { | ||
throw new Error( | ||
`Join columns metadata args are missing for relation ${relationMetadataArgs.name}`, | ||
); | ||
} | ||
|
||
return joinColumnsMetadataArgs.joinColumn; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters