public static bool TryUpdateExistingEntity()

in src/Cli/ConfigGenerator.cs [1161:1303]


        public static bool TryUpdateExistingEntity(UpdateOptions options, RuntimeConfig initialConfig, out RuntimeConfig updatedConfig)
        {
            updatedConfig = initialConfig;
            // Check if Entity is present
            if (!initialConfig.Entities.TryGetValue(options.Entity, out Entity? entity))
            {
                _logger.LogError("Entity: '{entityName}' not found. Please add the entity first.", options.Entity);
                return false;
            }

            if (!TryGetUpdatedSourceObjectWithOptions(options, entity, out EntitySource? updatedSource))
            {
                _logger.LogError("Failed to update the source object.");
                return false;
            }

            bool isCurrentEntityStoredProcedure = IsStoredProcedure(entity);
            bool doOptionsRepresentStoredProcedure = options.SourceType is not null && IsStoredProcedure(options);

            // Validations to ensure that REST methods and GraphQL operations can be configured only
            // for stored procedures
            if (options.GraphQLOperationForStoredProcedure is not null &&
                !(isCurrentEntityStoredProcedure || doOptionsRepresentStoredProcedure))
            {
                _logger.LogError("--graphql.operation can be configured only for stored procedures.");
                return false;
            }

            if ((options.RestMethodsForStoredProcedure is not null && options.RestMethodsForStoredProcedure.Any())
                && !(isCurrentEntityStoredProcedure || doOptionsRepresentStoredProcedure))
            {
                _logger.LogError("--rest.methods can be configured only for stored procedures.");
                return false;
            }

            if (isCurrentEntityStoredProcedure || doOptionsRepresentStoredProcedure)
            {
                if (CheckConflictingGraphQLConfigurationForStoredProcedures(options))
                {
                    _logger.LogError("Conflicting GraphQL configurations found.");
                    return false;
                }

                if (CheckConflictingRestConfigurationForStoredProcedures(options))
                {
                    _logger.LogError("Conflicting Rest configurations found.");
                    return false;
                }
            }

            EntityRestOptions updatedRestDetails = ConstructUpdatedRestDetails(entity, options, initialConfig.DataSource.DatabaseType == DatabaseType.CosmosDB_NoSQL);
            EntityGraphQLOptions updatedGraphQLDetails = ConstructUpdatedGraphQLDetails(entity, options);
            EntityPermission[]? updatedPermissions = entity!.Permissions;
            Dictionary<string, EntityRelationship>? updatedRelationships = entity.Relationships;
            Dictionary<string, string>? updatedMappings = entity.Mappings;
            EntityActionPolicy? updatedPolicy = GetPolicyForOperation(options.PolicyRequest, options.PolicyDatabase);
            EntityActionFields? updatedFields = GetFieldsForOperation(options.FieldsToInclude, options.FieldsToExclude);

            if (!updatedGraphQLDetails.Enabled)
            {
                _logger.LogWarning("Disabling GraphQL for this entity will restrict its usage in relationships");
            }

            EntitySourceType? updatedSourceType = updatedSource.Type;

            if (options.Permissions is not null && options.Permissions.Any())
            {
                // Get the Updated Permission Settings
                updatedPermissions = GetUpdatedPermissionSettings(entity, options.Permissions, updatedPolicy, updatedFields, updatedSourceType);

                if (updatedPermissions is null)
                {
                    _logger.LogError("Failed to update permissions.");
                    return false;
                }
            }
            else
            {

                if (options.FieldsToInclude is not null && options.FieldsToInclude.Any()
                    || options.FieldsToExclude is not null && options.FieldsToExclude.Any())
                {
                    _logger.LogInformation("--permissions is mandatory with --fields.include and --fields.exclude.");
                    return false;
                }

                if (options.PolicyRequest is not null || options.PolicyDatabase is not null)
                {
                    _logger.LogInformation("--permissions is mandatory with --policy-request and --policy-database.");
                    return false;
                }

                if (updatedSourceType is EntitySourceType.StoredProcedure &&
                    !VerifyPermissionOperationsForStoredProcedures(entity.Permissions))
                {
                    return false;
                }
            }

            if (options.Relationship is not null)
            {
                if (!VerifyCanUpdateRelationship(initialConfig, options.Cardinality, options.TargetEntity))
                {
                    return false;
                }

                if (updatedRelationships is null)
                {
                    updatedRelationships = new();
                }

                EntityRelationship? new_relationship = CreateNewRelationshipWithUpdateOptions(options);
                if (new_relationship is null)
                {
                    return false;
                }

                updatedRelationships[options.Relationship] = new_relationship;
            }

            if (options.Map is not null && options.Map.Any())
            {
                // Parsing mappings dictionary from Collection
                if (!TryParseMappingDictionary(options.Map, out updatedMappings))
                {
                    return false;
                }
            }

            Entity updatedEntity = new(
                Source: updatedSource,
                Rest: updatedRestDetails,
                GraphQL: updatedGraphQLDetails,
                Permissions: updatedPermissions,
                Relationships: updatedRelationships,
                Mappings: updatedMappings);
            IDictionary<string, Entity> entities = new Dictionary<string, Entity>(initialConfig.Entities.Entities)
            {
                [options.Entity] = updatedEntity
            };
            updatedConfig = initialConfig with { Entities = new(new ReadOnlyDictionary<string, Entity>(entities)) };
            return true;
        }