feat: manually implement joinColumn (#6022)
This PR introduce a new decorator named `@WorkspaceJoinColumn`, the goal
of this one is to manually declare the join columns inside the workspace
entities, so we don't have to rely on `ObjectRecord` type.
This decorator can be used that way:
```typescript
@WorkspaceRelation({
standardId: ACTIVITY_TARGET_STANDARD_FIELD_IDS.company,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Company',
description: 'ActivityTarget company',
icon: 'IconBuildingSkyscraper',
inverseSideTarget: () => CompanyWorkspaceEntity,
inverseSideFieldKey: 'activityTargets',
})
@WorkspaceIsNullable()
company: Relation<CompanyWorkspaceEntity> | null;
// The argument is the name of the relation above
@WorkspaceJoinColumn('company')
companyId: string | null;
```
This commit is contained in:
@ -16,6 +16,7 @@ import { metadataArgsStorage } from 'src/engine/twenty-orm/storage/metadata-args
|
||||
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
|
||||
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { createDeterministicUuid } from 'src/engine/workspace-manager/workspace-sync-metadata/utils/create-deterministic-uuid.util';
|
||||
import { getJoinColumn } from 'src/engine/twenty-orm/utils/get-join-column.util';
|
||||
|
||||
@Injectable()
|
||||
export class StandardFieldFactory {
|
||||
@ -139,6 +140,14 @@ export class StandardFieldFactory {
|
||||
const foreignKeyStandardId = createDeterministicUuid(
|
||||
workspaceRelationMetadataArgs.standardId,
|
||||
);
|
||||
const joinColumnMetadataArgsCollection =
|
||||
metadataArgsStorage.filterJoinColumns(
|
||||
workspaceRelationMetadataArgs.target,
|
||||
);
|
||||
const joinColumn = getJoinColumn(
|
||||
joinColumnMetadataArgsCollection,
|
||||
workspaceRelationMetadataArgs,
|
||||
);
|
||||
|
||||
if (
|
||||
isGatedAndNotEnabled(
|
||||
@ -149,11 +158,11 @@ export class StandardFieldFactory {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (workspaceRelationMetadataArgs.joinColumn) {
|
||||
if (joinColumn) {
|
||||
fieldMetadataCollection.push({
|
||||
type: FieldMetadataType.UUID,
|
||||
standardId: foreignKeyStandardId,
|
||||
name: workspaceRelationMetadataArgs.joinColumn,
|
||||
name: joinColumn,
|
||||
label: `${workspaceRelationMetadataArgs.label} id (foreign key)`,
|
||||
description: `${workspaceRelationMetadataArgs.description} id foreign key`,
|
||||
icon: workspaceRelationMetadataArgs.icon,
|
||||
|
||||
@ -1,20 +0,0 @@
|
||||
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
|
||||
|
||||
type RelationKeys<T> = {
|
||||
[K in keyof T]: NonNullable<T[K]> extends BaseWorkspaceEntity ? K : never;
|
||||
}[keyof T];
|
||||
|
||||
type ForeignKeyMap<T> = {
|
||||
[K in RelationKeys<T> as `${K & string}Id`]: string;
|
||||
};
|
||||
|
||||
type RecursiveObjectRecord<T> = {
|
||||
[P in keyof T]: NonNullable<T[P]> extends BaseWorkspaceEntity
|
||||
? ObjectRecord<NonNullable<T[P]>> & ForeignKeyMap<NonNullable<T[P]>>
|
||||
: T[P];
|
||||
};
|
||||
|
||||
// TODO: We should get rid of that it's causing too much issues
|
||||
// Some relations can be null or undefined because they're not mendatory and other cannot
|
||||
// This utility type put as defined all the joinColumn, so it's not well typed
|
||||
export type ObjectRecord<T> = RecursiveObjectRecord<T> & ForeignKeyMap<T>;
|
||||
Reference in New Issue
Block a user