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
@@ -1,12 +0,0 @@
import { type GraphQLResolveInfo } from 'graphql';
import { type AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { type ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
import { type ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
export interface WorkspaceQueryRunnerOptions {
authContext: AuthContext;
objectMetadataMaps: ObjectMetadataMaps;
objectMetadataItemWithFieldMaps: ObjectMetadataItemWithFieldMaps;
info: GraphQLResolveInfo;
}
@@ -1,9 +1,7 @@
import { msg } from '@lingui/core/macro';
import { isDefined } from 'twenty-shared/utils';
import { type QueryFailedError } from 'typeorm';
import { UserInputError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import { type ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
import {
TwentyORMException,
TwentyORMExceptionCode,
@@ -14,64 +12,16 @@ interface PostgreSQLError extends QueryFailedError {
}
export const handleDuplicateKeyError = (
error: PostgreSQLError,
objectMetadata: ObjectMetadataItemWithFieldMaps,
_error: PostgreSQLError,
_objectMetadata: FlatObjectMetadata,
) => {
const indexNameMatch = error.message.match(/"([^"]+)"/);
const duplicatedValues = error?.detail?.match(/=\(([^)]+)\)/)?.[1];
if (indexNameMatch) {
const indexName = indexNameMatch[1];
const deletedAtFieldMetadataId = objectMetadata.fieldIdByName['deletedAt'];
const affectedColumns = objectMetadata.indexMetadatas
.find((index) => index.name === indexName)
?.indexFieldMetadatas?.filter(
(field) => field.fieldMetadataId !== deletedAtFieldMetadataId,
)
.map((indexField) => {
const fieldMetadata =
objectMetadata.fieldsById[indexField.fieldMetadataId];
return fieldMetadata?.label;
});
if (!isDefined(affectedColumns)) {
throw new TwentyORMException(
`A duplicate entry was detected`,
TwentyORMExceptionCode.DUPLICATE_ENTRY_DETECTED,
{
userFriendlyMessage: msg`This record already exists. Please check your data and try again.`,
},
);
}
const columnNames = affectedColumns.join(', ');
if (affectedColumns?.length === 1) {
const fieldName = columnNames.toLowerCase();
throw new UserInputError(
`Duplicate ${columnNames} ${duplicatedValues ? `with value ${duplicatedValues}` : ''}. Please set a unique one.`,
{
userFriendlyMessage: duplicatedValues
? msg`This ${fieldName} with value ${duplicatedValues} is already taken. Please choose a different value.`
: msg`This ${fieldName} is already taken. Please choose a different value.`,
isExpected: true,
},
);
}
const fieldNames = columnNames.toLowerCase();
throw new TwentyORMException(
`A duplicate entry was detected. The combination of ${columnNames} must be unique.`,
TwentyORMExceptionCode.DUPLICATE_ENTRY_DETECTED,
{
userFriendlyMessage: msg`This combination of ${fieldNames} already exists. Please use different values.`,
},
);
}
// Since we no longer have indexMetadatas in FlatObjectMetadata,
// we provide a generic error message
throw new TwentyORMException(
`A duplicate entry was detected`,
TwentyORMExceptionCode.DUPLICATE_ENTRY_DETECTED,
{
userFriendlyMessage: msg`This record already exists. Please check your data and try again.`,
},
);
};