Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INV-3663] Create Common Database Class #3704

Closed
wants to merge 5 commits into from

Conversation

LocalNewsTV
Copy link
Collaborator

@LocalNewsTV LocalNewsTV commented Nov 21, 2024

Overview

This PR includes the following proposed change(s):

First pass

  • Compartmentalize Database functionality into class

    • Class connects to DB
    • Class Runs Queries, returning responses
    • Class can be configured to fire logs, removing need for logger object to be used
  • Converted Email Settings/Templates to use new Error Handler and CommonDatabase class

    • Showcases cutback on code, highlights functionality.
  • Closes ⚡ Common Query utility in API #3663

Examples

Having an Error occur in a response

function getEmailTemplates(): RequestHandler {
  return async (req, res) => {
    const response = await new CommonDatabase().query(getEmailTemplatesSQL());
    throw new Error('Error Occurring in Function');
    return res.status(200).json({
      message: 'Email templates retrieved',
      result: response?.rows,
      namespace: req.url,
      req: req.body
    });
  };
}

Error Handling Middleware kicks in
image

Throwing an Error in the CommonDatabase

  public readonly query = async (sqlStatement: SQLStatement): Promise<any> => {
    let connection: PoolClient;
    try {
      connection = await this.getDBConnection();
      if (this.logDetails) {
        this.log();
      }
      throw new Error('Error Occurring in Function');
      return await connection.query(sqlStatement.text, sqlStatement.values);
    } catch (e) {
      throw new CustomError(e?.message || 'Connection Refused', 503);
    } finally {
      connection?.release();
    }
  };
}

The API Handles the error and continues to run, changing the status code to one we defined in CustomError
image

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

⚡ Common Query utility in API
1 participant