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:
+110
-43
@@ -1,11 +1,16 @@
|
||||
import { type ObjectsPermissions } from 'twenty-shared/types';
|
||||
import {
|
||||
type FieldMetadataType,
|
||||
type ObjectsPermissions,
|
||||
} from 'twenty-shared/types';
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { EntityPersistExecutor } from 'typeorm/persistence/EntityPersistExecutor';
|
||||
import { PlainObjectToDatabaseEntityTransformer } from 'typeorm/query-builder/transformer/PlainObjectToDatabaseEntityTransformer';
|
||||
|
||||
import { type WorkspaceInternalContext } from 'src/engine/twenty-orm/interfaces/workspace-internal-context.interface';
|
||||
|
||||
import { type ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
|
||||
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
|
||||
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
|
||||
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
|
||||
import { type WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||
import { validateOperationIsPermittedOrThrow } from 'src/engine/twenty-orm/repository/permissions.utils';
|
||||
|
||||
@@ -79,47 +84,105 @@ describe('WorkspaceEntityManager', () => {
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
const mockFlatObjectMetadata: FlatObjectMetadata = {
|
||||
id: 'test-entity-id',
|
||||
nameSingular: 'test-entity',
|
||||
namePlural: 'test-entities',
|
||||
labelSingular: 'Test Entity',
|
||||
labelPlural: 'Test Entities',
|
||||
workspaceId: 'test-workspace-id',
|
||||
icon: 'test-icon',
|
||||
isCustom: false,
|
||||
isRemote: false,
|
||||
isAuditLogged: false,
|
||||
isSearchable: false,
|
||||
isSystem: false,
|
||||
isActive: true,
|
||||
targetTableName: 'test_entity',
|
||||
fieldMetadataIds: ['field-id'],
|
||||
indexMetadataIds: [],
|
||||
viewIds: [],
|
||||
universalIdentifier: 'test-entity-id',
|
||||
description: null,
|
||||
imageIdentifierFieldMetadataId: null,
|
||||
labelIdentifierFieldMetadataId: null,
|
||||
shortcut: null,
|
||||
standardId: null,
|
||||
standardOverrides: null,
|
||||
applicationId: null,
|
||||
isLabelSyncedWithName: false,
|
||||
isUIReadOnly: false,
|
||||
duplicateCriteria: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
const mockFlatFieldMetadata: FlatFieldMetadata = {
|
||||
id: 'field-id',
|
||||
type: 'TEXT' as FieldMetadataType,
|
||||
name: 'fieldName',
|
||||
label: 'Field Name',
|
||||
objectMetadataId: 'test-entity-id',
|
||||
isNullable: true,
|
||||
isLabelSyncedWithName: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
universalIdentifier: 'field-id',
|
||||
defaultValue: null,
|
||||
description: null,
|
||||
icon: null,
|
||||
isActive: true,
|
||||
isCustom: false,
|
||||
isSystem: false,
|
||||
isUIReadOnly: false,
|
||||
isUnique: false,
|
||||
options: null,
|
||||
settings: null,
|
||||
standardId: null,
|
||||
standardOverrides: null,
|
||||
workspaceId: 'test-workspace-id',
|
||||
viewFieldIds: [],
|
||||
viewFilterIds: [],
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
relationTargetFieldMetadataId: null,
|
||||
relationTargetObjectMetadataId: null,
|
||||
morphId: null,
|
||||
applicationId: null,
|
||||
};
|
||||
|
||||
const flatObjectMetadataMaps: FlatEntityMaps<FlatObjectMetadata> = {
|
||||
byId: {
|
||||
'test-entity-id': mockFlatObjectMetadata,
|
||||
},
|
||||
idByUniversalIdentifier: {
|
||||
'test-entity-id': 'test-entity-id',
|
||||
},
|
||||
universalIdentifiersByApplicationId: {},
|
||||
};
|
||||
|
||||
const flatFieldMetadataMaps: FlatEntityMaps<FlatFieldMetadata> = {
|
||||
byId: {
|
||||
'field-id': mockFlatFieldMetadata,
|
||||
},
|
||||
idByUniversalIdentifier: {
|
||||
'field-id': 'field-id',
|
||||
},
|
||||
universalIdentifiersByApplicationId: {},
|
||||
};
|
||||
|
||||
mockInternalContext = {
|
||||
workspaceId: 'test-workspace-id',
|
||||
objectMetadataMaps: {
|
||||
byId: {
|
||||
'test-entity-id': {
|
||||
id: 'test-entity-id',
|
||||
nameSingular: 'test-entity',
|
||||
namePlural: 'test-entities',
|
||||
labelSingular: 'Test Entity',
|
||||
labelPlural: 'Test Entities',
|
||||
workspaceId: 'test-workspace-id',
|
||||
icon: 'test-icon',
|
||||
color: 'test-color',
|
||||
isCustom: false,
|
||||
isRemote: false,
|
||||
isAuditLogged: false,
|
||||
isSearchable: false,
|
||||
isSystem: false,
|
||||
isActive: true,
|
||||
targetTableName: 'test_entity',
|
||||
indexMetadatas: [],
|
||||
fieldsById: {
|
||||
'field-id': {
|
||||
id: 'field-id',
|
||||
type: 'TEXT',
|
||||
name: 'fieldName',
|
||||
label: 'Field Name',
|
||||
objectMetadataId: 'test-entity-id',
|
||||
isNullable: true,
|
||||
isLabelSyncedWithName: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
},
|
||||
fieldIdByName: { fieldName: 'field-id' },
|
||||
fieldIdByJoinColumnName: {},
|
||||
} as unknown as ObjectMetadataItemWithFieldMaps,
|
||||
},
|
||||
idByNameSingular: {
|
||||
'test-entity': 'test-entity-id',
|
||||
},
|
||||
flatObjectMetadataMaps,
|
||||
flatFieldMetadataMaps,
|
||||
flatIndexMaps: {
|
||||
byId: {},
|
||||
idByUniversalIdentifier: {},
|
||||
universalIdentifiersByApplicationId: {},
|
||||
},
|
||||
objectIdByNameSingular: {
|
||||
'test-entity': 'test-entity-id',
|
||||
},
|
||||
featureFlagsMap: {
|
||||
IS_AIRTABLE_INTEGRATION_ENABLED: false,
|
||||
@@ -333,7 +396,9 @@ describe('WorkspaceEntityManager', () => {
|
||||
expect(validateOperationIsPermittedOrThrow).toHaveBeenCalledWith({
|
||||
entityName: 'test-entity',
|
||||
operationType: 'update',
|
||||
objectMetadataMaps: mockInternalContext.objectMetadataMaps,
|
||||
flatObjectMetadataMaps: mockInternalContext.flatObjectMetadataMaps,
|
||||
flatFieldMetadataMaps: mockInternalContext.flatFieldMetadataMaps,
|
||||
objectIdByNameSingular: mockInternalContext.objectIdByNameSingular,
|
||||
objectsPermissions: mockPermissionOptions.objectRecordsPermissions,
|
||||
selectedColumns: [],
|
||||
allFieldsSelected: false,
|
||||
@@ -366,7 +431,9 @@ describe('WorkspaceEntityManager', () => {
|
||||
expect(validateOperationIsPermittedOrThrow).toHaveBeenCalledWith({
|
||||
entityName: 'test-entity',
|
||||
operationType: 'delete',
|
||||
objectMetadataMaps: mockInternalContext.objectMetadataMaps,
|
||||
flatObjectMetadataMaps: mockInternalContext.flatObjectMetadataMaps,
|
||||
flatFieldMetadataMaps: mockInternalContext.flatFieldMetadataMaps,
|
||||
objectIdByNameSingular: mockInternalContext.objectIdByNameSingular,
|
||||
objectsPermissions: mockPermissionOptions.objectRecordsPermissions,
|
||||
selectedColumns: [],
|
||||
allFieldsSelected: false,
|
||||
|
||||
Reference in New Issue
Block a user