Database connection on server side

Stimulsoft Reports.JS discussion
Post Reply
Joanne
Posts: 23
Joined: Tue Dec 01, 2015 3:48 am

Database connection on server side

Post by Joanne »

Is it mandatory to use the mySQL or MSSQL adapters when trying to connect to a database on the server side (e.g. Node.js) ? Or is there another way to make the connection to the database on the server side ?
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Database connection on server side

Post by HighAley »

Hello Joanne,

Please try to add the following line of code in the first place. This method initializes the data adapters for Node.js

Stimulsoft.System.NodeJs.initialize();

Please let us know about the result.

Thank you.
Joanne
Posts: 23
Joined: Tue Dec 01, 2015 3:48 am

Re: Database connection on server side

Post by Joanne »

Hi, thanks for the reply. Do you have a full example of database connection and node.js + creating the report ?

What I am trying to achieve is the following:
- create a report template in the designer (with no database connection or data source attached - just the formatting, for e.g. columns layout for a simple list)
- then on node.js, create the database connection and data source, set it to the report template loaded and create the report (either saved on disk or returned as JSON string to the client side to be displayed in the viewer)

Is this possible ? I have not been able to achieve this, the result is always a blank report with the error "Cannot read property 'getType' of null" in the console log.

NOTE: I'm using the latest build - 2015.3

Thanks
Joanne
Posts: 23
Joined: Tue Dec 01, 2015 3:48 am

Re: Database connection on server side

Post by Joanne »

Hi, do you have guidelines for server side report creation and for database & datasource creation and linking to the report ?

I am desperately trying to make a simple report with a connection to a MySQL database work, but without any luck (using latest build 2015.3), if we go for Stimulsoft reports, it is very important for us to be able to set the connection information in the code itself.

When creating the report in designer with a MySQL connection and data source, it works well in the preview; the data is displayed. But when using same report and rendering on node.js server, nothing.... A blank report is returned and when looking at the ".mdc" file created, the database and data source information is not there....

The only error message displayed is "Cannot read property 'getType' of null" on the server console log.

I have attached the template file "empList.mrt" to this reply.

Please help.
Thanks

Below is my code on node.js server which returns the rendered report string to be viewed on client side in HTML viewer:

Code: Select all

var  connStr,
        ds,
        report,
        reportTemplate,
        renderedReport;

// added the following line based on response from support team - but does not seem to work...
    Stimulsoft.System.NodeJs.initialize();

// 1. create report object
    report = new Stimulsoft.Report.StiReport();
    console.log('report created');

// 2. load template
    reportTemplate = fs.readFileSync('./reportTemplates/empList.mrt', "utf8");
    report.load(reportTemplate);
    console.log('report loaded');
    console.log('pages: ' + report.pages.count);

// 3. change connection string
        // -- 3.1 set connection string
        report.dictionary.databases.clear();
        connStr = "UserID=root;Password=1234;Host=localhost;Port=3306;Database=testdb";
        report.dictionary.databases.add(new Stimulsoft.Report.Dictionary.StiMySqlDatabase("TestJS", "TestJS", connStr));

// added the following line based on Report.JS programming manual (Synchronize Data between DataStore and Dictionary) section        
        report.dictionary.synchronize();

        // -- 3.2 create data source
        ds = new Stimulsoft.Report.Dictionary.StiDataSource('shrm_employee', 'shrm_employee', 'shrm_employee');
        ds.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("employeeCode", "employeeCode", "employeeCode"));
        ds.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("employeeName", "employeeName", "employeeName"));
        ds.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("employeeSurname", "employeeSurname", "employeeSurname"));
        ds.sqlCommand = "SELECT * FROM shrm_employee WHERE scopeId = 2000;";
        report.dictionary.dataSources.add(ds);

        report.regData();

        report.renderAsync(function () {
            console.log('report rendered. pages: ' + report.renderedPages.count);

            renderedReport = report.saveDocumentToJsonString();
            fs.writeFileSync('./reportTemplates/empList.mdc', renderedReport);
            res.send({statusCode:200, message: "Report Created", reportData: renderedReport});
        });
