This tutorial describes how to connect to a PostgreSQL database using Flexmonster Data Server – a special server developed by Flexmonster. This server communicates with the client using the custom data source API – our custom communication protocol allowing you to retrieve already aggregated data from a server to Flexmonster Pivot.
To download the Data Server, you will need Flexmonster CLI — a command-line interface tool for Flexmonster. If needed, install the CLI globally using npm:
npm install -g flexmonster-cli
After that, a new flexmonster
command will be available in the console. Learn more about Flexmonster CLI and its commands in our documentation.
Now follow the steps below to connect to PostgreSQL using the Data Server.
If Flexmonster is not yet embedded, set up an empty component in your web page:
Complete the Quick start guide. Your code should look similar to the following example:
var pivot = new Flexmonster({
container: "pivotContainer",
toolbar: true
});
Complete the Integration with Angular guide. Your code should look similar to the following example:
<fm-pivot
[toolbar]="true">
</fm-pivot>
Complete the Integration with React guide. Your code should look similar to the following example:
<FlexmonsterReact.Pivot
toolbar={true}
/>
Complete the Integration with Vue guide. Your code should look similar to the following example:
<Pivot
ref="pivot"
toolbar>
</Pivot>
The previous step demonstrated how to configure Flexmonster Pivot. Now it’s time to set up Flexmonster Data Server.
Get the Data Server with the following CLI command:
flexmonster add fds
The flexmonster add fds
command will download the .zip
archive with Flexmonster Data Server and automatically unpack the files in the current folder. As a result, the flexmonster-data-server/
folder will appear in your working directory.
The Data Server can be configured in the flexmonster-data-server/flexmonster-config.json
file. To learn more about all the available configurations, see the configurations reference.
To set configurations needed for a PostgreSQL database, follow the steps below.
Set the "Type"
property in flexmonster-config.json
to "database"
. It should be done as follows:
"DataSources": [
{
"Type": "database"
}
],
Specify the name of the database you want to use by adding the "DatabaseType"
configuration to the flexmonster-config.json
file. In this case, the configuration value should be "postgresql"
:
"DataSources": [
{
"Type": "database",
"DatabaseType": "postgresql"
}
]
To enable the server to fetch data from your database, you have to provide the connection string to the database. The connection string should be added to flexmonster-config.json
as follows:
"DataSources": [
{
"Type": "database",
"DatabaseType": "postgresql",
"ConnectionString":
"Server=localhost;Port=5432;Uid=root;Pwd=password;Database=database_name"
}
]
Have a look at the examples of different connection strings for PostgreSQL.
In the flexmonster-config.json
file, create an index for the subset of data you want Flexmonster Pivot to visualize. "Query"
is an SQL query for the data. For example:
"DataSources": [
{
"Type": "database",
"DatabaseType": "postgresql",
"ConnectionString":
"Server=localhost;Port=5432;Uid=root;Pwd=password;Database=database_name",
"Indexes": {
"index_database": {
"Query": "SELECT * FROM tablename"
}
}
}
]
"index_database"
is a dataset identifier. It will be used to configure the data source on the client side.
In a similar way, additional indexes can be specified:
"Indexes": {
"index_database": {
"Query": "SELECT * FROM tablename"
},
"another_index_database": {
"Query": "SELECT column FROM tablename"
}
}
To start the Data Server, run the following command in the console:
flexmonster-data-server.exe
./flexmonster-data-server
As soon as you start Flexmonster Data Server, it automatically preloads the data specified in the "Indexes"
property. Thus, when Flexmonster Pivot requests the data, the server responds with already preloaded data.
The Data Server keeps preloaded data in the server’s RAM, so the number of indexes you can specify is limited by the server’s RAM amount.
On the client side, the report should be configured as follows:
var pivot = new Flexmonster({
container: "pivotContainer",
toolbar: true,
report: {
dataSource: {
type: "api",
url: "http://localhost:9500",
index: "index-database"
}
}
});
index
must match the name of the index defined in step 6 (e.g., "index_database"
).
When Flexmonster Pivot requests the data, Flexmonster Data Server server sends the response and then caches it. In case the component sends the same request once again, the server responds with the data from its cache. The Data Server clears the cache when restarted.
The Data Server’s cache has a memory limit. When the cache does not have enough memory for a new response, the Data Server server deletes one of the previously cached responses.
You can manage the cache size via the “CacheSizeLimit” property.
If any errors appear when trying to connect to PostgreSQL, refer to the troubleshooting section.
In case of connection to the secure PostgreSQL instance, some additional configurations are needed. Learn more details in this forum thread.
You may be interested in the following articles: