The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Improve metadata version caching (#11775)
Marie23d71915f6ff
diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception.tsindex 5e7f0d2ac0a..7edd9233c67 100644--- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception.ts+++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception.ts@@ -23,4 +23,5 @@ export enum GraphqlQueryRunnerExceptionCode {RELATION_SETTINGS_NOT_FOUND = 'RELATION_SETTINGS_NOT_FOUND',RELATION_TARGET_OBJECT_METADATA_NOT_FOUND = 'RELATION_TARGET_OBJECT_METADATA_NOT_FOUND',NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',+ OBJECT_METADATA_COLLECTION_NOT_FOUND = 'OBJECT_METADATA_COLLECTION_NOT_FOUND',}diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-schema.factory.ts b/packages/twenty-server/src/engine/api/graphql/workspace-schema.factory.tsindex c687f083d0f..526679fc9ff 100644--- a/packages/twenty-server/src/engine/api/graphql/workspace-schema.factory.ts+++ b/packages/twenty-server/src/engine/api/graphql/workspace-schema.factory.ts@@ -4,6 +4,7 @@ import { makeExecutableSchema } from '@graphql-tools/schema';import chalk from 'chalk';import { GraphQLSchema, printSchema } from 'graphql';import { gql } from 'graphql-tag';+import { isDefined } from 'twenty-shared/utils';import {GraphqlQueryRunnerException,@@ -17,6 +18,7 @@ import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.typimport { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';+import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';import { WorkspaceMetadataCacheService } from 'src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service';import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';@@ -58,34 +60,49 @@ export class WorkspaceSchemaFactory {return new GraphQLSchema({});}- const currentCacheVersion =+ let currentCacheVersion =await this.workspaceCacheStorageService.getMetadataVersion(authContext.workspace.id,);+ let objectMetadataMaps: ObjectMetadataMaps | undefined;+if (currentCacheVersion === undefined) {- await this.workspaceMetadataCacheService.recomputeMetadataCache({- workspaceId: authContext.workspace.id,- });+ const recomputed =+ await this.workspaceMetadataCacheService.recomputeMetadataCache({+ workspaceId: authContext.workspace.id,+ });++ objectMetadataMaps = recomputed?.recomputedObjectMetadataMaps;+ currentCacheVersion = recomputed?.recomputedMetadataVersion;+ } else {+ objectMetadataMaps =+ await this.workspaceCacheStorageService.getObjectMetadataMaps(+ authContext.workspace.id,+ currentCacheVersion,+ );- throw new GraphqlQueryRunnerException(- 'Metadata cache version not found',- GraphqlQueryRunnerExceptionCode.METADATA_CACHE_VERSION_NOT_FOUND,- );- }+ if (!isDefined(objectMetadataMaps)) {+ const recomputed =+ await this.workspaceMetadataCacheService.recomputeMetadataCache({+ workspaceId: authContext.workspace.id,+ });- const objectMetadataMaps =- await this.workspaceCacheStorageService.getObjectMetadataMaps(- authContext.workspace.id,- currentCacheVersion,- );+ objectMetadataMaps = recomputed?.recomputedObjectMetadataMaps;+ currentCacheVersion = recomputed?.recomputedMetadataVersion;+ }+ }if (!objectMetadataMaps) {- await this.workspaceMetadataCacheService.recomputeMetadataCache({- workspaceId: authContext.workspace.id,- });throw new GraphqlQueryRunnerException('Object metadata collection not found',+ GraphqlQueryRunnerExceptionCode.OBJECT_METADATA_COLLECTION_NOT_FOUND,+ );+ }++ if (!currentCacheVersion) {+ throw new GraphqlQueryRunnerException(+ 'Metadata cache version not found',GraphqlQueryRunnerExceptionCode.METADATA_CACHE_VERSION_NOT_FOUND,);}diff --git a/packages/twenty-server/src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service.ts b/packages/twenty-server/src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service.tsindex 5ae6f76763b..90f26706e60 100644--- a/packages/twenty-server/src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/workspace-metadata-cache/services/workspace-metadata-cache.service.ts@@ -5,6 +5,7 @@ import { Repository } from 'typeorm';import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';+import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';import { generateObjectMetadataMaps } from 'src/engine/metadata-modules/utils/generate-object-metadata-maps.util';import {WorkspaceMetadataCacheException,@@ -30,7 +31,13 @@ export class WorkspaceMetadataCacheService {}: {workspaceId: string;ignoreLock?: boolean;- }): Promise<void> {+ }): Promise<+ | {+ recomputedObjectMetadataMaps: ObjectMetadataMaps;+ recomputedMetadataVersion: number;+ }+ | undefined+ > {const currentCacheVersion =await this.getMetadataVersionFromCache(workspaceId);@@ -44,16 +51,22 @@ export class WorkspaceMetadataCacheService {);}- const isAlreadyCaching =- await this.workspaceCacheStorageService.getObjectMetadataOngoingCachingLock(- workspaceId,- currentDatabaseVersion,- );-- if (!ignoreLock && isAlreadyCaching) {+ if (currentDatabaseVersion === currentCacheVersion) {return;}+ if (!ignoreLock) {+ const isAlreadyCaching =+ await this.workspaceCacheStorageService.getObjectMetadataOngoingCachingLock(+ workspaceId,+ currentDatabaseVersion,+ );++ if (isAlreadyCaching) {+ return;+ }+ }+if (currentCacheVersion !== undefined) {this.workspaceCacheStorageService.flushVersionedMetadata(workspaceId,@@ -61,40 +74,47 @@ export class WorkspaceMetadataCacheService {);}- await this.workspaceCacheStorageService.addObjectMetadataCollectionOngoingCachingLock(- workspaceId,- currentDatabaseVersion,- );-- await this.workspaceCacheStorageService.setMetadataVersion(- workspaceId,- currentDatabaseVersion,- );-- const objectMetadataItems = await this.objectMetadataRepository.find({- where: { workspaceId },- relations: [- 'fields',- 'fields.fromRelationMetadata',- 'fields.toRelationMetadata',- 'indexMetadatas',- 'indexMetadatas.indexFieldMetadatas',- ],- });+ try {+ await this.workspaceCacheStorageService.addObjectMetadataCollectionOngoingCachingLock(+ workspaceId,+ currentDatabaseVersion,+ );- const freshObjectMetadataMaps =- generateObjectMetadataMaps(objectMetadataItems);+ const objectMetadataItems = await this.objectMetadataRepository.find({+ where: { workspaceId },+ relations: [+ 'fields',+ 'fields.fromRelationMetadata',+ 'fields.toRelationMetadata',+ 'indexMetadatas',+ 'indexMetadatas.indexFieldMetadatas',+ ],+ });++ const freshObjectMetadataMaps =+ generateObjectMetadataMaps(objectMetadataItems);++ await this.workspaceCacheStorageService.setObjectMetadataMaps(+ workspaceId,+ currentDatabaseVersion,+ freshObjectMetadataMaps,+ );- await this.workspaceCacheStorageService.setObjectMetadataMaps(- workspaceId,- currentDatabaseVersion,- freshObjectMetadataMaps,- );+ await this.workspaceCacheStorageService.setMetadataVersion(+ workspaceId,+ currentDatabaseVersion,+ );- await this.workspaceCacheStorageService.removeObjectMetadataOngoingCachingLock(- workspaceId,- currentDatabaseVersion,- );+ return {+ recomputedObjectMetadataMaps: freshObjectMetadataMaps,+ recomputedMetadataVersion: currentDatabaseVersion,+ };+ } finally {+ await this.workspaceCacheStorageService.removeObjectMetadataOngoingCachingLock(+ workspaceId,+ currentDatabaseVersion,+ );+ }}private async getMetadataVersionFromDatabase(