[PR #5210] [CLOSED] refactor: use singleton PrismaService and add env var guards for safety #5110

Closed
opened 2026-03-17 02:35:11 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/5210
Author: @Krishprajapati15
Created: 6/28/2025
Status: Closed

Base: mainHead: main


📝 Commits (8)

  • 9842df2 refactor(auth): enhance GithubStrategy with type safety, error handling, and secure validation
  • b001509 refactor(auth): apply robust type safety and error handling to all strategies
  • bdc82ae refactor(auth): enhance GithubStrategy with type safety, error handling, and secure validation
  • f1e2f4a refactor(auth): apply robust type safety and error handling to all strategies
  • d73e0f3 refactor: improve email validation and unused done()
  • a45dcb5 chore: remove unnecessary Promise return types
  • 3cb85ea refactor: use singleton PrismaService and add env var guards for safety
  • a6c046c refactor: use singleton PrismaService and add env var guards for safety

📊 Changes

7 files changed (+329 additions, -119 deletions)

View changed files

📝 packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts (+71 -12)
📝 packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts (+69 -15)
📝 packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts (+13 -9)
📝 packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts (+81 -14)
📝 packages/hoppscotch-backend/src/auth/strategies/rt-jwt.strategy.ts (+14 -10)
📝 packages/hoppscotch-backend/src/errors.ts (+7 -0)
📝 packages/hoppscotch-backend/src/infra-config/helper.ts (+74 -59)

📄 Description

What was the problem?

Previously, the codebase instantiated a new PrismaService inside each helper function that interacted with the database. This approach could lead to:

  • Database connection exhaustion: Multiple instances mean more connections, risking the maximum pool size being hit.
  • Resource leaks and instability: Extra PrismaService instances can cause memory leaks or unexpected disconnects, especially under load.

Additionally, the code accessed environment variables (e.g., process.env.XYZ) directly without checking if they were defined. If any expected environment variable was missing, this would cause:

  • Application crashes: For example, calling .split(',') on undefined throws a runtime error.
  • Unpredictable behavior: Passing undefined values to encrypt/decrypt functions or using them for configuration.

What has changed?

  • Singleton PrismaService:
    All database helper functions now accept a shared PrismaService instance as a parameter, instead of creating new ones internally. This ensures a single connection pool is used, following best practices for database usage and resource management.

  • Environment variable guards:
    Every access to an environment variable now uses a fallback (?? '') to guarantee a string value is used. This prevents runtime errors and ensures the app doesn't crash if an env variable is missing.


Before

  • Each function created its own PrismaService instance, risking DB connection pool exhaustion and leaks.
  • Missing environment variables could cause the app to crash with runtime errors or throw exceptions during encryption/decryption.

After

  • All functions use an injected/shared PrismaService instance, ensuring optimal DB resource usage and stability.
  • All environment variable lookups have safe fallbacks, so the app will not crash due to missing env vars.
  • The codebase is more robust and production-ready, with improved error safety and resource handling.

Summary:
This PR improves the reliability and stability of the infra config helper code by ensuring only a single PrismaService instance is used and by adding guards for all environment variable accesses.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/5210 **Author:** [@Krishprajapati15](https://github.com/Krishprajapati15) **Created:** 6/28/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (8) - [`9842df2`](https://github.com/hoppscotch/hoppscotch/commit/9842df2b6ff7138360035ff3217db07cf509dbdc) refactor(auth): enhance GithubStrategy with type safety, error handling, and secure validation - [`b001509`](https://github.com/hoppscotch/hoppscotch/commit/b001509133825a981aaeefc949b2ab4832c68b41) refactor(auth): apply robust type safety and error handling to all strategies - [`bdc82ae`](https://github.com/hoppscotch/hoppscotch/commit/bdc82aeb3c4dfc27cf168483351afa7446e9f3c1) refactor(auth): enhance GithubStrategy with type safety, error handling, and secure validation - [`f1e2f4a`](https://github.com/hoppscotch/hoppscotch/commit/f1e2f4a196a31ae83e91863f8b4ca9383a09d650) refactor(auth): apply robust type safety and error handling to all strategies - [`d73e0f3`](https://github.com/hoppscotch/hoppscotch/commit/d73e0f35ab67f01acadbb7e75e8981ef4168b03d) refactor: improve email validation and unused done() - [`a45dcb5`](https://github.com/hoppscotch/hoppscotch/commit/a45dcb57b911a591019fa35d79e0a5bdab8d7ff5) chore: remove unnecessary Promise<any> return types - [`3cb85ea`](https://github.com/hoppscotch/hoppscotch/commit/3cb85ea2cb60abe2107d23861d4449c58e9bfa0d) refactor: use singleton PrismaService and add env var guards for safety - [`a6c046c`](https://github.com/hoppscotch/hoppscotch/commit/a6c046c098a51699d4a2188b99a88a28ff420c51) refactor: use singleton PrismaService and add env var guards for safety ### 📊 Changes **7 files changed** (+329 additions, -119 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts` (+71 -12) 📝 `packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts` (+69 -15) 📝 `packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts` (+13 -9) 📝 `packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts` (+81 -14) 📝 `packages/hoppscotch-backend/src/auth/strategies/rt-jwt.strategy.ts` (+14 -10) 📝 `packages/hoppscotch-backend/src/errors.ts` (+7 -0) 📝 `packages/hoppscotch-backend/src/infra-config/helper.ts` (+74 -59) </details> ### 📄 Description ## What was the problem? Previously, the codebase instantiated a new `PrismaService` inside each helper function that interacted with the database. This approach could lead to: - **Database connection exhaustion:** Multiple instances mean more connections, risking the maximum pool size being hit. - **Resource leaks and instability:** Extra PrismaService instances can cause memory leaks or unexpected disconnects, especially under load. Additionally, the code accessed environment variables (e.g., `process.env.XYZ`) directly without checking if they were defined. If any expected environment variable was missing, this would cause: - **Application crashes:** For example, calling `.split(',')` on `undefined` throws a runtime error. - **Unpredictable behavior:** Passing undefined values to encrypt/decrypt functions or using them for configuration. --- ## What has changed? - **Singleton PrismaService:** All database helper functions now accept a shared PrismaService instance as a parameter, instead of creating new ones internally. This ensures a single connection pool is used, following best practices for database usage and resource management. - **Environment variable guards:** Every access to an environment variable now uses a fallback (`?? ''`) to guarantee a string value is used. This prevents runtime errors and ensures the app doesn't crash if an env variable is missing. --- ## Before - Each function created its own PrismaService instance, risking DB connection pool exhaustion and leaks. - Missing environment variables could cause the app to crash with runtime errors or throw exceptions during encryption/decryption. --- ## After - All functions use an injected/shared PrismaService instance, ensuring optimal DB resource usage and stability. - All environment variable lookups have safe fallbacks, so the app will not crash due to missing env vars. - The codebase is more robust and production-ready, with improved error safety and resource handling. --- **Summary:** This PR improves the reliability and stability of the infra config helper code by ensuring only a single PrismaService instance is used and by adding guards for all environment variable accesses. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-17 02:35:11 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#5110
No description provided.