in src/Cli/ConfigGenerator.cs [360:453]
public static bool TryAddNewEntity(AddOptions options, RuntimeConfig initialRuntimeConfig, out RuntimeConfig updatedRuntimeConfig)
{
updatedRuntimeConfig = initialRuntimeConfig;
// If entity exists, we cannot add. Display warning
//
if (initialRuntimeConfig.Entities.ContainsKey(options.Entity))
{
_logger.LogWarning("Entity '{entityName}' is already present. No new changes are added to Config.", options.Entity);
return false;
}
// Try to get the source object as string or DatabaseObjectSource for new Entity
if (!TryCreateSourceObjectForNewEntity(
options,
initialRuntimeConfig.DataSource.DatabaseType == DatabaseType.CosmosDB_NoSQL,
out EntitySource? source))
{
_logger.LogError("Unable to create the source object.");
return false;
}
EntityActionPolicy? policy = GetPolicyForOperation(options.PolicyRequest, options.PolicyDatabase);
EntityActionFields? field = GetFieldsForOperation(options.FieldsToInclude, options.FieldsToExclude);
EntityPermission[]? permissionSettings = ParsePermission(options.Permissions, policy, field, source.Type);
if (permissionSettings is null)
{
_logger.LogError("Please add permission in the following format. --permissions \"<<role>>:<<actions>>\"");
return false;
}
bool isStoredProcedure = IsStoredProcedure(options);
// Validations to ensure that REST methods and GraphQL operations can be configured only
// for stored procedures
if (options.GraphQLOperationForStoredProcedure is not null && !isStoredProcedure)
{
_logger.LogError("--graphql.operation can be configured only for stored procedures.");
return false;
}
if ((options.RestMethodsForStoredProcedure is not null && options.RestMethodsForStoredProcedure.Any())
&& !isStoredProcedure)
{
_logger.LogError("--rest.methods can be configured only for stored procedures.");
return false;
}
GraphQLOperation? graphQLOperationsForStoredProcedures = null;
SupportedHttpVerb[]? SupportedRestMethods = null;
if (isStoredProcedure)
{
if (CheckConflictingGraphQLConfigurationForStoredProcedures(options))
{
_logger.LogError("Conflicting GraphQL configurations found.");
return false;
}
if (!TryAddGraphQLOperationForStoredProcedure(options, out graphQLOperationsForStoredProcedures))
{
return false;
}
if (CheckConflictingRestConfigurationForStoredProcedures(options))
{
_logger.LogError("Conflicting Rest configurations found.");
return false;
}
if (!TryAddSupportedRestMethodsForStoredProcedure(options, out SupportedRestMethods))
{
return false;
}
}
EntityRestOptions restOptions = ConstructRestOptions(options.RestRoute, SupportedRestMethods, initialRuntimeConfig.DataSource.DatabaseType == DatabaseType.CosmosDB_NoSQL);
EntityGraphQLOptions graphqlOptions = ConstructGraphQLTypeDetails(options.GraphQLType, graphQLOperationsForStoredProcedures);
// Create new entity.
Entity entity = new(
Source: source,
Rest: restOptions,
GraphQL: graphqlOptions,
Permissions: permissionSettings,
Relationships: null,
Mappings: null);
// Add entity to existing runtime config.
IDictionary<string, Entity> entities = new Dictionary<string, Entity>(initialRuntimeConfig.Entities.Entities)
{
{ options.Entity, entity }
};
updatedRuntimeConfig = initialRuntimeConfig with { Entities = new(new ReadOnlyDictionary<string, Entity>(entities)) };
return true;
}