Attachments
empList.mrt
Test template file
(3.4 KiB) Downloaded 315 times
Ivan
Posts: 960
Joined: Thu Aug 10, 2006 1:37 am

Re: Database connection on server side

Post by Ivan »

Hello,
The only error message displayed is "Cannot read property 'getType' of null" on the server console log.
From your code:

Code: Select all

        report.regData();
but the regData method has 3 parameters:

Code: Select all

public regData(name: string, alias: string, data: any)
Also, the regData method is used for direct data transfer (dataTable, dataSet, xml, json, etc).
But in your case the database is used, so calling this method is not needed.

Thank you.
Joanne
Posts: 23
Joined: Tue Dec 01, 2015 3:48 am

Re: Database connection on server side

Post by Joanne »

Hi, thanks for the reply. I have been able to set the database and data source connection on server side (node.js). Attached is the report template used for this example. The final code looks like below:

Code: Select all

var fs          = require('fs'),
    Stimulsoft  = require('stimulsoft-reports-js').Stimulsoft,
	connStr,
	ds, sqlQuery,
	report,
	reportTemplate,
	renderedReport,
	repPage, dtBand, dataText;

    // 1. create report object
    report = new Stimulsoft.Report.StiReport();
    console.log('report created');

    // 2. load template
    reportTemplate = fs.readFileSync('./empList.mrt', "utf8");
    report.load(reportTemplate);
    console.log('report loaded');
    console.log('pages: ' + report.pages.count);

    // 3. change connection string
	// -- 3.1 set connection string
	report.dictionary.databases.clear();
	connStr = "UserID=root;Password=123456;Host=localhost;Port=3306;Database=testdb";
	report.dictionary.databases.add(new Stimulsoft.Report.Dictionary.StiMySqlDatabase("LocalJS", "LocalJS", connStr));

	// -- 3.2 create data source
	sqlQuery = 'SELECT * FROM tb_employee';
	// StiMySqlSource(string nameInSource, string name, string alias, string sqlCommand, bool connectOnStart, bool reconnectOnEachRow, int commandTimeout, string key)
	ds = new Stimulsoft.Report.Dictionary.StiMySqlSource("LocalJS", 'tb_employee', 'tb_employee', sqlQuery, true);
	ds.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("employeeCode", "employeeCode", "employeeCode"));
	ds.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("employeeName", "employeeName", "employeeName"));
	ds.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("employeeSurname", "employeeSurname", "employeeSurname"));
	report.dictionary.dataSources.add(ds);

	// reset data band's data source
	repPage = report.pages.getByName('Page1');
	dtBand = repPage.components.getByName('db_employees');
	dtBand.dataSourceName = 'tb_employee';

	// reset text on data band
	// -- emp. code
	dataText = dtBand.components.getByName('txt_empCode');
	dataText.text = "{tb_employee.employeeCode}";

	// -- emp. Name
	dataText = dtBand.components.getByName('txt_empName');
	dataText.text = "{tb_employee.employeeName}";

	// -- emp. surname
	dataText = dtBand.components.getByName('txt_empSurname');
	dataText.text = "{tb_employee.employeeSurname}";

	report.dictionary.synchronize();

	report.renderAsync(function () {
		console.log('report rendered. pages: ' + report.renderedPages.count);

		renderedReport = report.saveDocumentToJsonString();
		// save as document file on server
		fs.writeFileSync('./empList.mdc', renderedReport);

		// return as JSON string which can be used in the HTML viewer on client side
		res.send({statusCode:200, message: "Report Created", reportData: renderedReport});
	});
Attachments
empList.mrt
(3.74 KiB) Downloaded 331 times
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Database connection on server side

Post by HighAley »

Hello.

We have made an improvement.
Please, try our next prerelease build on December 11.

Thank you.
Joanne
Posts: 23
Joined: Tue Dec 01, 2015 3:48 am

Re: Database connection on server side

Post by Joanne »

Hi HighAley, thanks for the reply. What improvement has been made concerning the data base connection on server side ?
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Database connection on server side

Post by HighAley »

Hello.

There was no changes on server-side. We have made an improvement on client side.

Thank you.
Post Reply