feat: twenty orm for standard and custom objects (#6178)

### Overview

This PR builds upon #5153, adding the ability to get a repository for
custom objects. The `entitySchema` is now generated for both standard
and custom objects based on metadata stored in the database instead of
the decorated `WorkspaceEntity` in the code. This change ensures that
standard objects with custom fields and relations can also support
custom objects.

### Implementation Details

#### Key Changes:

- **Dynamic Schema Generation:** The `entitySchema` for standard and
custom objects is now dynamically generated from the metadata stored in
the database. This shift allows for greater flexibility and
adaptability, particularly for standard objects with custom fields and
relations.
  
- **Custom Object Repository Retrieval:** A repository for a custom
object can be retrieved using `TwentyORMManager` based on the object's
name. Here's an example of how this can be achieved:

  ```typescript
const repository = await this.twentyORMManager.getRepository('custom');
  /*
* `repository` variable will be typed as follows, ensuring that standard
fields and relations are properly typed:
   * const repository: WorkspaceRepository<CustomWorkspaceEntity & {
   *    [key: string]: any;
   * }>
   */
  const res = await repository.find({});
  ```

Fix #6179

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
Jérémy M
2024-07-19 18:23:52 +02:00
committed by GitHub
parent a86cc5cb9c
commit 088d061b3e
33 changed files with 947 additions and 587 deletions

View File

@ -0,0 +1,33 @@
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import {
RelationMetadataEntity,
RelationMetadataType,
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import {
deduceRelationDirection,
RelationDirection,
} from 'src/engine/utils/deduce-relation-direction.util';
export const computeRelationType = (
fieldMetadata: FieldMetadataEntity,
relationMetadata: RelationMetadataEntity,
) => {
const relationDirection = deduceRelationDirection(
fieldMetadata,
relationMetadata,
);
switch (relationMetadata.relationType) {
case RelationMetadataType.ONE_TO_MANY: {
return relationDirection === RelationDirection.FROM
? 'one-to-many'
: 'many-to-one';
}
case RelationMetadataType.ONE_TO_ONE:
return 'one-to-one';
case RelationMetadataType.MANY_TO_MANY:
return 'many-to-many';
default:
throw new Error('Invalid relation type');
}
};

View File

@ -0,0 +1,59 @@
import { RelationType } from 'typeorm/metadata/types/RelationTypes';
import { RelationMetadataEntity } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { computeRelationType } from 'src/engine/twenty-orm/utils/compute-relation-type.util';
interface RelationDetails {
relationType: RelationType;
target: string;
inverseSide: string;
joinColumn: { name: string } | undefined;
}
export async function determineRelationDetails(
workspaceId: string,
fieldMetadata: FieldMetadataEntity,
relationMetadata: RelationMetadataEntity,
workspaceCacheStorageService: WorkspaceCacheStorageService,
): Promise<RelationDetails> {
const relationType = computeRelationType(fieldMetadata, relationMetadata);
let fromObjectMetadata: ObjectMetadataEntity | undefined =
fieldMetadata.object;
let toObjectMetadata: ObjectMetadataEntity | undefined =
relationMetadata.toObjectMetadata;
// RelationMetadata always store the relation from the perspective of the `from` object, MANY_TO_ONE relations are not stored yet
if (relationType === 'many-to-one') {
fromObjectMetadata = fieldMetadata.object;
toObjectMetadata = await workspaceCacheStorageService.getObjectMetadata(
workspaceId,
(objectMetadata) =>
objectMetadata.id === relationMetadata.toObjectMetadataId,
);
}
if (!fromObjectMetadata || !toObjectMetadata) {
throw new Error('Object metadata not found');
}
// TODO: Support many to many relations
if (relationType === 'many-to-many') {
throw new Error('Many to many relations are not supported yet');
}
return {
relationType,
target: toObjectMetadata.nameSingular,
inverseSide: fromObjectMetadata.nameSingular,
joinColumn:
// TODO: This will work for now but we need to handle this better in the future for custom names on the join column
relationType === 'many-to-one' ||
(relationType === 'one-to-one' &&
relationMetadata.toObjectMetadataId === fieldMetadata.objectMetadataId)
? { name: `${fieldMetadata.name}` + 'Id' }
: undefined,
};
}