Ask a question about custom data sources

Stimulsoft Reports.JS discussion
Post Reply
huangguangc
Posts: 6
Joined: Wed Jul 05, 2023 1:22 am

Ask a question about custom data sources

Post by huangguangc »

version: 2024.3.1

We have embedded Stimulsoft Reports.JS in our system as a web-based reporting tool, but at the same time, we don't want to expose the database information, such as ip, account and password. Therefore, we would like to use the ‘Custom Database’ feature to achieve this.

This is our key front-end code:
The reason I don't use origin mysql adapters on the front end is that I can't bring the token on header when I initiate a request.

Code: Select all

import { Stimulsoft, StiOptions } from 'stimulsoft-reports-js/Scripts/stimulsoft.blockly.editor'
import axios from 'axios'
import { encode, decode } from 'js-base64'

// ... other code

StiOptions.WebServer.url = 'http://192.168.100.78:9615'
Stimulsoft.Report.Dictionary.StiCustomDatabase.registerCustomDatabase({
  serviceName: 'MPHDB',
  designerCategory: 'SQL',
  sampleConnectionString: ' ',
  process: async(command, callback) => {
    try {
      const requestStr = encode(JSON.stringify(command)).replace(/[A-Za-z]/g, (c) => {
        return String.fromCharCode(c.charCodeAt(0) + (c.toUpperCase() <= 'M' ? 13 : -13))
      })
      const response = await axios.post('http://192.168.100.78:9615/', requestStr, {
        headers: {
          'Content-Type': 'text/plain',
          'Token': '123456'
        },
        responseType: 'text'
      })
      callback(JSON.parse(decode(response.data.replace(/[A-Za-z]/g, (c) => {
        return String.fromCharCode(c.charCodeAt(0) + (c.toUpperCase() <= 'M' ? 13 : -13))
      }))))
    } catch (error) {
      console.error(error)
      // eslint-disable-next-line n/no-callback-literal
      callback({ success: false, notice: error.message })
    }
  }
})
We modified the code based on the official Nodejs database adapter example by changing the logic for getting the connection parameters from the command to fixed values.

Code: Select all

// MySQLAdapter.js
var getConnectionStringInfo = function () {
     var info = { host: "192.168.100.32", port: "3306", charset: "utf8", database: 'test', userId: 'root', password: "123456" };
     return info;
};
We then changed the code in index.js to accommodate the front-end logic.

Code: Select all

// index.js
function process(command, onResult) {
    if (typeof command !== "object") command = getCommand(command);

    var onProcessHandler = onProcess.bind(null, onResult, command.encryptResult);

    if (command.command === "GetSupportedAdapters") {
        onProcessHandler({ success: true, types: ["MPHDB"] });
    } else {
        if (command.parameters) {
            command.parameters.forEach(parameter => {
                if (parameter.name.length > 1 && parameter.name[0] == "@") parameter.name = parameter.name.substring(1);
            })
        }

        if (command.database == "MPHDB") {
            var MySQLAdapter = require('./MySQLAdapter');
            MySQLAdapter.process(command, onProcessHandler);
        }
        else onProcessHandler({ success: false, notice: "Database '" + command.database + "' not supported!" });
    }
}

We then encountered the following problem when we actually ran it:

1. If I don't fill in the ‘Connection String’, there will be a Connection error: Connection string empty, is there any way to cancel the validation? Is there any way to cancel the validation? Or can I fill in some content by default.
2450.jpg
2450.jpg (6.76 KiB) Viewed 744 times
2. I can add a data source, but I can't execute the SQL statement. No network request is sent and no access is made to the ‘process’ function of the ‘custom database’.
1204.jpg
1204.jpg (91.21 KiB) Viewed 744 times
3. How do I hide recent connections, and unsupported databases? I was under the impression that older versions sent a request for supported databases when opening this screen, but the current version does not. My backend doesn't actually have support for databases like MS SQL, Oracle, etc. either.
32583.jpg
32583.jpg (72.55 KiB) Viewed 744 times
Thanks.
Lech Kulikowski
Posts: 7023
Joined: Tue Mar 20, 2018 5:34 am

Re: Ask a question about custom data sources

Post by Lech Kulikowski »

Hello,

Please send us a sample project that reproduces the issue for analysis.

3. You can use the following code

Code: Select all

let databases = StiOptions.Services.databases;
databases.clear();
databases.add(new Stimulsoft.Report.Dictionary.StiFirebirdDatabase());
//databases.add(new Stimulsoft.Report.Dictionary.StiMySqlDatabase());
//databases.add(new Stimulsoft.Report.Dictionary.StiSqlDatabase());
Thank you.
Post Reply