@@ -25,7 +25,7 @@ public record RuntimeConfig
2525 [ JsonPropertyName ( "azure-key-vault" ) ]
2626 public AzureKeyVaultOptions ? AzureKeyVault { get ; init ; }
2727
28- public RuntimeAutoentities ? Autoentities { get ; init ; }
28+ public RuntimeAutoentities Autoentities { get ; init ; }
2929
3030 public virtual RuntimeEntities Entities { get ; init ; }
3131
@@ -216,6 +216,8 @@ Runtime.GraphQL.FeatureFlags is not null &&
216216
217217 private Dictionary < string , string > _entityNameToDataSourceName = new ( ) ;
218218
219+ private Dictionary < string , string > _autoentityNameToDataSourceName = new ( ) ;
220+
219221 private Dictionary < string , string > _entityPathNameToEntityName = new ( ) ;
220222
221223 /// <summary>
@@ -245,6 +247,21 @@ public bool TryGetEntityNameFromPath(string entityPathName, [NotNullWhen(true)]
245247 return _entityPathNameToEntityName . TryGetValue ( entityPathName , out entityName ) ;
246248 }
247249
250+ public bool TryAddEntityNameToDataSourceName ( string entityName )
251+ {
252+ return _entityNameToDataSourceName . TryAdd ( entityName , this . DefaultDataSourceName ) ;
253+ }
254+
255+ public bool TryAddGeneratedAutoentityNameToDataSourceName ( string entityName , string autoEntityDefinition )
256+ {
257+ if ( _autoentityNameToDataSourceName . TryGetValue ( autoEntityDefinition , out string ? dataSourceName ) )
258+ {
259+ return _entityNameToDataSourceName . TryAdd ( entityName , dataSourceName ) ;
260+ }
261+
262+ return false ;
263+ }
264+
248265 /// <summary>
249266 /// Constructor for runtimeConfig.
250267 /// To be used when setting up from cli json scenario.
@@ -268,8 +285,8 @@ public RuntimeConfig(
268285 this . DataSource = DataSource ;
269286 this . Runtime = Runtime ;
270287 this . AzureKeyVault = AzureKeyVault ;
271- this . Entities = Entities ;
272- this . Autoentities = Autoentities ;
288+ this . Entities = Entities ?? new RuntimeEntities ( new Dictionary < string , Entity > ( ) ) ;
289+ this . Autoentities = Autoentities ?? new RuntimeAutoentities ( new Dictionary < string , Autoentity > ( ) ) ;
273290 this . DefaultDataSourceName = Guid . NewGuid ( ) . ToString ( ) ;
274291
275292 if ( this . DataSource is null )
@@ -287,25 +304,38 @@ public RuntimeConfig(
287304 } ;
288305
289306 _entityNameToDataSourceName = new Dictionary < string , string > ( ) ;
290- if ( Entities is null )
307+ if ( Entities is null && this . Entities . Entities . Count == 0 &&
308+ Autoentities is null && this . Autoentities . Autoentities . Count == 0 )
291309 {
292310 throw new DataApiBuilderException (
293- message : "entities is a mandatory property in DAB Config " ,
311+ message : "Configuration file should contain either at least the entities or autoentities property " ,
294312 statusCode : HttpStatusCode . UnprocessableEntity ,
295313 subStatusCode : DataApiBuilderException . SubStatusCodes . ConfigValidationError ) ;
296314 }
297315
298- foreach ( KeyValuePair < string , Entity > entity in Entities )
316+ if ( Entities is not null )
317+ {
318+ foreach ( KeyValuePair < string , Entity > entity in Entities )
319+ {
320+ _entityNameToDataSourceName . TryAdd ( entity . Key , this . DefaultDataSourceName ) ;
321+ }
322+ }
323+
324+ if ( Autoentities is not null )
299325 {
300- _entityNameToDataSourceName . TryAdd ( entity . Key , this . DefaultDataSourceName ) ;
326+ foreach ( KeyValuePair < string , Autoentity > autoentity in Autoentities )
327+ {
328+ _autoentityNameToDataSourceName . TryAdd ( autoentity . Key , this . DefaultDataSourceName ) ;
329+ }
301330 }
302331
303332 // Process data source and entities information for each database in multiple database scenario.
304333 this . DataSourceFiles = DataSourceFiles ;
305334
306335 if ( DataSourceFiles is not null && DataSourceFiles . SourceFiles is not null )
307336 {
308- IEnumerable < KeyValuePair < string , Entity > > allEntities = Entities . AsEnumerable ( ) ;
337+ IEnumerable < KeyValuePair < string , Entity > > ? allEntities = Entities ? . AsEnumerable ( ) ;
338+ IEnumerable < KeyValuePair < string , Autoentity > > ? allAutoentities = Autoentities ? . AsEnumerable ( ) ;
309339 // Iterate through all the datasource files and load the config.
310340 IFileSystem fileSystem = new FileSystem ( ) ;
311341 // This loader is not used as a part of hot reload and therefore does not need a handler.
@@ -322,7 +352,9 @@ public RuntimeConfig(
322352 {
323353 _dataSourceNameToDataSource = _dataSourceNameToDataSource . Concat ( config . _dataSourceNameToDataSource ) . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
324354 _entityNameToDataSourceName = _entityNameToDataSourceName . Concat ( config . _entityNameToDataSourceName ) . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
325- allEntities = allEntities . Concat ( config . Entities . AsEnumerable ( ) ) ;
355+ _autoentityNameToDataSourceName = _autoentityNameToDataSourceName . Concat ( config . _autoentityNameToDataSourceName ) . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
356+ allEntities = allEntities ? . Concat ( config . Entities . AsEnumerable ( ) ) ;
357+ allAutoentities = allAutoentities ? . Concat ( config . Autoentities . AsEnumerable ( ) ) ;
326358 }
327359 catch ( Exception e )
328360 {
@@ -336,7 +368,8 @@ public RuntimeConfig(
336368 }
337369 }
338370
339- this . Entities = new RuntimeEntities ( allEntities . ToDictionary ( x => x . Key , x => x . Value ) ) ;
371+ this . Entities = new RuntimeEntities ( allEntities != null ? allEntities . ToDictionary ( x => x . Key , x => x . Value ) : new Dictionary < string , Entity > ( ) ) ;
372+ this . Autoentities = new RuntimeAutoentities ( allAutoentities != null ? allAutoentities . ToDictionary ( x => x . Key , x => x . Value ) : new Dictionary < string , Autoentity > ( ) ) ;
340373 }
341374
342375 SetupDataSourcesUsed ( ) ;
@@ -351,17 +384,19 @@ public RuntimeConfig(
351384 /// <param name="DataSource">Default datasource.</param>
352385 /// <param name="Runtime">Runtime settings.</param>
353386 /// <param name="Entities">Entities</param>
387+ /// <param name="Autoentities">Autoentities</param>
354388 /// <param name="DataSourceFiles">List of datasource files for multiple db scenario.Null for single db scenario.
355389 /// <param name="DefaultDataSourceName">DefaultDataSourceName to maintain backward compatibility.</param>
356390 /// <param name="DataSourceNameToDataSource">Dictionary mapping datasourceName to datasource object.</param>
357391 /// <param name="EntityNameToDataSourceName">Dictionary mapping entityName to datasourceName.</param>
358392 /// <param name="DataSourceFiles">Datasource files which represent list of child runtimeconfigs for multi-db scenario.</param>
359- public RuntimeConfig ( string Schema , DataSource DataSource , RuntimeOptions Runtime , RuntimeEntities Entities , string DefaultDataSourceName , Dictionary < string , DataSource > DataSourceNameToDataSource , Dictionary < string , string > EntityNameToDataSourceName , DataSourceFiles ? DataSourceFiles = null , AzureKeyVaultOptions ? AzureKeyVault = null )
393+ public RuntimeConfig ( string Schema , DataSource DataSource , RuntimeOptions Runtime , RuntimeEntities Entities , string DefaultDataSourceName , Dictionary < string , DataSource > DataSourceNameToDataSource , Dictionary < string , string > EntityNameToDataSourceName , DataSourceFiles ? DataSourceFiles = null , AzureKeyVaultOptions ? AzureKeyVault = null , RuntimeAutoentities ? Autoentities = null )
360394 {
361395 this . Schema = Schema ;
362396 this . DataSource = DataSource ;
363397 this . Runtime = Runtime ;
364398 this . Entities = Entities ;
399+ this . Autoentities = Autoentities ?? new RuntimeAutoentities ( new Dictionary < string , Autoentity > ( ) ) ;
365400 this . DefaultDataSourceName = DefaultDataSourceName ;
366401 _dataSourceNameToDataSource = DataSourceNameToDataSource ;
367402 _entityNameToDataSourceName = EntityNameToDataSourceName ;
@@ -451,6 +486,24 @@ public DataSource GetDataSourceFromEntityName(string entityName)
451486 return _dataSourceNameToDataSource [ _entityNameToDataSourceName [ entityName ] ] ;
452487 }
453488
489+ /// <summary>
490+ /// Gets datasourceName from AutoentityNameToDatasourceName dictionary.
491+ /// </summary>
492+ /// <param name="autoentityName">autoentityName</param>
493+ /// <returns>DataSourceName</returns>
494+ public string GetDataSourceNameFromAutoentityName ( string autoentityName )
495+ {
496+ if ( ! _autoentityNameToDataSourceName . TryGetValue ( autoentityName , out string ? autoentityDataSource ) )
497+ {
498+ throw new DataApiBuilderException (
499+ message : $ "{ autoentityName } is not a valid autoentity.",
500+ statusCode : HttpStatusCode . NotFound ,
501+ subStatusCode : DataApiBuilderException . SubStatusCodes . EntityNotFound ) ;
502+ }
503+
504+ return autoentityDataSource ;
505+ }
506+
454507 /// <summary>
455508 /// Validates if datasource is present in runtimeConfig.
456509 /// </summary>
0 commit comments