report-developer-server

1.0.7 • Public • Published

Report Developer Server

Report Developer Server is a report developer's server side package that facilitates database operations for report development and management.

Features

  • Database Connection: Establish a connection to a database by providing credentials.
  • Save Reports: Save new report in the connected database.
  • Retrieve Reports: Retrieve all saved reports from the connected database.
  • Report Development: Execute custom queries for report development purposes.

Installation

To install the package, use npm:

npm i report-developer-server

Usage

import Express from "express";
import dotenv from "dotenv";

// import all 4 methods provided by the report-developer-server
import {
  connect,
  reportDeveloper,
  saveReport,
  getReports,
} from "report-developer-server";

const app = Express();
app.use(Express.json());
dotenv.config();

// create dbCredentials a object of database credentials with host, port, user, password, and database feilds
// all the feilds of dbCredentials are required
const dbCredentials = {
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
};

// use try/catch inorder to catch the error and handle it
try {
  // 1) CONNECT : establishes connection with your database and creates a new ReportBuilder table if it doesn't exist inorder to save your reports
  // parameters: 1) dbCredentials [object] - used to create connection to your database
  //             2) logs [boolean] - pass 'true' if you want success and error logs (recommended during development)
  //                               - by default the value is 'false' so don't pass anything if you don't want the logs (recommended during production)
  connect(dbCredentials, true);
} catch (error) {
  // log the error in case when an error occurs
  console.error(error);
}

// create a POST route to handle POST requests to '/reportDeveloper'
// this endpoint is responsible to take the 'query' from the client, build the report by passing the 'query' to 'reportDeveloper()' and return a response with the obtained data from 'reportDeveloper()' to the client
app.post("/reportDeveloper", async (req, res) => {
  // destructure 'query' from the request body
  const { query } = req.body;

  // check if the 'query' parameter is missing
  if (!query) {
    // return a 400 Bad Request response if 'query' is not provided
    return res.status(400).json({ error: "Query parameter is missing" });
  }

  try {
    // 2) REPORT DEVELOPER: executes the 'query' on your database and returns the data
    // parameters: 1) query [String] - sqlQuery to be executed on your database to create your desired report
    const response = await reportDeveloper(query);

    // return a 200 OK response with the obtained data from 'reportDeveloper()'
    return res
      .status(200)
      .json({ data: response.data, error: null, status: response.status });
  } catch (error) {
    // if an error occurs during 'reportDeveloper()' execution, handle it here
    // return a 500 Internal Server Error response with the error message
    return res.status(500).json({ error: error.message });
  }
});

// create a POST route to handle POST requests to '/saveReport'
// this endpoint is responsible to take 'name' and 'query' from the client, pass them to 'saveReport()' which then saves the report in the 'reportBuilder' table on your database and return a success or error response to the client
app.post("/saveReport", async (req, res) => {
  // destructure 'name' and 'query' from the request body
  const { name, query } = req.body;

  // check if the 'name' or 'query' parameters are missing
  if (!name || !query) {
    // return a 400 Bad Request response with an error message if parameters are missing
    return res.status(400).json({ error: "Report parameters are missing" });
  }

  try {
    // 3) SAVE REPORT: saves your report in the 'reportBuilder' table on your database
    // parameters: 1) name [String] - name of your report
    //             2) query [String] - sqlQuery of your report
    const response = await saveReport({ name, query });

    // return a successful 200 OK response with the saved report data
    return res
      .status(200)
      .json({ data: response.data, error: null, status: response.status });
  } catch (error) {
    // handle any errors that occurred during the attempt to save the report
    // return a 500 Internal Server Error response with the error message
    return res.status(500).json({ error: error.message });
  }
});

// create a GET route to handle GET requests to '/savedReports'
// this endpoint is responsible to fetch all the saved reports from the 'reportBuilder' table on your database and return a response with the obtained reports to the client
app.get("/savedReports", async (req, res) => {
  try {
    // 4) SAVED REPORTS: fetches all the saved reports from 'reportDeveloper' table on your database and return a response with the obtained reports
    const response = await getReports();

    // return a successful 200 OK response with the obtained reports
    return res
      .status(200)
      .json({ data: response.data, error: null, status: response.status });
  } catch (error) {
    // handle any errors that occurred during the attempt to retrieve reports
    // return a 500 Internal Server Error response with the error message
    return res.status(500).json({ error: error.message });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, function () {
  console.log(`Server running on port ${PORT} `);
});

Package Sidebar

Install

npm i report-developer-server

Weekly Downloads

0

Version

1.0.7

License

ISC

Unpacked Size

11.2 kB

Total Files

3

Last publish

Collaborators

  • finson