In our previous guide, we launched our sample GitHub project demonstrating the basics of using Flexmonster MongoDB Connector. The information from that guide is considered a prerequisite for this tutorial, it is highly recommended that you complete the previous guide before starting this one.
This tutorial describes how to embed the Connector into a server.
To fetch data from a database and pass it to the component, the Connector needs a mediator between itself and Flexmonster. Your server acts as this mediator and its task is to handle Flexmonster’s requests and to pass them to the Connector in the correct format. The server has to be configured properly to complete this task.
All requests have the type
property in the request body. There are 4 types of requests that can be distinguished by the URL path and type
value:
<url>/handshake
– The first (handshake) request to establish a connection between the client and server.<url>/fields
– Request for all fields with their types (i.e., meta-object or schema).<url>/members
– Request for all members of a field.<url>/select
– Request for the data.The value of type
will always be the same as the endpoint name, e.g., when a request is sent to <url>/fields
, the value of type is "fields"
.
The Connector has three methods to handle /fields, /members, and /select requests. See the documentation to learn more about these methods.
The following guide will walk you through the process of server configuration.
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>
In the next steps, we will start configuring the server.
Now we will focus on the server part of our project.
First, let’s create a simple server using Express.js. If you already have a server, jump to Step 3. Create a separate module for the request routing.
Follow the steps below to get a simple server application:
Step 2.1. Create a folder for the server and run the npm init
command there to generate the package.json
file.
Step 2.2. The server needs the express
, cors
, and body-parser
packages. Install them with npm:
npm install express
npm install cors
npm install body-parser
Step 2.3. Then, create a simple server in a .js
file (e.g., server.js
):
const express = require("express");
var cors = require('cors');
var bodyParser = require('body-parser');
const app = express();
app.use(cors());
app.use(bodyParser.json());
var port = 9204;
app.listen(port, () => {
console.log(Example app listening at http://localhost:${port}
)
});
All the request routing will be implemented in a separate .js
file (e.g., mongo.js
).
At this step, include the .js
file (i.e., mongo.js
) into your server. All the requests coming to <url>
will be handled by this separate module (in this case, the <url>/mongo
path is used):
app.use('/mongo', require('./mongo.js'));
Now, all the requests sent to <url>
(in this case,<url>/mongo
) will be processed by the mongo
module.
However, since we haven’t yet defined the mongo.js
file properly, the code won’t work just yet.
If Flexmonster Pivot Table is running on a different server, enable CORS.
Install Flexmonster MongoDB Connector and the MongoDB driver:
npm install flexmonster-mongo-connector
npm install mongodb
Now it’s time to configure the mongo.js
module.
Configure the mongo.js
module in three steps:
Step 5.1. In the JavaScript file mentioned in the previous step (i.e., mongo.js
), include the following libraries:
const mongo = require('express').Router();
const mongodb = require("mongodb");
const fm_mongo_connector = require("flexmonster-mongo-connector");
Step 5.2. Then set up a connection with your MongoDB database and define the Connector:
let dbo = null;
let _apiReference = null; // it’ll be the Connector instance
mongodb.MongoClient.connect("mongodb://read:only@olap.flexmonster.com:27017", {
useNewUrlParser: true
}, (err, db) => {
if (err)
throw err;
dbo = db.db("flexmonster");
_apiReference = new fm_mongo_connector.MongoDataAPI();
});
Step 5.3. Export the mongo
module so the server can use it:
// requests handling functions will appear here
module.exports = mongo;
This module can now connect to your MongoDB database, but it does not handle the Flexmonster Pivot requests. In the next steps, we will handle all Flexmonster requests.
The /handshake request establishes a connection between the client and server sides. If the server sends the implemented version of the custom data source API in response to the /handshake request, the client can check whether the server and the client implement the same version of the custom data source API.
To receive notifications about version compatibility, respond to the /handshake request with the implemented version of the custom data source API:
mongo.post("/handshake", async (req, res) => {
try {
res.json({ version: _apiReference.API_VERSION });
} catch (err) {
handleError(err, res);
}
});
Handing the /handshake request is optional. Flexmonster Pivot will proceed to the next request even if the server does not handle it.
The next request that needs to be handled is a /fields request; it is sent to <url>/fields
. It can be handled like this:
mongo.post("/fields", async (req, res) => {
try {
const result = await _apiReference.getSchema(dbo, req.body.index);
res.json(result);
} catch (err) {
//your error handler
}
});
When Flexmonster Pivot successfully receives a response to the /fields request, it shows the Field List with all of the available fields.
To learn more about the getSchema
method, see our documentation.
The next request to handle is the request for the field’s members that is sent to <url>/members
. It can be handled like this:
mongo.post("/members", async (req, res) => {
try {
const result = await _apiReference.getMembers(
dbo, req.body.index, req.body.field,
{ page: req.body.page, pageToken: req.body.pageToken });
res.json(result);
} catch (err) {
//your error handler
}
});
When Flexmonster Pivot successfully receives the response to this request, you will be able to select a string field for rows or for columns and retrieve its members.
To learn more about the getMembers
method, see our documentation.
When a field is selected for rows and/or columns and a numeric field is selected for measures in the Field List, the /select request is sent to the endpoint <url>/select
. It can be handled like this:
mongo.post("/select", async (req, res) => {
try {
const result = await _apiReference.getSelectResult(
dbo, req.body.index, req.body.query,
{ page: req.body.page, pageToken: req.body.pageToken });
res.json(result);
} catch (err) {
//your error handler
}
});
When Flexmonster successfully receives the response to this kind of /select request, a pivot table with the received data is shown.
To learn more about the getSelectResult
method, see our documentation.
Run your server with the following command:
node server.js
Now it’s time to configure the pivot table on the web page. In report.dataSource
, define these parameters to connect to your server:
var pivot = new Flexmonster({
container: "pivotContainer",
toolbar: true,
report: {
dataSource: {
type: "api",
url: "<url>",
index: "your-collection-name"
}
}
});
Here, url
is the path to your API endpoints (e.g., "http://localhost:9204/mongo"
), and index
is your collection’s name. index
will be sent with every request.
Open your HTML page in the browser to see the result – the pivot table with data from your MongoDB database is shown.