-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: manually implement joinColumn #6022
Changes from 8 commits
96eaf99
5e42fe7
ef86814
a904261
5b127ca
21821d5
acd15a9
6767456
be77e4a
df8a4ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { metadataArgsStorage } from 'src/engine/twenty-orm/storage/metadata-args.storage'; | ||
|
||
export function WorkspaceJoinColumn( | ||
relationPropertyKey: string, | ||
): PropertyDecorator { | ||
return (object, propertyKey) => { | ||
metadataArgsStorage.addJoinColumns({ | ||
target: object.constructor, | ||
relationName: relationPropertyKey, | ||
joinColumn: propertyKey.toString(), | ||
}); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface WorkspaceJoinColumnsMetadataArgs { | ||
/** | ||
* Class to which relation is applied. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
readonly target: Function; | ||
|
||
/** | ||
* Relation name. | ||
*/ | ||
readonly relationName: string; | ||
|
||
/** | ||
* Relation label. | ||
*/ | ||
readonly joinColumn: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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, | ||
opposite = false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🪶 style: Consider renaming |
||
): string | null => { | ||
if ( | ||
relationMetadataArgs.type === RelationMetadataType.ONE_TO_MANY || | ||
relationMetadataArgs.type === RelationMetadataType.MANY_TO_MANY | ||
) { | ||
return null; | ||
} | ||
|
||
const inverseSideTarget = relationMetadataArgs.inverseSideTarget(); | ||
const inverseSideJoinColumnsMetadataArgsCollection = | ||
metadataArgsStorage.filterJoinColumns(inverseSideTarget); | ||
const filteredJoinColumnsMetadataArgsCollection = | ||
joinColumnsMetadataArgsCollection.filter( | ||
(joinColumnsMetadataArgs) => | ||
joinColumnsMetadataArgs.relationName === relationMetadataArgs.name, | ||
); | ||
const oppositeFilteredJoinColumnsMetadataArgsCollection = | ||
inverseSideJoinColumnsMetadataArgsCollection.filter( | ||
(joinColumnsMetadataArgs) => | ||
joinColumnsMetadataArgs.relationName === relationMetadataArgs.name, | ||
); | ||
|
||
if ( | ||
filteredJoinColumnsMetadataArgsCollection.length > 0 && | ||
oppositeFilteredJoinColumnsMetadataArgsCollection.length > 0 | ||
) { | ||
throw new Error( | ||
`Join column for ${relationMetadataArgs.name} relation is present on both sides`, | ||
); | ||
Comment on lines
+34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧠 logic: Avoid potential infinite recursion by adding a maximum recursion depth check. |
||
} | ||
|
||
// 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 && | ||
!opposite | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧠 logic: Avoid potential infinite recursion by adding a maximum recursion depth check. |
||
) { | ||
const inverseSideRelationMetadataArgsCollection = | ||
metadataArgsStorage.filterRelations(inverseSideTarget); | ||
const inverseSideRelationMetadataArgs = | ||
inverseSideRelationMetadataArgsCollection.find( | ||
(inverseSideRelationMetadataArgs) => | ||
inverseSideRelationMetadataArgs.inverseSideFieldKey === | ||
relationMetadataArgs.name, | ||
); | ||
|
||
if (!inverseSideRelationMetadataArgs) { | ||
throw new Error( | ||
`Inverse side join column of relation ${relationMetadataArgs.name} is missing`, | ||
); | ||
} | ||
|
||
return getJoinColumn( | ||
inverseSideJoinColumnsMetadataArgsCollection, | ||
inverseSideRelationMetadataArgs, | ||
// Avoid infinite recursion | ||
true, | ||
); | ||
} | ||
|
||
// Check if there are multiple join columns for the relation | ||
if (filteredJoinColumnsMetadataArgsCollection.length > 1) { | ||
throw new Error( | ||
`Multiple join columns found for relation ${relationMetadataArgs.name}`, | ||
); | ||
} | ||
|
||
const joinColumnsMetadataArgs = filteredJoinColumnsMetadataArgsCollection[0]; | ||
|
||
if (!joinColumnsMetadataArgs) { | ||
throw new Error( | ||
`Join column is missing for relation ${relationMetadataArgs.name}`, | ||
); | ||
} | ||
|
||
return joinColumnsMetadataArgs.joinColumn; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🪶 style: Consider renaming
opposite
to something more descriptive likeisInverseSide
.