The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
refactor(cache): derive role target and resolver name caches from flat entity maps (#27204)
Charles Bochet302384303586
diff --git a/packages/twenty-server/src/engine/api/graphql/direct-execution/services/workspace-resolver-name-map-cache.service.ts b/packages/twenty-server/src/engine/api/graphql/direct-execution/services/workspace-resolver-name-map-cache.service.tsindex 18e48741b4a..1c8a35abb69 100644--- a/packages/twenty-server/src/engine/api/graphql/direct-execution/services/workspace-resolver-name-map-cache.service.ts+++ b/packages/twenty-server/src/engine/api/graphql/direct-execution/services/workspace-resolver-name-map-cache.service.ts@@ -1,31 +1,31 @@import { Injectable } from '@nestjs/common';-import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';+import { isDefined } from 'twenty-shared/utils';++import { WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';import {type ResolverNameMapEntry,buildResolverNameMap,} from 'src/engine/api/graphql/direct-execution/utils/build-resolver-name-map.util';-import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';-import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/types/workspace-cache-provider-context.type';-import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';--const GRAPHQL_RESOLVER_NAME_ROWS_REQUIREMENT = {- objectMetadata: ['universalIdentifier', 'nameSingular', 'namePlural'],-} as const satisfies WorkspaceCacheRowsRequirement;+import { WorkspaceDerivedCache } from 'src/engine/workspace-cache/decorators/workspace-derived-cache.decorator';+import { type WorkspaceCacheDataMap } from 'src/engine/workspace-cache/types/workspace-cache-key.type';@Injectable()-@WorkspaceCache('graphQLResolverNameMap', { packingPonderation: 4 })-export class WorkspaceResolverNameMapCacheService extends WorkspaceCacheProvider<- Record<string, ResolverNameMapEntry>+@WorkspaceDerivedCache('graphQLResolverNameMap')+export class WorkspaceResolverNameMapCacheService extends WorkspaceDerivedCacheProvider<+ 'graphQLResolverNameMap',+ 'flatObjectMetadataMaps'> {- override readonly rowsRequirement = GRAPHQL_RESOLVER_NAME_ROWS_REQUIREMENT;+ readonly sourceKeyName = 'flatObjectMetadataMaps';- computeForCache({- rows,- }: WorkspaceCacheProviderContext<- typeof GRAPHQL_RESOLVER_NAME_ROWS_REQUIREMENT- >): Record<string, ResolverNameMapEntry> {- return buildResolverNameMap(rows.objectMetadata);+ protected computeFromSource(+ flatObjectMetadataMaps: WorkspaceCacheDataMap['flatObjectMetadataMaps'],+ ): Record<string, ResolverNameMapEntry> {+ return buildResolverNameMap(+ Object.values(flatObjectMetadataMaps.byUniversalIdentifier).filter(+ isDefined,+ ),+ );}}diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-cached-metadata.ts b/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-cached-metadata.tsindex 227ff839bb7..f2a2435f35f 100644--- a/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-cached-metadata.ts+++ b/packages/twenty-server/src/engine/api/graphql/graphql-config/hooks/use-cached-metadata.ts@@ -5,10 +5,10 @@ import { type Request } from 'express';import { type Plugin } from 'graphql-yoga';import { isDefined } from 'twenty-shared/utils';-import { type WorkspaceCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';+import { type WorkspaceCacheOrDerivedCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';export type CachedOperationConfig = {- dependencies: WorkspaceCacheKeyName[];+ dependencies: WorkspaceCacheOrDerivedCacheKeyName[];scope: 'workspace' | 'userWorkspace';};@@ -20,7 +20,7 @@ export type CacheMetadataPluginConfig = {operationsToCache: Record<string, CachedOperationConfig>;dependencyHashGetter: (workspaceId: string,- cacheKeyNames: WorkspaceCacheKeyName[],+ cacheKeyNames: WorkspaceCacheOrDerivedCacheKeyName[],) => Promise<string>;};diff --git a/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.module.ts b/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.module.tsindex b1255ce88ac..6ad3faed960 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.module.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.module.ts@@ -5,7 +5,6 @@ import { ApplicationEntity } from 'src/engine/core-modules/application/applicatiimport { ApplicationVariableEntity } from 'src/engine/core-modules/application/application-variable/application-variable.entity';import { ApplicationVariableEntityResolver } from 'src/engine/core-modules/application/application-variable/application-variable.resolver';import { ApplicationVariableEntityService } from 'src/engine/core-modules/application/application-variable/application-variable.service';-import { WorkspaceApplicationVariableMapCacheService } from 'src/engine/core-modules/application/application-variable/workspace-application-variable-map-cache.service';import { SecretEncryptionModule } from 'src/engine/core-modules/secret-encryption/secret-encryption.module';import { FlatApplicationVariableModule } from 'src/engine/metadata-modules/flat-application-variable/flat-application-variable.module';import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permissions.module';@@ -24,11 +23,7 @@ import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cacheprovideWorkspaceScopedRepository(ApplicationVariableEntity),ApplicationVariableEntityService,ApplicationVariableEntityResolver,- WorkspaceApplicationVariableMapCacheService,- ],- exports: [- ApplicationVariableEntityService,- WorkspaceApplicationVariableMapCacheService,],+ exports: [ApplicationVariableEntityService],})export class ApplicationVariableEntityModule {}diff --git a/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.service.tsindex 1ec26a67335..b82d7f3deb5 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-variable/application-variable.service.ts@@ -8,7 +8,7 @@ import {ApplicationVariableEntityExceptionCode,} from 'src/engine/core-modules/application/application-variable/application-variable.exception';import { SECRET_APPLICATION_VARIABLE_MASK } from 'src/engine/core-modules/application/application-variable/constants/secret-application-variable-mask.constant';-import { type ApplicationVariableCacheMaps } from 'src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type';+import { type FlatApplicationVariableMaps } from 'src/engine/metadata-modules/flat-application-variable/types/flat-application-variable-maps.type';import { type PlaintextString } from 'src/engine/core-modules/secret-encryption/branded-strings/plaintext-string.type';import { SecretEncryptionService } from 'src/engine/core-modules/secret-encryption/secret-encryption.service';import { type FlatApplicationVariable } from 'src/engine/metadata-modules/flat-application-variable/types/flat-application-variable.type';@@ -19,7 +19,7 @@ import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workstype GetEnvVariablesArgs = {workspaceId: string;applicationId: string;- applicationVariableMaps?: ApplicationVariableCacheMaps;+ flatApplicationVariableMaps?: FlatApplicationVariableMaps;};@Injectable()@@ -74,25 +74,27 @@ export class ApplicationVariableEntityService {private async findFlatApplicationVariables({workspaceId,applicationId,- applicationVariableMaps: preloadedApplicationVariableMaps,+ flatApplicationVariableMaps: preloadedFlatApplicationVariableMaps,}: GetEnvVariablesArgs): Promise<FlatApplicationVariable[]> {- const applicationVariableMaps =- preloadedApplicationVariableMaps ??+ const flatApplicationVariableMaps =+ preloadedFlatApplicationVariableMaps ??(await this.workspaceCacheService.getOrRecompute(workspaceId, [- 'applicationVariableMaps',+ 'flatApplicationVariableMaps',])- ).applicationVariableMaps;+ ).flatApplicationVariableMaps;const universalIdentifiers =- applicationVariableMaps.universalIdentifiersByApplicationId[+ flatApplicationVariableMaps.universalIdentifiersByApplicationId[applicationId] ?? [];return universalIdentifiers.map((universalIdentifier) =>- applicationVariableMaps.byUniversalIdentifier[universalIdentifier],+ flatApplicationVariableMaps.byUniversalIdentifier[+ universalIdentifier+ ],).filter(isDefined);}@@ -154,7 +156,7 @@ export class ApplicationVariableEntityService {);await this.workspaceCacheService.invalidateAndRecompute(workspaceId, [- 'applicationVariableMaps',+ 'flatApplicationVariableMaps',]);}}diff --git a/packages/twenty-server/src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type.ts b/packages/twenty-server/src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type.tsdeleted file mode 100644index 001f652a706..00000000000--- a/packages/twenty-server/src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type.ts+++ /dev/null@@ -1,5 +0,0 @@-import { type FlatApplicationVariable } from 'src/engine/metadata-modules/flat-application-variable/types/flat-application-variable.type';-import type { FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';--export type ApplicationVariableCacheMaps =- FlatEntityMaps<FlatApplicationVariable>;diff --git a/packages/twenty-server/src/engine/core-modules/application/application-variable/workspace-application-variable-map-cache.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-variable/workspace-application-variable-map-cache.service.tsdeleted file mode 100644index c924e787dbe..00000000000--- a/packages/twenty-server/src/engine/core-modules/application/application-variable/workspace-application-variable-map-cache.service.ts+++ /dev/null@@ -1,58 +0,0 @@-import { Injectable } from '@nestjs/common';--import { isDefined } from 'twenty-shared/utils';--import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';--import { type ApplicationVariableCacheMaps } from 'src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type';-import { fromApplicationVariableEntityToFlatApplicationVariable } from 'src/engine/metadata-modules/flat-application-variable/utils/from-application-variable-entity-to-flat-application-variable.util';-import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';-import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/types/workspace-cache-provider-context.type';-import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';-import { createIdToUniversalIdentifierMap } from 'src/engine/workspace-cache/utils/create-id-to-universal-identifier-map.util';-import { createEmptyFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-flat-entity-maps.constant';-import { addFlatEntityToFlatEntityMapsThroughMutationOrThrow } from 'src/engine/workspace-manager/workspace-migration/utils/add-flat-entity-to-flat-entity-maps-through-mutation-or-throw.util';--const APPLICATION_VARIABLE_ROWS_REQUIREMENT = {- applicationVariable: true,- application: ['id', 'universalIdentifier', 'deletedAt'],-} as const satisfies WorkspaceCacheRowsRequirement;--@Injectable()-@WorkspaceCache('applicationVariableMaps', { packingPonderation: 1 })-export class WorkspaceApplicationVariableMapCacheService extends WorkspaceCacheProvider<ApplicationVariableCacheMaps> {- override readonly rowsRequirement = APPLICATION_VARIABLE_ROWS_REQUIREMENT;-- computeForCache({- rows,- }: WorkspaceCacheProviderContext<- typeof APPLICATION_VARIABLE_ROWS_REQUIREMENT- >): ApplicationVariableCacheMaps {- const {- applicationVariable: applicationVariableEntities,- application: applications,- } = rows;-- const applicationIdToUniversalIdentifierMap =- createIdToUniversalIdentifierMap(- applications.filter((application) => !isDefined(application.deletedAt)),- );-- const applicationVariableMaps = createEmptyFlatEntityMaps();-- for (const entity of applicationVariableEntities) {- const flatApplicationVariable =- fromApplicationVariableEntityToFlatApplicationVariable({- entity,- applicationIdToUniversalIdentifierMap,- });-- addFlatEntityToFlatEntityMapsThroughMutationOrThrow({- flatEntity: flatApplicationVariable,- flatEntityMapsToMutate: applicationVariableMaps,- });- }-- return applicationVariableMaps;- }-}diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.tsindex 819e1f6408e..23be2e295ec 100644--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-executor/logic-function-executor.service.ts@@ -29,7 +29,7 @@ import { isBillingExemptApplication } from 'src/engine/core-modules/application/import { ApplicationRegistrationVariableEntity } from 'src/engine/core-modules/application/application-registration-variable/application-registration-variable.entity';import { ApplicationStopService } from 'src/engine/core-modules/application/application-stop/application-stop.service';import { ApplicationVariableEntityService } from 'src/engine/core-modules/application/application-variable/application-variable.service';-import { type ApplicationVariableCacheMaps } from 'src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type';+import { type FlatApplicationVariableMaps } from 'src/engine/metadata-modules/flat-application-variable/types/flat-application-variable-maps.type';import { ApplicationService } from 'src/engine/core-modules/application/application.service';import { FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';import { ApplicationTokenService } from 'src/engine/core-modules/auth/token/services/application-token.service';@@ -147,7 +147,7 @@ export class LogicFunctionExecutorService {retry?: LogicFunctionRetryContext;shouldEnforceUsageLimits?: boolean;}): Promise<LogicFunctionExecuteResult> {- const { flatApplication, flatLogicFunction, applicationVariableMaps } =+ const { flatApplication, flatLogicFunction, flatApplicationVariableMaps } =await this.getFlatEntitiesOrThrow({workspaceId,logicFunctionId,@@ -169,7 +169,7 @@ export class LogicFunctionExecutorService {const envVariables = await this.getExecutionEnvVariables({workspaceId,flatApplication,- applicationVariableMaps,+ flatApplicationVariableMaps,userId,userWorkspaceId,workspaceDeletionRequestTimestamp,@@ -355,11 +355,11 @@ export class LogicFunctionExecutorService {const {flatLogicFunctionMaps,flatApplicationMaps,- applicationVariableMaps,+ flatApplicationVariableMaps,} = await this.workspaceCacheService.getOrRecompute(workspaceId, ['flatLogicFunctionMaps','flatApplicationMaps',- 'applicationVariableMaps',+ 'flatApplicationVariableMaps',]);const flatLogicFunction = findFlatEntityByIdInFlatEntityMaps({@@ -388,7 +388,7 @@ export class LogicFunctionExecutorService {);}- return { flatApplication, flatLogicFunction, applicationVariableMaps };+ return { flatApplication, flatLogicFunction, flatApplicationVariableMaps };}private async buildExecutionContext({@@ -430,14 +430,14 @@ export class LogicFunctionExecutorService {private async getExecutionEnvVariables({workspaceId,flatApplication,- applicationVariableMaps,+ flatApplicationVariableMaps,userId,userWorkspaceId,workspaceDeletionRequestTimestamp,}: {workspaceId: string;flatApplication: FlatApplication;- applicationVariableMaps: ApplicationVariableCacheMaps;+ flatApplicationVariableMaps: FlatApplicationVariableMaps;userId?: string;userWorkspaceId?: string;workspaceDeletionRequestTimestamp?: string;@@ -481,7 +481,7 @@ export class LogicFunctionExecutorService {await this.applicationVariableService.getServerEnvVariables({workspaceId,applicationId: flatApplication.id,- applicationVariableMaps,+ flatApplicationVariableMaps,});return {diff --git a/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts b/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.tsindex 9d83760ebc6..4344af16d46 100644--- a/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts+++ b/packages/twenty-server/src/engine/core-modules/record-export/services/record-export.workspace-service.ts@@ -68,7 +68,7 @@ import { wrapAsyncIteratorWithLifecycle } from 'src/engine/subscriptions/utils/wimport { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';-import { type WorkspaceCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';+import { type WorkspaceCacheOrDerivedCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';import { combineCacheHashes } from 'src/engine/workspace-cache/utils/combine-cache-hashes.util';export type RecordExportQueryContext = {@@ -77,16 +77,17 @@ export type RecordExportQueryContext = {selectedFields: CommonSelectedFields;};-const RECORD_EXPORT_PERMISSION_CACHE_KEYS: WorkspaceCacheKeyName[] = [- 'rolesPermissions',- 'userWorkspaceRoleMap',- 'flatRoleMaps',- 'flatRowLevelPermissionPredicateMaps',- 'flatRowLevelPermissionPredicateGroupMaps',- 'flatWorkspaceMemberMaps',- 'flatObjectMetadataMaps',- 'flatFieldMetadataMaps',-];+const RECORD_EXPORT_PERMISSION_CACHE_KEYS: WorkspaceCacheOrDerivedCacheKeyName[] =+ [+ 'rolesPermissions',+ 'userWorkspaceRoleMap',+ 'flatRoleMaps',+ 'flatRowLevelPermissionPredicateMaps',+ 'flatRowLevelPermissionPredicateGroupMaps',+ 'flatWorkspaceMemberMaps',+ 'flatObjectMetadataMaps',+ 'flatFieldMetadataMaps',+ ];const recordExportProgressSchema = z.object({processedRecordCount: z.number().int().nonnegative(),diff --git a/packages/twenty-server/src/engine/metadata-modules/flat-agent/services/workspace-flat-role-target-by-agent-id.service.ts b/packages/twenty-server/src/engine/metadata-modules/flat-agent/services/workspace-flat-role-target-by-agent-id.service.tsindex 07ff85e72f5..60a82d83df7 100644--- a/packages/twenty-server/src/engine/metadata-modules/flat-agent/services/workspace-flat-role-target-by-agent-id.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/flat-agent/services/workspace-flat-role-target-by-agent-id.service.ts@@ -1,64 +1,32 @@import { Injectable } from '@nestjs/common';-import { IsNull, Not } from 'typeorm';+import { isDefined } from 'twenty-shared/utils';-import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';+import { WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';-import { FlatRoleTargetByAgentIdMaps } from 'src/engine/metadata-modules/flat-agent/types/flat-role-target-by-agent-id-maps.type';-import { fromRoleTargetEntityToFlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/utils/from-role-target-entity-to-flat-role-target.util';-import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';-import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/types/workspace-cache-provider-context.type';-import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';-import { createIdToUniversalIdentifierMap } from 'src/engine/workspace-cache/utils/create-id-to-universal-identifier-map.util';--const FLAT_ROLE_TARGET_BY_AGENT_ID_ROWS_REQUIREMENT = {- roleTarget: {- columns: true,- groupBy: ['agentId'],- where: { agentId: Not(IsNull()) },- },- application: ['id', 'universalIdentifier'],- role: ['id', 'universalIdentifier'],- agent: ['id', 'universalIdentifier'],-} as const satisfies WorkspaceCacheRowsRequirement;+import { type FlatRoleTargetByAgentIdMaps } from 'src/engine/metadata-modules/flat-agent/types/flat-role-target-by-agent-id-maps.type';+import { WorkspaceDerivedCache } from 'src/engine/workspace-cache/decorators/workspace-derived-cache.decorator';+import { type WorkspaceCacheDataMap } from 'src/engine/workspace-cache/types/workspace-cache-key.type';@Injectable()-@WorkspaceCache('flatRoleTargetByAgentIdMaps', { packingPonderation: 1 })-export class WorkspaceFlatRoleTargetByAgentIdService extends WorkspaceCacheProvider<FlatRoleTargetByAgentIdMaps> {- override readonly rowsRequirement =- FLAT_ROLE_TARGET_BY_AGENT_ID_ROWS_REQUIREMENT;-- computeForCache({- rows,- }: WorkspaceCacheProviderContext<- typeof FLAT_ROLE_TARGET_BY_AGENT_ID_ROWS_REQUIREMENT- >): FlatRoleTargetByAgentIdMaps {- const {- roleTarget: roleTargets,- application: applications,- role: roles,- agent: agents,- } = rows;-- const applicationIdToUniversalIdentifierMap =- createIdToUniversalIdentifierMap(applications);- const roleIdToUniversalIdentifierMap =- createIdToUniversalIdentifierMap(roles);- const agentIdToUniversalIdentifierMap =- createIdToUniversalIdentifierMap(agents);-+@WorkspaceDerivedCache('flatRoleTargetByAgentIdMaps')+export class WorkspaceFlatRoleTargetByAgentIdService extends WorkspaceDerivedCacheProvider<+ 'flatRoleTargetByAgentIdMaps',+ 'flatRoleTargetMaps'+> {+ readonly sourceKeyName = 'flatRoleTargetMaps';++ protected computeFromSource(+ flatRoleTargetMaps: WorkspaceCacheDataMap['flatRoleTargetMaps'],+ ): FlatRoleTargetByAgentIdMaps {const flatRoleTargetByAgentIdMaps: FlatRoleTargetByAgentIdMaps = {};- for (const [agentId, agentRoleTargets] of roleTargets.byAgentId) {- const roleTargetEntity = agentRoleTargets[agentRoleTargets.length - 1];-- flatRoleTargetByAgentIdMaps[agentId] =- fromRoleTargetEntityToFlatRoleTarget({- entity: roleTargetEntity,- applicationIdToUniversalIdentifierMap,- roleIdToUniversalIdentifierMap,- agentIdToUniversalIdentifierMap,- });+ for (const flatRoleTarget of Object.values(+ flatRoleTargetMaps.byUniversalIdentifier,+ )) {+ if (isDefined(flatRoleTarget?.agentId)) {+ flatRoleTargetByAgentIdMaps[flatRoleTarget.agentId] = flatRoleTarget;+ }}return flatRoleTargetByAgentIdMaps;diff --git a/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-api-key-role-map-cache.service.ts b/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-api-key-role-map-cache.service.tsindex a212d87ae61..25e76af10ee 100644--- a/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-api-key-role-map-cache.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-api-key-role-map-cache.service.ts@@ -1,45 +1,33 @@import { Injectable } from '@nestjs/common';-import { IsNull, Not } from 'typeorm';-import { isDefined } from 'twenty-shared/utils';-import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';--import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';-import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/types/workspace-cache-provider-context.type';-import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';+import { WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';-const API_KEY_ROLE_ROWS_REQUIREMENT = {- roleTarget: {- columns: ['apiKeyId', 'roleId'],- where: { apiKeyId: Not(IsNull()) },- },-} as const satisfies WorkspaceCacheRowsRequirement;+import { WorkspaceDerivedCache } from 'src/engine/workspace-cache/decorators/workspace-derived-cache.decorator';+import { type WorkspaceCacheDataMap } from 'src/engine/workspace-cache/types/workspace-cache-key.type';@Injectable()-@WorkspaceCache('apiKeyRoleMap', { packingPonderation: 1 })-export class WorkspaceApiKeyRoleMapCacheService extends WorkspaceCacheProvider<- Record<string, string>+@WorkspaceDerivedCache('apiKeyRoleMap')+export class WorkspaceApiKeyRoleMapCacheService extends WorkspaceDerivedCacheProvider<+ 'apiKeyRoleMap',+ 'flatRoleTargetMaps'> {- override readonly rowsRequirement = API_KEY_ROLE_ROWS_REQUIREMENT;-- computeForCache({- rows,- }: WorkspaceCacheProviderContext<- typeof API_KEY_ROLE_ROWS_REQUIREMENT- >): Record<string, string> {- const { roleTarget: roleTargets } = rows;-- return roleTargets.reduce(- (acc, { apiKeyId, roleId }) => {- if (isDefined(apiKeyId)) {- acc[apiKeyId] = roleId;- }-- return acc;- },- {} as Record<string, string>,- );+ readonly sourceKeyName = 'flatRoleTargetMaps';++ protected computeFromSource(+ flatRoleTargetMaps: WorkspaceCacheDataMap['flatRoleTargetMaps'],+ ): Record<string, string> {+ const apiKeyRoleMap: Record<string, string> = {};++ for (const flatRoleTarget of Object.values(+ flatRoleTargetMaps.byUniversalIdentifier,+ )) {+ if (isDefined(flatRoleTarget?.apiKeyId)) {+ apiKeyRoleMap[flatRoleTarget.apiKeyId] = flatRoleTarget.roleId;+ }+ }++ return apiKeyRoleMap;}}diff --git a/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-user-workspace-role-map-cache.service.ts b/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-user-workspace-role-map-cache.service.tsindex 84070eb2aec..eba92b3e957 100644--- a/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-user-workspace-role-map-cache.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/role-target/services/workspace-user-workspace-role-map-cache.service.ts@@ -1,41 +1,35 @@import { Injectable } from '@nestjs/common';-import { IsNull, Not } from 'typeorm';-import { isDefined } from 'twenty-shared/utils';-import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';--import { UserWorkspaceRoleMap } from 'src/engine/metadata-modules/role-target/types/user-workspace-role-map.type';-import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';-import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/types/workspace-cache-provider-context.type';-import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';+import { WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';-const USER_WORKSPACE_ROLE_ROWS_REQUIREMENT = {- roleTarget: {- columns: ['userWorkspaceId', 'roleId'],- where: { userWorkspaceId: Not(IsNull()) },- },-} as const satisfies WorkspaceCacheRowsRequirement;+import { type UserWorkspaceRoleMap } from 'src/engine/metadata-modules/role-target/types/user-workspace-role-map.type';+import { WorkspaceDerivedCache } from 'src/engine/workspace-cache/decorators/workspace-derived-cache.decorator';+import { type WorkspaceCacheDataMap } from 'src/engine/workspace-cache/types/workspace-cache-key.type';@Injectable()-@WorkspaceCache('userWorkspaceRoleMap', { packingPonderation: 1 })-export class WorkspaceUserWorkspaceRoleMapCacheService extends WorkspaceCacheProvider<UserWorkspaceRoleMap> {- override readonly rowsRequirement = USER_WORKSPACE_ROLE_ROWS_REQUIREMENT;-- computeForCache({- rows,- }: WorkspaceCacheProviderContext<- typeof USER_WORKSPACE_ROLE_ROWS_REQUIREMENT- >): UserWorkspaceRoleMap {- const { roleTarget: roleTargets } = rows;-- return roleTargets.reduce((acc, { userWorkspaceId, roleId }) => {- if (isDefined(userWorkspaceId)) {- acc[userWorkspaceId] = roleId;+@WorkspaceDerivedCache('userWorkspaceRoleMap')+export class WorkspaceUserWorkspaceRoleMapCacheService extends WorkspaceDerivedCacheProvider<+ 'userWorkspaceRoleMap',+ 'flatRoleTargetMaps'+> {+ readonly sourceKeyName = 'flatRoleTargetMaps';++ protected computeFromSource(+ flatRoleTargetMaps: WorkspaceCacheDataMap['flatRoleTargetMaps'],+ ): UserWorkspaceRoleMap {+ const userWorkspaceRoleMap: UserWorkspaceRoleMap = {};++ for (const flatRoleTarget of Object.values(+ flatRoleTargetMaps.byUniversalIdentifier,+ )) {+ if (isDefined(flatRoleTarget?.userWorkspaceId)) {+ userWorkspaceRoleMap[flatRoleTarget.userWorkspaceId] =+ flatRoleTarget.roleId;}+ }- return acc;- }, {} as UserWorkspaceRoleMap);+ return userWorkspaceRoleMap;}}diff --git a/packages/twenty-server/src/engine/metadata-modules/role/services/__tests__/workspace-role-ids-with-all-records-access-cache.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/role/services/__tests__/workspace-role-ids-with-all-records-access-cache.service.spec.tsdeleted file mode 100644index 863519ffb5d..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/role/services/__tests__/workspace-role-ids-with-all-records-access-cache.service.spec.ts+++ /dev/null@@ -1,17 +0,0 @@-import { WorkspaceRoleIdsWithAllRecordsAccessCacheService } from 'src/engine/metadata-modules/role/services/workspace-role-ids-with-all-records-access-cache.service';--describe('WorkspaceRoleIdsWithAllRecordsAccessCacheService', () => {- it('lists the roles that administer every setting', () => {- expect(- new WorkspaceRoleIdsWithAllRecordsAccessCacheService().computeForCache({- workspaceId: 'workspace',- rows: {- role: [- { id: 'admin', canUpdateAllSettings: true },- { id: 'member', canUpdateAllSettings: false },- ],- },- } as never),- ).toEqual(['admin']);- });-});diff --git a/packages/twenty-server/src/engine/metadata-modules/role/services/workspace-role-ids-with-all-records-access-cache.service.ts b/packages/twenty-server/src/engine/metadata-modules/role/services/workspace-role-ids-with-all-records-access-cache.service.tsindex 455a2f69563..8d00a7ca6ae 100644--- a/packages/twenty-server/src/engine/metadata-modules/role/services/workspace-role-ids-with-all-records-access-cache.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/role/services/workspace-role-ids-with-all-records-access-cache.service.ts@@ -1,30 +1,26 @@import { Injectable } from '@nestjs/common';-import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';+import { isDefined } from 'twenty-shared/utils';-import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';-import { type WorkspaceCacheProviderContext } from 'src/engine/workspace-cache/types/workspace-cache-provider-context.type';-import { type WorkspaceCacheRowsRequirement } from 'src/engine/workspace-cache/types/workspace-cache-rows-requirement.type';+import { WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';-const ROLE_IDS_WITH_ALL_RECORDS_ACCESS_ROWS_REQUIREMENT = {- role: ['id', 'canUpdateAllSettings'],-} as const satisfies WorkspaceCacheRowsRequirement;+import { WorkspaceDerivedCache } from 'src/engine/workspace-cache/decorators/workspace-derived-cache.decorator';+import { type WorkspaceCacheDataMap } from 'src/engine/workspace-cache/types/workspace-cache-key.type';@Injectable()-@WorkspaceCache('roleIdsWithAllRecordsAccess', { packingPonderation: 1 })-export class WorkspaceRoleIdsWithAllRecordsAccessCacheService extends WorkspaceCacheProvider<- string[]+@WorkspaceDerivedCache('roleIdsWithAllRecordsAccess')+export class WorkspaceRoleIdsWithAllRecordsAccessCacheService extends WorkspaceDerivedCacheProvider<+ 'roleIdsWithAllRecordsAccess',+ 'flatRoleMaps'> {- override readonly rowsRequirement =- ROLE_IDS_WITH_ALL_RECORDS_ACCESS_ROWS_REQUIREMENT;+ readonly sourceKeyName = 'flatRoleMaps';- computeForCache({- rows,- }: WorkspaceCacheProviderContext<- typeof ROLE_IDS_WITH_ALL_RECORDS_ACCESS_ROWS_REQUIREMENT- >): string[] {- return rows.role- .filter((role) => role.canUpdateAllSettings)- .map((role) => role.id);+ protected computeFromSource(+ flatRoleMaps: WorkspaceCacheDataMap['flatRoleMaps'],+ ): string[] {+ return Object.values(flatRoleMaps.byUniversalIdentifier)+ .filter(isDefined)+ .filter((flatRole) => flatRole.canUpdateAllSettings)+ .map((flatRole) => flatRole.id);}}diff --git a/packages/twenty-server/src/engine/workspace-cache/constants/workspace-derived-cache-key-names.constant.ts b/packages/twenty-server/src/engine/workspace-cache/constants/workspace-derived-cache-key-names.constant.tsnew file mode 100644index 00000000000..a3324ca0549--- /dev/null+++ b/packages/twenty-server/src/engine/workspace-cache/constants/workspace-derived-cache-key-names.constant.ts@@ -0,0 +1,9 @@+import { type WorkspaceDerivedCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';++export const WORKSPACE_DERIVED_CACHE_KEY_NAMES = {+ roleIdsWithAllRecordsAccess: true,+ userWorkspaceRoleMap: true,+ apiKeyRoleMap: true,+ flatRoleTargetByAgentIdMaps: true,+ graphQLResolverNameMap: true,+} as const satisfies Record<WorkspaceDerivedCacheKeyName, true>;diff --git a/packages/twenty-server/src/engine/workspace-cache/decorators/workspace-derived-cache.decorator.ts b/packages/twenty-server/src/engine/workspace-cache/decorators/workspace-derived-cache.decorator.tsnew file mode 100644index 00000000000..b5ae4f6f71c--- /dev/null+++ b/packages/twenty-server/src/engine/workspace-cache/decorators/workspace-derived-cache.decorator.ts@@ -0,0 +1,21 @@+import { SetMetadata, type Type } from '@nestjs/common';++import { type WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';+import { type WorkspaceDerivedCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';++export const WORKSPACE_DERIVED_CACHE_KEY = 'WORKSPACE_DERIVED_CACHE_KEY';++export const WorkspaceDerivedCache =+ <TDerivedKeyName extends WorkspaceDerivedCacheKeyName>(+ workspaceDerivedCacheKeyName: TDerivedKeyName,+ ) =>+ <TProviderClass extends Type<WorkspaceDerivedCacheProvider<TDerivedKeyName>>>(+ target: TProviderClass,+ ): TProviderClass => {+ SetMetadata(+ WORKSPACE_DERIVED_CACHE_KEY,+ workspaceDerivedCacheKeyName,+ )(target);++ return target;+ };diff --git a/packages/twenty-server/src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service.ts b/packages/twenty-server/src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service.tsnew file mode 100644index 00000000000..42b4acc488e--- /dev/null+++ b/packages/twenty-server/src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service.ts@@ -0,0 +1,51 @@+import { Injectable } from '@nestjs/common';++import { isDefined } from 'twenty-shared/utils';++import {+ type WorkspaceCacheDataMap,+ type WorkspaceCacheKeyName,+ type WorkspaceDerivedCacheDataMap,+ type WorkspaceDerivedCacheKeyName,+} from 'src/engine/workspace-cache/types/workspace-cache-key.type';++type WorkspaceDerivedCacheSourceKeyName = {+ [TCacheKeyName in WorkspaceCacheKeyName]: WorkspaceCacheDataMap[TCacheKeyName] extends object+ ? TCacheKeyName+ : never;+}[WorkspaceCacheKeyName];++@Injectable()+export abstract class WorkspaceDerivedCacheProvider<+ TDerivedKeyName extends WorkspaceDerivedCacheKeyName =+ WorkspaceDerivedCacheKeyName,+ TSourceKeyName extends WorkspaceDerivedCacheSourceKeyName =+ WorkspaceDerivedCacheSourceKeyName,+> {+ abstract readonly sourceKeyName: TSourceKeyName;++ private readonly derivedDataBySource = new WeakMap<+ WorkspaceCacheDataMap[TSourceKeyName],+ WorkspaceDerivedCacheDataMap[TDerivedKeyName]+ >();++ protected abstract computeFromSource(+ source: WorkspaceCacheDataMap[TSourceKeyName],+ ): WorkspaceDerivedCacheDataMap[TDerivedKeyName];++ getFromSource(+ source: WorkspaceCacheDataMap[TSourceKeyName],+ ): WorkspaceDerivedCacheDataMap[TDerivedKeyName] {+ const memoizedDerivedData = this.derivedDataBySource.get(source);++ if (isDefined(memoizedDerivedData)) {+ return memoizedDerivedData;+ }++ const derivedData = this.computeFromSource(source);++ this.derivedDataBySource.set(source, derivedData);++ return derivedData;+ }+}diff --git a/packages/twenty-server/src/engine/workspace-cache/services/workspace-cache.service.ts b/packages/twenty-server/src/engine/workspace-cache/services/workspace-cache.service.tsindex d22cbe5e0b0..8d4458d1d25 100644--- a/packages/twenty-server/src/engine/workspace-cache/services/workspace-cache.service.ts+++ b/packages/twenty-server/src/engine/workspace-cache/services/workspace-cache.service.ts@@ -15,6 +15,7 @@ import { DataSource } from 'typeorm';import { isDefined, isValidUuid } from 'twenty-shared/utils';import { WorkspaceCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-cache-provider.service';+import { WorkspaceDerivedCacheProvider } from 'src/engine/workspace-cache/interfaces/workspace-derived-cache-provider.service';import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';@@ -26,6 +27,7 @@ import {WORKSPACE_CACHE_OPTIONS,WorkspaceCacheOptions,} from 'src/engine/workspace-cache/decorators/workspace-cache.decorator';+import { WORKSPACE_DERIVED_CACHE_KEY } from 'src/engine/workspace-cache/decorators/workspace-derived-cache.decorator';import {WorkspaceCacheException,WorkspaceCacheExceptionCode,@@ -35,9 +37,12 @@ import { WorkspaceCacheRowsBatchLoader } from 'src/engine/workspace-cache/servicimport {WorkspaceCacheKeyName,type WorkspaceCacheDataMap,+ type WorkspaceCacheOrDerivedCacheDataMap,+ type WorkspaceCacheOrDerivedCacheKeyName,type WorkspaceCacheResult,type WorkspaceCacheResultWithHashes,type WorkspaceCacheStoredDataMap,+ type WorkspaceDerivedCacheKeyName,} from 'src/engine/workspace-cache/types/workspace-cache-key.type';import {type VersionEntry,@@ -46,6 +51,7 @@ import {import { combineCacheHashes } from 'src/engine/workspace-cache/utils/combine-cache-hashes.util';import { getKeyNameFromLocalCacheKey } from 'src/engine/workspace-cache/utils/get-key-name-from-local-cache-key.util';import { packIdleVersions } from 'src/engine/workspace-cache/utils/pack-idle-versions.util';+import { isWorkspaceDerivedCacheKeyName } from 'src/engine/workspace-cache/utils/is-workspace-derived-cache-key-name.util';import {deserializeCacheBlob,serializeCacheBlob,@@ -78,6 +84,11 @@ type CacheEntriesResult = {hashes: Partial<Record<WorkspaceCacheKeyName, string>>;};+type CacheOrDerivedCacheEntriesResult = {+ data: Partial<WorkspaceCacheOrDerivedCacheDataMap>;+ hashes: Partial<Record<WorkspaceCacheOrDerivedCacheKeyName, string>>;+};+type RecomputeHashResolution =| { strategy: 'mint' }| {@@ -97,6 +108,10 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {WorkspaceCacheKeyName,WorkspaceCacheProvider<CacheDataType, StoredCacheDataType>>();+ private readonly workspaceDerivedCacheProviders = new Map<+ WorkspaceDerivedCacheKeyName,+ WorkspaceDerivedCacheProvider+ >();private readonly localDataOnlyKeys = new Set<WorkspaceCacheKeyName>();private readonly packingPonderationByKey = new Map<WorkspaceCacheKeyName,@@ -157,6 +172,22 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {);}}++ const workspaceDerivedCacheKeyName =+ this.reflector.get<WorkspaceDerivedCacheKeyName>(+ WORKSPACE_DERIVED_CACHE_KEY,+ instance.constructor,+ );++ if (+ isDefined(workspaceDerivedCacheKeyName) &&+ instance instanceof WorkspaceDerivedCacheProvider+ ) {+ this.workspaceDerivedCacheProviders.set(+ workspaceDerivedCacheKeyName,+ instance,+ );+ }}this.cacheMetricsService.start(this.localCache);@@ -187,10 +218,9 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {this.packingTimer.unref();}- public async getOrRecompute<const K extends WorkspaceCacheKeyName[]>(- workspaceId: string,- cacheKeyNames: K,- ): Promise<WorkspaceCacheResult<K>> {+ public async getOrRecompute<+ const K extends WorkspaceCacheOrDerivedCacheKeyName[],+ >(workspaceId: string, cacheKeyNames: K): Promise<WorkspaceCacheResult<K>> {const { data } = await this.getOrRecomputeWithHashes(workspaceId,cacheKeyNames,@@ -200,13 +230,88 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {}public async getOrRecomputeWithHashes<- const K extends WorkspaceCacheKeyName[],+ const K extends WorkspaceCacheOrDerivedCacheKeyName[],>(workspaceId: string,cacheKeyNames: K,): Promise<WorkspaceCacheResultWithHashes<K>> {this.assertValidCacheParameters(workspaceId, cacheKeyNames);+ const providerEntries = await this.getOrRecomputeProviderEntries(+ workspaceId,+ this.getSourceCacheKeyNames(cacheKeyNames),+ );++ if (!cacheKeyNames.some(isWorkspaceDerivedCacheKeyName)) {+ return providerEntries as WorkspaceCacheResultWithHashes<K>;+ }++ const result: CacheOrDerivedCacheEntriesResult = { data: {}, hashes: {} };++ for (const cacheKeyName of cacheKeyNames) {+ if (!isWorkspaceDerivedCacheKeyName(cacheKeyName)) {+ Object.assign(result.data, {+ [cacheKeyName]: providerEntries.data[cacheKeyName],+ });+ result.hashes[cacheKeyName] = providerEntries.hashes[cacheKeyName];+ continue;+ }++ const derivedCacheProvider =+ this.getDerivedCacheProviderOrThrow(cacheKeyName);+ const { sourceKeyName } = derivedCacheProvider;+ const source = providerEntries.data[sourceKeyName];++ if (!isDefined(source)) {+ throw new WorkspaceCacheException(+ `Source cache "${sourceKeyName}" of derived cache "${cacheKeyName}" is not loaded`,+ WorkspaceCacheExceptionCode.INTERNAL_SERVER_ERROR,+ );+ }++ Object.assign(result.data, {+ [cacheKeyName]: derivedCacheProvider.getFromSource(source),+ });+ result.hashes[cacheKeyName] = providerEntries.hashes[sourceKeyName];+ }++ return result as WorkspaceCacheResultWithHashes<K>;+ }++ private getSourceCacheKeyNames(+ cacheKeyNames: readonly WorkspaceCacheOrDerivedCacheKeyName[],+ ): WorkspaceCacheKeyName[] {+ return [+ ...new Set(+ cacheKeyNames.map((cacheKeyName) =>+ isWorkspaceDerivedCacheKeyName(cacheKeyName)+ ? this.getDerivedCacheProviderOrThrow(cacheKeyName).sourceKeyName+ : cacheKeyName,+ ),+ ),+ ];+ }++ private getDerivedCacheProviderOrThrow(+ derivedKeyName: WorkspaceDerivedCacheKeyName,+ ): WorkspaceDerivedCacheProvider {+ const derivedCacheProvider =+ this.workspaceDerivedCacheProviders.get(derivedKeyName);++ if (!isDefined(derivedCacheProvider)) {+ throw new WorkspaceCacheException(+ `Derived cache provider with key name "${derivedKeyName}" not found`,+ WorkspaceCacheExceptionCode.INTERNAL_SERVER_ERROR,+ );+ }++ return derivedCacheProvider;+ }++ private async getOrRecomputeProviderEntries(+ workspaceId: string,+ cacheKeyNames: WorkspaceCacheKeyName[],+ ): Promise<CacheEntriesResult> {const memoKey =`${workspaceId}-${[...cacheKeyNames].sort().join(',')}` as const;@@ -263,33 +368,49 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {},);- return result as WorkspaceCacheResultWithHashes<K>;+ return result as CacheEntriesResult;}public async getOrRecomputeCombinedHash(workspaceId: string,- cacheKeyNames: WorkspaceCacheKeyName[],+ cacheKeyNames: WorkspaceCacheOrDerivedCacheKeyName[],): Promise<string> {this.assertValidCacheParameters(workspaceId, cacheKeyNames);- const cachedHashes = await this.getCacheHashes(workspaceId, cacheKeyNames);- const missingKeys = cacheKeyNames.filter(+ const sourceCacheKeyNames = this.getSourceCacheKeyNames(cacheKeyNames);++ const cachedHashes = await this.getCacheHashes(+ workspaceId,+ sourceCacheKeyNames,+ );+ const missingKeys = sourceCacheKeyNames.filter((cacheKeyName) => !isDefined(cachedHashes[cacheKeyName]),);- if (missingKeys.length === 0) {- return combineCacheHashes(cachedHashes, cacheKeyNames);+ const sourceHashes =+ missingKeys.length === 0+ ? cachedHashes+ : {+ ...cachedHashes,+ ...(+ await this.getOrRecomputeProviderEntries(workspaceId, missingKeys)+ ).hashes,+ };++ const cacheOrDerivedCacheHashes: Partial<+ Record<WorkspaceCacheOrDerivedCacheKeyName, string>+ > = { ...sourceHashes };++ for (const cacheKeyName of cacheKeyNames) {+ if (isWorkspaceDerivedCacheKeyName(cacheKeyName)) {+ cacheOrDerivedCacheHashes[cacheKeyName] =+ sourceHashes[+ this.getDerivedCacheProviderOrThrow(cacheKeyName).sourceKeyName+ ];+ }}- const { hashes: recomputedHashes } = await this.getOrRecomputeWithHashes(- workspaceId,- missingKeys,- );-- return combineCacheHashes(- { ...cachedHashes, ...recomputedHashes },- cacheKeyNames,- );+ return combineCacheHashes(cacheOrDerivedCacheHashes, cacheKeyNames);}private collectRowsRequirements(cacheKeyNames: WorkspaceCacheKeyName[]) {@@ -350,11 +471,16 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {public async flush(workspaceId: string,- cacheKeyNames: WorkspaceCacheKeyName[],+ cacheKeyNames: WorkspaceCacheOrDerivedCacheKeyName[],): Promise<void> {- await this.deleteFromRedis(workspaceId, cacheKeyNames);+ const storedCacheKeyNames = cacheKeyNames.filter(+ (cacheKeyName): cacheKeyName is WorkspaceCacheKeyName =>+ !isWorkspaceDerivedCacheKeyName(cacheKeyName),+ );++ await this.deleteFromRedis(workspaceId, storedCacheKeyNames);- this.deleteFromLocalCache(workspaceId, cacheKeyNames);+ this.deleteFromLocalCache(workspaceId, storedCacheKeyNames);}public async evictWorkspaceFromLocalCache(@@ -371,7 +497,7 @@ export class WorkspaceCacheService implements OnModuleInit, OnModuleDestroy {private assertValidCacheParameters(workspaceId: string,- cacheKeyNames: WorkspaceCacheKeyName[],+ cacheKeyNames: WorkspaceCacheOrDerivedCacheKeyName[],): void {if (!isDefined(workspaceId) ||diff --git a/packages/twenty-server/src/engine/workspace-cache/types/workspace-cache-key.type.ts b/packages/twenty-server/src/engine/workspace-cache/types/workspace-cache-key.type.tsindex 0b3e7730b69..2e7152a010a 100644--- a/packages/twenty-server/src/engine/workspace-cache/types/workspace-cache-key.type.ts+++ b/packages/twenty-server/src/engine/workspace-cache/types/workspace-cache-key.type.ts@@ -9,7 +9,6 @@ import { type OrmFlatFieldMetadata } from 'src/engine/metadata-modules/flat-fielimport { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';import { type ResolverNameMapEntry } from 'src/engine/api/graphql/direct-execution/utils/build-resolver-name-map.util';import { type FlatApiKey } from 'src/engine/core-modules/api-key/types/flat-api-key.type';-import { type ApplicationVariableCacheMaps } from 'src/engine/core-modules/application/application-variable/types/application-variable-cache-maps.type';import { type FlatApplicationCacheMaps } from 'src/engine/core-modules/application/types/flat-application-cache-maps.type';import { type CurrentBillingSubscription } from 'src/engine/core-modules/billing/types/flat-billing-subscription.type';import { type BillingEntitlements } from 'src/engine/core-modules/billing/types/billing-entitlements.type';@@ -26,19 +25,13 @@ import { type UsageLimits } from 'src/engine/core-modules/usage-limit/types/usagexport type AdditionalCacheDataMaps = {featureFlagsMap: Record<FeatureFlagKey, boolean>;rolesPermissions: ObjectsPermissionsByRoleId;- roleIdsWithAllRecordsAccess: string[];- userWorkspaceRoleMap: UserWorkspaceRoleMap;- apiKeyRoleMap: Record<string, string>;apiKeyMap: Record<string, FlatApiKey>;flatApplicationMaps: FlatApplicationCacheMaps;ORMEntityMetadatas: EntityMetadata[];flatFieldMetadataMapsOrm: FlatEntityMaps<OrmFlatFieldMetadata>;- flatRoleTargetByAgentIdMaps: FlatRoleTargetByAgentIdMaps;flatRowLevelPermissionPredicateMaps: FlatRowLevelPermissionPredicateMaps;flatRowLevelPermissionPredicateGroupMaps: FlatRowLevelPermissionPredicateGroupMaps;flatWorkspaceMemberMaps: FlatWorkspaceMemberMaps;- applicationVariableMaps: ApplicationVariableCacheMaps;- graphQLResolverNameMap: Record<string, ResolverNameMapEntry>;currentBillingSubscription: CurrentBillingSubscription;billingEntitlements: BillingEntitlements;workflowAutomatedTriggerMaps: WorkflowAutomatedTriggerMaps;@@ -50,15 +43,34 @@ export type WorkspaceCacheDataMap = AllFlatEntityMaps<true> &export type WorkspaceCacheKeyName = keyof WorkspaceCacheDataMap;-export type WorkspaceCacheResult<K extends WorkspaceCacheKeyName[]> = {- [P in K[number]]: WorkspaceCacheDataMap[P];+export type WorkspaceDerivedCacheDataMap = {+ roleIdsWithAllRecordsAccess: string[];+ userWorkspaceRoleMap: UserWorkspaceRoleMap;+ apiKeyRoleMap: Record<string, string>;+ flatRoleTargetByAgentIdMaps: FlatRoleTargetByAgentIdMaps;+ graphQLResolverNameMap: Record<string, ResolverNameMapEntry>;+};++export type WorkspaceDerivedCacheKeyName = keyof WorkspaceDerivedCacheDataMap;++export type WorkspaceCacheOrDerivedCacheDataMap = WorkspaceCacheDataMap &+ WorkspaceDerivedCacheDataMap;++export type WorkspaceCacheOrDerivedCacheKeyName =+ keyof WorkspaceCacheOrDerivedCacheDataMap;++export type WorkspaceCacheResult<+ K extends WorkspaceCacheOrDerivedCacheKeyName[],+> = {+ [P in K[number]]: WorkspaceCacheOrDerivedCacheDataMap[P];};-export type WorkspaceCacheResultWithHashes<K extends WorkspaceCacheKeyName[]> =- {- data: WorkspaceCacheResult<K>;- hashes: { [P in K[number]]: string };- };+export type WorkspaceCacheResultWithHashes<+ K extends WorkspaceCacheOrDerivedCacheKeyName[],+> = {+ data: WorkspaceCacheResult<K>;+ hashes: { [P in K[number]]: string };+};export type WorkspaceCacheStoredDataMap = Omit<WorkspaceCacheDataMap,diff --git a/packages/twenty-server/src/engine/workspace-cache/utils/combine-cache-hashes.util.ts b/packages/twenty-server/src/engine/workspace-cache/utils/combine-cache-hashes.util.tsindex 6576a514683..6e0636e97ae 100644--- a/packages/twenty-server/src/engine/workspace-cache/utils/combine-cache-hashes.util.ts+++ b/packages/twenty-server/src/engine/workspace-cache/utils/combine-cache-hashes.util.ts@@ -2,11 +2,11 @@ import { createHash } from 'crypto';import { isDefined } from 'twenty-shared/utils';-import { type WorkspaceCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';+import { type WorkspaceCacheOrDerivedCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';export const combineCacheHashes = (- hashes: Partial<Record<WorkspaceCacheKeyName, string>>,- cacheKeyNames: readonly WorkspaceCacheKeyName[],+ hashes: Partial<Record<WorkspaceCacheOrDerivedCacheKeyName, string>>,+ cacheKeyNames: readonly WorkspaceCacheOrDerivedCacheKeyName[],): string => {if (cacheKeyNames.length === 0) {throw new Error('Cannot combine cache hashes without cache key names');diff --git a/packages/twenty-server/src/engine/workspace-cache/utils/is-workspace-derived-cache-key-name.util.ts b/packages/twenty-server/src/engine/workspace-cache/utils/is-workspace-derived-cache-key-name.util.tsnew file mode 100644index 00000000000..395b0db60c1--- /dev/null+++ b/packages/twenty-server/src/engine/workspace-cache/utils/is-workspace-derived-cache-key-name.util.ts@@ -0,0 +1,13 @@+import { WORKSPACE_DERIVED_CACHE_KEY_NAMES } from 'src/engine/workspace-cache/constants/workspace-derived-cache-key-names.constant';+import {+ type WorkspaceCacheOrDerivedCacheKeyName,+ type WorkspaceDerivedCacheKeyName,+} from 'src/engine/workspace-cache/types/workspace-cache-key.type';++export const isWorkspaceDerivedCacheKeyName = (+ cacheKeyName: WorkspaceCacheOrDerivedCacheKeyName,+): cacheKeyName is WorkspaceDerivedCacheKeyName =>+ Object.prototype.hasOwnProperty.call(+ WORKSPACE_DERIVED_CACHE_KEY_NAMES,+ cacheKeyName,+ );diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/services/workspace-migration-runner.service.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/services/workspace-migration-runner.service.tsindex 4ce0de5b6e3..4be0c658257 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/services/workspace-migration-runner.service.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/workspace-migration-runner/services/workspace-migration-runner.service.ts@@ -66,31 +66,18 @@ export class WorkspaceMigrationRunnerService {flatMapsKeysSet.has('flatFieldMetadataMaps');if (shouldIncrementMetadataGraphqlSchemaVersion) {- legacyCacheKeyNames.push('ORMEntityMetadatas', 'graphQLResolverNameMap');+ legacyCacheKeyNames.push('ORMEntityMetadatas');}- const shouldInvalidateRoleMapCache =- flatMapsKeysSet.has('flatRoleMaps') ||- flatMapsKeysSet.has('flatRoleTargetMaps');-const shouldInvalidateRolesPermissionsCache =+ flatMapsKeysSet.has('flatRoleMaps') ||+ flatMapsKeysSet.has('flatRoleTargetMaps') ||flatMapsKeysSet.has('flatObjectPermissionMaps') ||flatMapsKeysSet.has('flatFieldPermissionMaps') ||flatMapsKeysSet.has('flatRolePermissionFlagMaps');- if (shouldInvalidateRoleMapCache || shouldInvalidateRolesPermissionsCache) {- legacyCacheKeyNames.push(- 'rolesPermissions',- 'roleIdsWithAllRecordsAccess',- 'userWorkspaceRoleMap',- 'flatRoleTargetMaps',- 'apiKeyRoleMap',- 'flatRoleTargetByAgentIdMaps',- );- }-- if (flatMapsKeysSet.has('flatApplicationVariableMaps')) {- legacyCacheKeyNames.push('applicationVariableMaps');+ if (shouldInvalidateRolesPermissionsCache) {+ legacyCacheKeyNames.push('rolesPermissions');}return {diff --git a/packages/twenty-server/test/integration/graphql/suites/settings-permissions/settings-permission-resolution.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/settings-permissions/settings-permission-resolution.integration-spec.tsindex e9c4f8c0c35..7331fa536d4 100644--- a/packages/twenty-server/test/integration/graphql/suites/settings-permissions/settings-permission-resolution.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/settings-permissions/settings-permission-resolution.integration-spec.ts@@ -224,7 +224,7 @@ describe('Settings permission resolution', () => {'DELETE FROM core."roleTarget" WHERE "apiKeyId" = $1',[unassignedApiKey.apiKeyId],);- await invalidateWorkspaceCache(['apiKeyRoleMap']);+ await invalidateWorkspaceCache(['flatRoleTargetMaps']);grantedApplication = await setupApplicationWithVariable({name: 'Settings resolution granted app',@@ -277,7 +277,7 @@ describe('Settings permission resolution', () => {);}- await invalidateWorkspaceCache(['apiKeyMap', 'apiKeyRoleMap']);+ await invalidateWorkspaceCache(['apiKeyMap', 'flatRoleTargetMaps']);for (const roleId of createdRoleIds) {await deleteOneRole({diff --git a/packages/twenty-server/test/integration/metadata/suites/graphql-operation-cache/metadata-graphql-operations-cache-dependencies.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/graphql-operation-cache/metadata-graphql-operations-cache-dependencies.integration-spec.tsindex 168669de39b..2496e42aa5d 100644--- a/packages/twenty-server/test/integration/metadata/suites/graphql-operation-cache/metadata-graphql-operations-cache-dependencies.integration-spec.ts+++ b/packages/twenty-server/test/integration/metadata/suites/graphql-operation-cache/metadata-graphql-operations-cache-dependencies.integration-spec.ts@@ -7,7 +7,7 @@ import { type ASTNode } from 'graphql';import { METADATA_GRAPHQL_OPERATIONS_TO_CACHE } from 'src/engine/api/graphql/graphql-config/constants/metadata-graphql-operations-to-cache.constant';import { FIND_ALL_VIEWS_GRAPHQL_OPERATION } from 'src/engine/metadata-modules/view/constants/find-all-views-graphql-operation.constant';import { type WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';-import { type WorkspaceCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';+import { type WorkspaceCacheOrDerivedCacheKeyName } from 'src/engine/workspace-cache/types/workspace-cache-key.type';// Mirrors OBJECT_METADATA_FRAGMENT from twenty-front// (packages/twenty-front/src/modules/object-metadata/graphql/fragment.ts):@@ -233,7 +233,7 @@ const FIND_ALL_VIEWS_BASELINE_QUERY = gql`describe('metadata GraphQL operations cache dependencies', () => {const recordAccessedCacheKeys = async (query: ASTNode,- ): Promise<Set<WorkspaceCacheKeyName>> => {+ ): Promise<Set<WorkspaceCacheOrDerivedCacheKeyName>> => {const workspaceCacheService =getAppProviderByClassName<WorkspaceCacheService>('WorkspaceCacheService');const spy = jest.spyOn(workspaceCacheService, 'getOrRecomputeWithHashes');@@ -243,7 +243,7 @@ describe('metadata GraphQL operations cache dependencies', () => {expect(response.body.errors).toBeUndefined();- return new Set<WorkspaceCacheKeyName>(+ return new Set<WorkspaceCacheOrDerivedCacheKeyName>(spy.mock.calls.flatMap(([, cacheKeyNames]) => cacheKeyNames),);} finally {@@ -259,8 +259,8 @@ describe('metadata GraphQL operations cache dependencies', () => {operationName: string;fullQuery: ASTNode;baselineQuery: ASTNode;- }): Promise<WorkspaceCacheKeyName[]> => {- const declaredDependencies = new Set<WorkspaceCacheKeyName>(+ }): Promise<WorkspaceCacheOrDerivedCacheKeyName[]> => {+ const declaredDependencies = new Set<WorkspaceCacheOrDerivedCacheKeyName>(METADATA_GRAPHQL_OPERATIONS_TO_CACHE[operationName].dependencies,);const requestInfrastructureKeys =