Introduce UniversalFlatEntityFrom (#17367)
# Introduction
Creating a `UniversalFlatEntityFrom` that strips out all the relation
and foreignKey properties in order to replace them with
`UniversalIdentifier` suffix
This data type will be major for the workspace migration workspace
agnostic refactor
## Chore
- renamed `flat-entity.type` to `flat-entity-from.type.ts` ( more
accurate to exported module )
- create static test type over the field metadata entity on quite
complex utils as both coverage and documentation
## Example
Here's an example of a `UniversalFlatEntityFrom<FieldMetadataEntity>`
```ts
const universalFlatFieldMetadata: UniversalFlatFieldMetadata<FieldMetadataType.RELATION> = {
// Base properties (from FieldMetadataEntity, excluding relations and applicationId)
universalIdentifier: '550e8400-e29b-41d4-a716-446655440001',
applicationUniversalIdentifier: '5800681c-088e-4e2b-9fc3-bcf6e8ec2051',
type: FieldMetadataType.RELATION,
name: 'firstName',
label: 'First Name',
defaultValue: null,
description: 'The first name of the person',
icon: 'IconUser',
standardOverrides: null,
options: null,
settings: {
relationType: RelationType.ONE_TO_MANY,
},
isCustom: false,
isActive: true,
isSystem: false,
isUIReadOnly: false,
isNullable: true,
isUnique: false,
isLabelSyncedWithName: true,
morphId: null,
// Date properties cast to string
createdAt: '2024-01-15T10:30:00.000Z',
updatedAt: '2024-01-15T10:30:00.000Z',
// ManyToOne relation universal identifiers (from FieldMetadataEntity relations)
relationTargetFieldMetadataUniversalIdentifier:
'550e8400-e29b-41d4-a716-446655440012',
relationTargetObjectMetadataUniversalIdentifier:
'550e8400-e29b-41d4-a716-446655440013',
// Join column universal identifiers (foreignKey -> universalIdentifier)
objectMetadataUniversalIdentifier: '550e8400-e29b-41d4-a716-446655440010',
// OneToMany relation universal identifiers (array of related entity identifiers)
viewFieldUniversalIdentifiers: [
'550e8400-e29b-41d4-a716-446655440020',
'550e8400-e29b-41d4-a716-446655440021',
],
viewFilterUniversalIdentifiers: ['550e8400-e29b-41d4-a716-446655440030'],
kanbanAggregateOperationViewUniversalIdentifiers: [],
calendarViewUniversalIdentifiers: [],
mainGroupByFieldMetadataViewUniversalIdentifiers: [],
};
```
## Settings
Will hop on the settings typing next. Might not be dynamic but
declarative though
This commit is contained in:
+81
@@ -0,0 +1,81 @@
|
||||
import { type Expect, type HasAllProperties } from 'twenty-shared/testing';
|
||||
import {
|
||||
type FieldMetadataType,
|
||||
type NullablePartial,
|
||||
} from 'twenty-shared/types';
|
||||
|
||||
import { type UniversalFlatFieldMetadata } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-field-metadata.type';
|
||||
|
||||
// Relation field types have defined relation universal identifiers
|
||||
type DefinedRelationUniversalIdentifierRecord = {
|
||||
relationTargetFieldMetadataUniversalIdentifier: string;
|
||||
relationTargetObjectMetadataUniversalIdentifier: string;
|
||||
};
|
||||
|
||||
// Non-relation field types have never | null relation universal identifiers
|
||||
type NotDefinedRelationUniversalIdentifierRecord = {
|
||||
relationTargetFieldMetadataUniversalIdentifier: never | null;
|
||||
relationTargetObjectMetadataUniversalIdentifier: never | null;
|
||||
};
|
||||
|
||||
// Date properties are cast to strings
|
||||
type DatePropertiesCastToString = {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
// OneToMany relations become ...UniversalIdentifiers arrays
|
||||
type OneToManyUniversalIdentifierArrays = {
|
||||
viewFieldUniversalIdentifiers: string[];
|
||||
viewFilterUniversalIdentifiers: string[];
|
||||
kanbanAggregateOperationViewUniversalIdentifiers: string[];
|
||||
calendarViewUniversalIdentifiers: string[];
|
||||
mainGroupByFieldMetadataViewUniversalIdentifiers: string[];
|
||||
};
|
||||
|
||||
// Narrowed relation universal identifier assertions - verifies conditional typing is preserved
|
||||
// eslint-disable-next-line unused-imports/no-unused-vars
|
||||
type RelationUniversalIdentifierAssertions = [
|
||||
// Non-relation types have undefined relation universal identifiers
|
||||
Expect<
|
||||
HasAllProperties<
|
||||
UniversalFlatFieldMetadata<FieldMetadataType.UUID>,
|
||||
NotDefinedRelationUniversalIdentifierRecord
|
||||
>
|
||||
>,
|
||||
|
||||
// Relation types have defined relation universal identifiers
|
||||
Expect<
|
||||
HasAllProperties<
|
||||
UniversalFlatFieldMetadata<FieldMetadataType.RELATION>,
|
||||
DefinedRelationUniversalIdentifierRecord
|
||||
>
|
||||
>,
|
||||
Expect<
|
||||
HasAllProperties<
|
||||
UniversalFlatFieldMetadata<FieldMetadataType.MORPH_RELATION>,
|
||||
DefinedRelationUniversalIdentifierRecord
|
||||
>
|
||||
>,
|
||||
|
||||
// Abstract type has nullable partial relation universal identifiers
|
||||
Expect<
|
||||
HasAllProperties<
|
||||
UniversalFlatFieldMetadata,
|
||||
NullablePartial<DefinedRelationUniversalIdentifierRecord>
|
||||
>
|
||||
>,
|
||||
];
|
||||
|
||||
// eslint-disable-next-line unused-imports/no-unused-vars
|
||||
type UniversalFlatTransformationAssertions = [
|
||||
Expect<
|
||||
HasAllProperties<UniversalFlatFieldMetadata, DatePropertiesCastToString>
|
||||
>,
|
||||
Expect<
|
||||
HasAllProperties<
|
||||
UniversalFlatFieldMetadata,
|
||||
OneToManyUniversalIdentifierArrays
|
||||
>
|
||||
>,
|
||||
];
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
import { type AllMetadataName } from 'twenty-shared/metadata';
|
||||
|
||||
import { type CastRecordTypeOrmDatePropertiesToString } from 'src/engine/metadata-modules/flat-entity/types/cast-record-typeorm-date-properties-to-string.type';
|
||||
import { type ExtractEntityOneToManyEntityRelationProperties } from 'src/engine/metadata-modules/flat-entity/types/extract-entity-one-to-many-entity-relation-properties.type';
|
||||
import { type ExtractEntityRelatedEntityProperties } from 'src/engine/metadata-modules/flat-entity/types/extract-entity-related-entity-properties.type';
|
||||
import { type FromMetadataEntityToMetadataName } from 'src/engine/metadata-modules/flat-entity/types/from-metadata-entity-to-metadata-name.type';
|
||||
import { type MetadataManyToOneJoinColumn } from 'src/engine/metadata-modules/flat-entity/types/metadata-many-to-one-join-column.type';
|
||||
import { type SyncableEntity } from 'src/engine/workspace-manager/types/syncable-entity.interface';
|
||||
import { type RemoveSuffix } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/types/remove-suffix.type';
|
||||
|
||||
// TODO Handle universal settings
|
||||
export type UniversalFlatEntityFrom<
|
||||
TEntity extends SyncableEntity,
|
||||
TMetadataName extends
|
||||
AllMetadataName = FromMetadataEntityToMetadataName<TEntity>,
|
||||
> = Omit<
|
||||
TEntity,
|
||||
| 'applicationId'
|
||||
| 'workspaceId'
|
||||
| 'standardId'
|
||||
| 'id'
|
||||
| ExtractEntityRelatedEntityProperties<TEntity>
|
||||
| Extract<MetadataManyToOneJoinColumn<TMetadataName>, keyof TEntity>
|
||||
| keyof CastRecordTypeOrmDatePropertiesToString<TEntity>
|
||||
> &
|
||||
CastRecordTypeOrmDatePropertiesToString<TEntity> & {
|
||||
[P in ExtractEntityOneToManyEntityRelationProperties<
|
||||
TEntity,
|
||||
SyncableEntity
|
||||
> &
|
||||
string as `${RemoveSuffix<P, 's'>}UniversalIdentifiers`]: string[];
|
||||
} & {
|
||||
[P in Extract<MetadataManyToOneJoinColumn<TMetadataName>, keyof TEntity> &
|
||||
string as `${RemoveSuffix<P, 'Id'>}UniversalIdentifier`]: TEntity[P];
|
||||
} & {
|
||||
applicationUniversalIdentifier: string;
|
||||
};
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { type FieldMetadataType } from 'twenty-shared/types';
|
||||
|
||||
import { type FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { type UniversalFlatEntityFrom } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/universal-flat-entity-from.type';
|
||||
|
||||
export type UniversalFlatFieldMetadata<
|
||||
T extends FieldMetadataType = FieldMetadataType,
|
||||
> = UniversalFlatEntityFrom<FieldMetadataEntity<T>, 'fieldMetadata'>;
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { type EachTestingContext } from 'twenty-shared/testing';
|
||||
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity.type';
|
||||
import {
|
||||
type CircularDependencyValidationResult,
|
||||
validateFlatEntityCircularDependency,
|
||||
|
||||
+1
-1
@@ -4,8 +4,8 @@ import {
|
||||
FlatEntityMapsException,
|
||||
FlatEntityMapsExceptionCode,
|
||||
} from 'src/engine/metadata-modules/flat-entity/exceptions/flat-entity-maps.exception';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity.type';
|
||||
|
||||
type AddFlatEntityToFlatEntityMapsThroughMutationOrThrowArgs<
|
||||
T extends SyncableFlatEntity,
|
||||
|
||||
+1
-1
@@ -4,8 +4,8 @@ import {
|
||||
FlatEntityMapsException,
|
||||
FlatEntityMapsExceptionCode,
|
||||
} from 'src/engine/metadata-modules/flat-entity/exceptions/flat-entity-maps.exception';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity.type';
|
||||
|
||||
export type DeleteFlatEntityFromFlatEntityMapsThroughMutationOrThrowArgs<
|
||||
T extends SyncableFlatEntity,
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity.type';
|
||||
import { addFlatEntityToFlatEntityMapsThroughMutationOrThrow } from 'src/engine/workspace-manager/workspace-migration/utils/add-flat-entity-to-flat-entity-maps-through-mutation-or-throw.util';
|
||||
import { deleteFlatEntityFromFlatEntityMapsThroughMutationOrThrow } from 'src/engine/workspace-manager/workspace-migration/utils/delete-flat-entity-from-flat-entity-maps-through-mutation-or-throw.util';
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity.type';
|
||||
|
||||
export type CircularDependencyValidationSuccess = {
|
||||
status: 'success';
|
||||
|
||||
Reference in New Issue
Block a user