Skip to content

Commit 2902ccb

Browse files
Helper methods to expose field mappings (#1918)
## Why make this change? For customers consuming dab, it maybe useful to store field mappings internally and surface them to their end users. Currently these are private dictionaries and hence need to be regenerated if wanting to show on consumer side. ## What is this change? Exposing the dictionaries publicly through readonlydictionary. ## How was this tested? Existing unit tests should cover all scenarios. --------- Co-authored-by: Abhishek Kumar <[email protected]>
1 parent 2820516 commit 2902ccb

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/Core/Services/MetadataProviders/CosmosSqlMetadataProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public bool TryGetBackingColumn(string entityName, string field, [NotNullWhen(tr
276276
return true;
277277
}
278278

279-
public IDictionary<string, DatabaseObject> GetEntityNamesAndDbObjects()
279+
public IReadOnlyDictionary<string, DatabaseObject> GetEntityNamesAndDbObjects()
280280
{
281281
throw new NotImplementedException();
282282
}

src/Core/Services/MetadataProviders/ISqlMetadataProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ bool VerifyForeignKeyExistsInDB(
121121
/// Returns a dictionary of (EntityName, DatabaseObject).
122122
/// </summary>
123123
/// <returns></returns>
124-
public IDictionary<string, DatabaseObject> GetEntityNamesAndDbObjects();
124+
public IReadOnlyDictionary<string, DatabaseObject> GetEntityNamesAndDbObjects();
125125

126126
/// <summary>
127127
/// Gets Partition Key Path of a database container.

src/Core/Services/MetadataProviders/SqlMetadataProvider.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public virtual bool TryGetEntityNameFromPath(string entityPathName, [NotNullWhen
222222
}
223223

224224
/// <inheritdoc />
225-
public IDictionary<string, DatabaseObject> GetEntityNamesAndDbObjects()
225+
public IReadOnlyDictionary<string, DatabaseObject> GetEntityNamesAndDbObjects()
226226
{
227227
return EntityToDatabaseObject;
228228
}
@@ -284,6 +284,38 @@ public async Task InitializeAsync()
284284
_logger.LogTrace($"Done inferring Sql database schema in {timer.ElapsedMilliseconds}ms.");
285285
}
286286

287+
/// <summary>
288+
/// Given entity name, gets the entity to column mappings if present.
289+
/// </summary>
290+
public bool TryGetEntityToColumnMappings(string entityName, [NotNullWhen(true)] out IReadOnlyDictionary<string, string>? mappings)
291+
{
292+
Dictionary<string, string>? entityToColumnMappings;
293+
mappings = null;
294+
if (EntityExposedNamesToBackingColumnNames.TryGetValue(entityName, out entityToColumnMappings))
295+
{
296+
mappings = entityToColumnMappings;
297+
return true;
298+
}
299+
300+
return false;
301+
}
302+
303+
/// <summary>
304+
/// Given entity name, gets the column to entity mappings if present.
305+
/// </summary>
306+
public bool TryGetColumnToEntityMappings(string entityName, [NotNullWhen(true)] out IReadOnlyDictionary<string, string>? mappings)
307+
{
308+
Dictionary<string, string>? columntoEntityMappings;
309+
mappings = null;
310+
if (EntityBackingColumnsToExposedNames.TryGetValue(entityName, out columntoEntityMappings))
311+
{
312+
mappings = columntoEntityMappings;
313+
return true;
314+
}
315+
316+
return false;
317+
}
318+
287319
/// <summary>
288320
/// Log Primary key information. Function only called when not
289321
/// in a hosted scenario. Log relevant information about Primary keys

src/Service.Tests/Unittests/SqlMetadataProviderUnitTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,30 @@ public async Task CheckCorrectParsingForStoredProcedure()
234234
TestHelper.UnsetAllDABEnvironmentVariables();
235235
}
236236

237+
/// <summary>
238+
/// <code>Do: </code> Load runtimeConfig and set up the source fields for the entities.
239+
/// <code>Check: </code> Verifies that source object is correctly parsed.
240+
/// </summary>
241+
[TestMethod, TestCategory(TestCategory.MSSQL)]
242+
public async Task CheckGetFieldMappings()
243+
{
244+
DatabaseEngine = TestCategory.MSSQL;
245+
TestHelper.SetupDatabaseEnvironment(DatabaseEngine);
246+
RuntimeConfig runtimeConfig = SqlTestHelper.SetupRuntimeConfig();
247+
RuntimeConfigProvider runtimeConfigProvider = TestHelper.GenerateInMemoryRuntimeConfigProvider(runtimeConfig);
248+
SetUpSQLMetadataProvider(runtimeConfigProvider);
249+
250+
await _sqlMetadataProvider.InitializeAsync();
251+
252+
MsSqlMetadataProvider metadataProvider = (MsSqlMetadataProvider)_sqlMetadataProvider;
253+
Assert.IsFalse(metadataProvider.TryGetColumnToEntityMappings("InvalidEntity", out _), "Column to entity mappings should not exist for invalid entity.");
254+
Assert.IsFalse(metadataProvider.TryGetEntityToColumnMappings("invalidEntity", out _), "Entity to column mappings should not exist for invalid entity.");
255+
Assert.IsTrue(metadataProvider.TryGetEntityToColumnMappings("Publisher", out IReadOnlyDictionary<string, string> _), "Entity to column mappings should exist for valid entity.");
256+
Assert.IsTrue(metadataProvider.TryGetColumnToEntityMappings("Publisher", out IReadOnlyDictionary<string, string> _), "Column to entity mappings should exist for valid entity.");
257+
258+
TestHelper.UnsetAllDABEnvironmentVariables();
259+
}
260+
237261
[DataTestMethod, TestCategory(TestCategory.MSSQL)]
238262
[DataRow("/mygql", "/graphql", true, DisplayName = "Entity Rest path conflicts with default path /graphql")]
239263
[DataRow("/mygql", "/mygql", true, DisplayName = "Entity Rest path conflicts with configured GraphQL path")]

0 commit comments

Comments
 (0)