How do I get data from a db?



  • Just playing around, my data are in different databases, mysql, postgres, RDS, how do I connect, extract or migrate them to use them i jsreport?


  • administrators

    @tinynet hi! you can write a script and use a nodejs module that handles the connection to your db and query the data to fill it in your report

    minimal example:

    // your jsreport script
    var dbHandler = require('nodejs-connector-for-your-favorite-db')
    
    function beforeRender (req, res, done) {
      dbHandler.connect(/*connection parameters here*/, function (err) {
        if (err) { return done(err) }
     
        dbHandler.query(/*query parameters here*/, function (err, results) {
          if (err) { return done(err) }
          
          // next line fill the data that your current report is going to use 
          req.data = results // results should containing the data of your query (array, object, etc)
    
          done()
        })
      }
    }
    

    the exact code will depend on the nodejs module that you choose to manage your db but this code is the general idea.



  • Oh, thank you.
    Is this the only way? Is there a way of importing for example a csv file? or even post data through API?


  • administrators

    there are a lot of ways i just shared one 😃

    Is there a way of importing for example a csv file?

    not at this moment but this can be easily supported with a custom extension. the only reason there is no option to import this file is just because no one has asked for it until now.

    or even post data through API?

    yes, you can query your data in your backend and then prepare a request and communicate with jsreport with our http API too.



  • ok...what I'm trying to understand is if jsreport can be used to create dashboards and report for an application backed by a database..
    It would be a good use case for jsreport or I'm completely on a wrong path?


  • administrators

    what I'm trying to understand is if jsreport can be used to create dashboards and report for an application backed by a database..

    yes, this is the normal flow. jsreport only cares about the data, not about the details, or handling something fancy like automatic datasource, you can extract the data from anywhere, using your own custom backend (and send it through http) or using the power of scripts and node.js. we don't have any built-in logic to extract data automatically, and this is a key design decision of jsreport, because we think this is usually part of your business, and it will depend of the kind of tech you use and the constraints you have, so it is something that we think it is better handled by yourself rather than a "smart" tool.

    so, yes, you are in the right path, you just need to decide how to feed jsreport according to your needs.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.