Deprecate object metadata maps in favor of flat entities (#16080)

## Context
Deprecating the old objectMetadataMap type in favour of split flat
entities to match with our new caching.
In the long run, trying to achieve:
- Better performance through caching
- Consistent data access patterns across the codebase
- Reduced database queries

Now that everything is based on flat entities, which are cached, we can
finish the refactoring of workspace context cache which should already
improve performances.
Then the last step will be to consume that new cache in the new global
datasource to get rid of the many workspace datasources stored in the
server
This commit is contained in:
Weiko
2025-11-27 13:43:34 +01:00
committed by GitHub
parent 978c0acb90
commit 1607aebcc6
281 changed files with 6510 additions and 6627 deletions
@@ -599,13 +599,8 @@ export class ObjectMetadataServiceV2 extends TypeOrmQueryService<ObjectMetadataE
public async findManyWithinWorkspace(
workspaceId: string,
options?: FindManyOptions<ObjectMetadataEntity>,
) {
return this.objectMetadataRepository.find({
relations: [
'fields.object',
'fields',
'fields.relationTargetObjectMetadata',
],
): Promise<FlatObjectMetadata[]> {
const objectMetadataEntities = await this.objectMetadataRepository.find({
...options,
where: {
...options?.where,
@@ -614,6 +609,27 @@ export class ObjectMetadataServiceV2 extends TypeOrmQueryService<ObjectMetadataE
order: {
...options?.order,
},
select: { id: true },
});
const objectMetadataIds = objectMetadataEntities.map(
(objectMetadata) => objectMetadata.id,
);
const { flatObjectMetadataMaps } =
await this.flatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
{
workspaceId,
flatMapsKeys: ['flatObjectMetadataMaps'],
},
);
const filteredOutAndSortedFlatObjectMetadataMaps =
findManyFlatEntityByIdInFlatEntityMapsOrThrow({
flatEntityMaps: flatObjectMetadataMaps,
flatEntityIds: objectMetadataIds,
});
return filteredOutAndSortedFlatObjectMetadataMaps;
}
}
@@ -1,65 +0,0 @@
import { RelationOnDeleteAction } from 'twenty-shared/types';
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
import { type ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
import {
WorkspaceMigrationColumnActionType,
type WorkspaceMigrationColumnCreate,
type WorkspaceMigrationTableAction,
WorkspaceMigrationTableActionType,
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
export const buildMigrationsForCustomObjectRelations = (
createdObjectMetadata: Pick<
ObjectMetadataItemWithFieldMaps,
'nameSingular' | 'isCustom'
>,
relatedObjectMetadataCollection: Pick<
ObjectMetadataItemWithFieldMaps,
'nameSingular' | 'isCustom'
>[],
): WorkspaceMigrationTableAction[] => {
const migrations: WorkspaceMigrationTableAction[] = [];
for (const relatedObjectMetadata of relatedObjectMetadataCollection) {
migrations.push(
{
name: computeObjectTargetTable(relatedObjectMetadata),
action: WorkspaceMigrationTableActionType.ALTER,
columns: [
{
action: WorkspaceMigrationColumnActionType.CREATE,
// TODO: When we get rid of this and use the sync metadata, columnName must be based on the joinColumnName from the field metadata settings
columnName: computeColumnName(createdObjectMetadata.nameSingular, {
isForeignKey: true,
}),
columnType: 'uuid',
isNullable: true,
defaultValue: null,
} satisfies WorkspaceMigrationColumnCreate,
],
},
{
name: computeObjectTargetTable(relatedObjectMetadata),
action: WorkspaceMigrationTableActionType.ALTER,
columns: [
{
action: WorkspaceMigrationColumnActionType.CREATE_FOREIGN_KEY,
// TODO: When we get rid of this and use the sync metadata, columnName must be based on the joinColumnName from the field metadata settings
columnName: computeColumnName(createdObjectMetadata.nameSingular, {
isForeignKey: true,
}),
referencedTableName: computeObjectTargetTable(
createdObjectMetadata,
),
referencedTableColumnName: 'id',
onDelete: RelationOnDeleteAction.CASCADE,
},
],
},
);
}
return migrations;
};