Files
twenty/packages/twenty-server/src/modules/attachment/standard-objects/attachment.workspace-entity.ts
Jérémy M 8b5f79ddbf fix: multiple twenty orm issues & show an example of use (#5439)
This PR is fixing some issues and adding enhancement in TwentyORM:

- [x] Composite fields in nested relations are not formatted properly
- [x] Passing operators like `Any` in `where` condition is breaking the
query
- [x] Ability to auto load workspace-entities based on a regex path

I've also introduced an example of use for `CalendarEventService`:


https://github.com/twentyhq/twenty/pull/5439/files#diff-3a7dffc0dea57345d10e70c648e911f98fe237248bcea124dafa9c8deb1db748R15
2024-05-20 11:01:47 +02:00

139 lines
5.5 KiB
TypeScript

import { Relation } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/relation.interface';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { ATTACHMENT_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { CustomWorkspaceEntity } from 'src/engine/twenty-orm/custom.workspace-entity';
import { ActivityWorkspaceEntity } from 'src/modules/activity/standard-objects/activity.workspace-entity';
import { CompanyWorkspaceEntity } from 'src/modules/company/standard-objects/company.workspace-entity';
import { OpportunityWorkspaceEntity } from 'src/modules/opportunity/standard-objects/opportunity.workspace-entity';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator';
import { WorkspaceIsSystem } from 'src/engine/twenty-orm/decorators/workspace-is-system.decorator';
import { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/workspace-is-not-audit-logged.decorator';
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
import { WorkspaceDynamicRelation } from 'src/engine/twenty-orm/decorators/workspace-dynamic-relation.decorator';
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.attachment,
namePlural: 'attachments',
labelSingular: 'Attachment',
labelPlural: 'Attachments',
description: 'An attachment',
icon: 'IconFileImport',
})
@WorkspaceIsSystem()
@WorkspaceIsNotAuditLogged()
export class AttachmentWorkspaceEntity extends BaseWorkspaceEntity {
@WorkspaceField({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.name,
type: FieldMetadataType.TEXT,
label: 'Name',
description: 'Attachment name',
icon: 'IconFileUpload',
})
name: string;
@WorkspaceField({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.fullPath,
type: FieldMetadataType.TEXT,
label: 'Full path',
description: 'Attachment full path',
icon: 'IconLink',
})
fullPath: string;
@WorkspaceField({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.type,
type: FieldMetadataType.TEXT,
label: 'Type',
description: 'Attachment type',
icon: 'IconList',
})
type: string;
@WorkspaceRelation({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.author,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Author',
description: 'Attachment author',
icon: 'IconCircleUser',
joinColumn: 'authorId',
inverseSideTarget: () => WorkspaceMemberWorkspaceEntity,
inverseSideFieldKey: 'authoredAttachments',
})
author: Relation<WorkspaceMemberWorkspaceEntity>;
@WorkspaceRelation({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.activity,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Activity',
description: 'Attachment activity',
icon: 'IconNotes',
joinColumn: 'activityId',
inverseSideTarget: () => ActivityWorkspaceEntity,
inverseSideFieldKey: 'attachments',
})
@WorkspaceIsNullable()
activity: Relation<ActivityWorkspaceEntity>;
@WorkspaceRelation({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.person,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Person',
description: 'Attachment person',
icon: 'IconUser',
joinColumn: 'personId',
inverseSideTarget: () => PersonWorkspaceEntity,
inverseSideFieldKey: 'attachments',
})
@WorkspaceIsNullable()
person: Relation<PersonWorkspaceEntity>;
@WorkspaceRelation({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.company,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Company',
description: 'Attachment company',
icon: 'IconBuildingSkyscraper',
joinColumn: 'companyId',
inverseSideTarget: () => CompanyWorkspaceEntity,
inverseSideFieldKey: 'attachments',
})
@WorkspaceIsNullable()
company: Relation<CompanyWorkspaceEntity>;
@WorkspaceRelation({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.opportunity,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Opportunity',
description: 'Attachment opportunity',
icon: 'IconBuildingSkyscraper',
joinColumn: 'opportunityId',
inverseSideTarget: () => OpportunityWorkspaceEntity,
inverseSideFieldKey: 'attachments',
})
@WorkspaceIsNullable()
opportunity: Relation<OpportunityWorkspaceEntity>;
@WorkspaceDynamicRelation({
type: RelationMetadataType.MANY_TO_ONE,
argsFactory: (oppositeObjectMetadata) => ({
standardId: ATTACHMENT_STANDARD_FIELD_IDS.custom,
name: oppositeObjectMetadata.nameSingular,
label: oppositeObjectMetadata.labelSingular,
description: `Attachment ${oppositeObjectMetadata.labelSingular}`,
joinColumn: `${oppositeObjectMetadata.nameSingular}Id`,
icon: 'IconBuildingSkyscraper',
}),
inverseSideTarget: () => CustomWorkspaceEntity,
inverseSideFieldKey: 'attachments',
})
custom: Relation<CustomWorkspaceEntity>;
}