How dynamic connection string is achievable in JS based embedding
Dynamic connection strings can be particularly useful in scenarios where there are multiple databases, and the user needs to switch between them based on certain conditions. Here are two primary use cases:
Case 1: Multiple Databases Without User Added to Bold BI Server
If there are multiple databases and the user is not added to the Bold BI server, it is possible to switch the connection string dynamically based on the user’s identity. This can be achieved using an external web API.
Case 2: Multiple Databases with User Added to Bold BI Server
If there are multiple databases and the user is added to the Bold BI server, it is possible to add a custom attribute to each user that specifies a different database. This custom attribute can then be used to dynamically switch the connection string.
Real-World Example
Consider a scenario where a company has multiple regional databases, and each user is associated with a specific region. When a user logs in, the system needs to connect to the database corresponding to their region. By using dynamic connection strings, the application can fetch the appropriate connection string based on the user’s identity and connect to the correct database.
For instance, if a user from the “North America” region logs in, the application will fetch the connection string for the North America database and use it to embed the Bold BI dashboard. Similarly, if a user from the “Europe” region logs in, the application will fetch the connection string for the Europe database.
Achieving Dynamic Connection Strings in JS-Based Embedding Using External API
To achieve dynamic connection strings in JavaScript-based embedding, you can use an external API to fetch the appropriate connection string based on the user’s identity. Below is an example code snippet demonstrating this approach:
function initializeBoldBIEmbedding(connectionString) {
// Your code to initialize Bold BI embedding with the provided connection string
BoldBI.create({
serverUrl: "https://your-boldbi-server.com",
dashboardId: "your-dashboard-id",
dynamicConnection: {
isEnabled: true,
identity: "test1@test.com",
}
});
}
// Example code to fetch dynamic connection string based on user identity
public ApiResponse GetConnectionDetails()
{
try
{
var customIdentity = HttpContext.Current.Request.Headers["customIdentity"];
var identityType = HttpContext.Current.Request.Headers["identityType"];
if (!string.IsNullOrEmpty(customIdentity))
{
if (identityType.ToString().ToLowerInvariant().Equals("custom") || identityType.ToString().ToLowerInvariant().Equals("email") || identityType.ToString().ToLowerInvariant().Equals("full name"))
{
if ( customIdentity.ToString().ToLowerInvariant().Equals("test1@test.com") || customIdentity.ToString().ToLowerInvariant().Equals("user1"))
{
return new ApiResponse
{
Status = true, Message = "Success", Data = new
{
DataSource = "EuropeDBSource",InitialCatalog = "EuropeDB", Username = "usernameEU",Password = "passwordEU"
}
};
}
else if ( customIdentity.ToString().ToLowerInvariant().Equals("test1@test.com") || customIdentity.ToString().ToLowerInvariant().Equals("user1"))
{
return new ApiResponse
{
Status = true, Message = "Success", Data = new
{
DataSource = "NorthAmericaDBSource",InitialCatalog = "NorthAmericaDB", Username = "usernameNA", Password = "passwordNA",
}
};
}
}
}
}
}
For more details refer the Dynamic connection string sample
Conclusion
Dynamic connection strings provide a flexible way to manage multiple databases in JavaScript-based embedding scenarios. By using external APIs and custom attributes, it is possible to switch connection strings based on user identity, ensuring that the correct database is accessed for each user.