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 })
}
}
})
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;
};
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. 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’. 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. Thanks.