Skip to main content

Adding a PostgreSQL Data Source

breaking changes

Currently, the Reveal SDK is in the process of decoupling the data sources from the Reveal SDK core package. In order to ensure the project's continued functionality, you might be required to install additional packages into your project. Please see the Supported Data Sources topic for more information.

On the Client

Step 1 - Add an event handler for the RevealView.onDataSourcesRequested event.

var revealView = new $.ig.RevealView("#revealView");
revealView.onDataSourcesRequested = (callback) => {
//add code here
callback(new $.ig.RevealDataSources([], [], false));
};

Step 2 - In the RevealView.onDataSourcesRequested event handler, create a new instance of the RVPostgresDataSource object. Set the Host, Database, Port, and Title properties to values that correspond to your PostgreSQL server. After you have created the RVPostgresDataSource object, add it to the data sources collection.

revealView.onDataSourcesRequested = (callback) => {
var postgresDataSource = new $.ig.RVPostgresDataSource();
postgresDataSource.host = "your-db-host";
postgresDataSource.database = "your-db-name";
postgresDataSource.port = 1234;
postgresDataSource.title = "My PostgreSQL";

callback(new $.ig.RevealDataSources([postgresDataSource], [], false));
};

When the application runs, create a new Visualization and you will see the newly created PostgreSQL data source listed in the "Select a Data Source" dialog.

Step 3 - Add a new Data Source Item by creating a new instance of the RVPostgresDataSourceItem object. Set the Id,Title, and Table properties that correspond to your database table. After you have created the RVPostgresDataSourceItem object, add it to the data source items collection.

revealView.onDataSourcesRequested = (callback) => {
var postgresDataSource = new $.ig.RVPostgresDataSource();
postgresDataSource.host = "your-db-host";
postgresDataSource.database = "your-db-name";
postgresDataSource.port = 1234;
postgresDataSource.title = "My PostgreSQL";

var postgresDsi = new $.ig.RVPostgresDataSourceItem(postgresDataSource);
postgresDsi.id = "MyPostgresDataSourceItem";
postgresDsi.title = "My PostgreSQL Item";
postgresDsi.table = "TableName";

callback(new $.ig.RevealDataSources([postgresDataSource], [postgresDsi], false));
};

When the application runs, create a new Visualization and you will see the newly created PostgreSQL data source item listed in the "Select a Data Source" dialog.

On the Server

Step 1 - Create the data source and data source item on the client, but do not provide any connection information. Only provie an id, title, and/or subtitle.

var revealView = new $.ig.RevealView("#revealView");
revealView.onDataSourcesRequested = (callback) => {

var postgresDataSource = new $.ig.RVPostgresDataSource();
postgresDataSource.id = "MyPostgresDataSource";
postgresDataSource.title = "My PostgreSQL";

var postgresDsi = new $.ig.RVPostgresDataSourceItem(postgresDataSource);
postgresDsi.id = "MyPostgresDataSourceItem";
postgresDsi.title = "My PostgreSQL Item";

callback(new $.ig.RevealDataSources([postgresDataSource], [postgresDsi], false));
};

Step 2 - Create the data source provider. In this example, we are providing connection information to connect to our PostgreSQL database that was defined on the client. To achieve this, we determine the type of the data source/item we are working with, and set the available properties on the object.

public class DataSourceProvider : IRVDataSourceProvider
{
public Task<RVDataSourceItem> ChangeDataSourceItemAsync(IRVUserContext userContext, string dashboardId,
RVDataSourceItem dataSourceItem)
{
if (dataSourceItem is RVPostgresDataSourceItem postgresDataSourceItem)
{
//update underlying data source
ChangeDataSourceAsync(userContext, postgresDataSourceItem.DataSource);

//only change the table if we have selected our custom data source item
if (postgresDataSourceItem.Id == "MyPostgresDataSourceItem")
{
postgresDataSourceItem.Table = "orders";
}
}

return Task.FromResult(dataSourceItem);
}

public Task<RVDashboardDataSource> ChangeDataSourceAsync(IRVUserContext userContext,
RVDashboardDataSource dataSource)
{
if (dataSource is RVPostgresDataSource postgresDataSource)
{
postgresDataSource.Host = "localhost";
postgresDataSource.Database = "database";
postgresDataSource.Schema = "public";
}

return Task.FromResult(dataSource);
}
}
Get the Code

The source code to this sample can be found on GitHub