Skip to content

feat: consolidate prisma client usage#120

Merged
robertocarlous merged 1 commit into
Neurowealth:mainfrom
Lansa-18:feature/consolidate-prisma-client-usage
May 31, 2026
Merged

feat: consolidate prisma client usage#120
robertocarlous merged 1 commit into
Neurowealth:mainfrom
Lansa-18:feature/consolidate-prisma-client-usage

Conversation

@Lansa-18
Copy link
Copy Markdown
Contributor

Closes #116

Consolidate Prisma Client to Shared Singleton (#116)

Summary

Consolidates all Prisma client usage to the shared singleton in src/db/index.ts. Previously, 6 modules each called new PrismaClient() directly, creating extra connection pools and making graceful shutdown fragile.


Changes Implemented

Converted to Singleton — import db from '../db'

  • src/stellar/events.ts
  • src/agent/snapshotter.ts
  • src/agent/router.ts
  • src/agent/loop.ts
  • src/agent/scanner.ts
  • src/jobs/sessionCleanup.ts

Graceful Shutdown Fixed — src/agent/loop.ts

  • Removed await prisma.$disconnect() from stopAgentLoop() — the singleton's disconnect is now handled exclusively by src/index.ts after stopAgentLoop() returns, eliminating duplicate disconnects
  • Removed the duplicate SIGTERM/SIGINT signal handlers from setupGracefulShutdown() — these were racing with src/index.ts's own gracefulShutdown() and calling process.exit(0) prematurely. Only the uncaughtException handler is kept

Test Mocks Updated

File Change
src/stellar/__tests__/events.helpers.test.ts Replaced the now-unused PrismaClient mock with jest.mock('../../db', () => ({ default: {} }))
tests/unit/stellar/events.test.ts Added jest.mock('../../../src/db', ...) pointing to the existing mockPrisma instance

Technical Decisions

  • Pure infrastructure refactor — no business logic changes. Query behaviour is identical since the singleton uses the same @prisma/client under the hood
  • After this change, a single db.$disconnect() in src/index.ts cleanly closes the one shared pool on SIGTERM/SIGINT
  • Shutdown sequence is now unambiguously:
  stop event listener → stop agent loop (cron jobs only) → disconnect Prisma

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 31, 2026

@Lansa-18 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@robertocarlous robertocarlous merged commit 5b1569c into Neurowealth:main May 31, 2026
1 of 2 checks passed
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.

Consolidate Prisma client usage

2 participants