feat(backend): prisma singleton + updateMarketStatus service (#880 #884) - #1166
Merged
Ehonrie merged 1 commit intoJul 28, 2026
Conversation
…s#880 Netwalls#884) - Set up global PrismaClient singleton with hot-reload guard (issue Netwalls#880) - Consolidate lib/prisma.ts and lib/db.ts to re-export from canonical db.ts - Fix duplicate createMarketRecord declaration in market.service.ts - Fix undefined prisma variable in resolveMarket, cancelMarket, resolveMarketDispute - Harden updateMarketStatus to use MarketStatus enum and set resolvedAt on Resolved - Align pagination.pageSize usage across getAllMarkets and getMarketLeaderboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Issue #880 — Prisma Singleton (
src/db.ts)Problem: `src/db.ts` exported a bare `new PrismaClient()` with no singleton guard. In development, `ts-node-dev` hot-reloads modules on every file save, which would instantiate a new `PrismaClient` on each reload — exhausting the PostgreSQL connection pool quickly.
Additionally, `src/lib/prisma.ts` and `src/lib/db.ts` each created their own independent `PrismaClient` instances, meaning the app could silently run with 3 separate connection pools.
Fix:
```ts
// src/db.ts
export const db: PrismaClient =
globalThis.__prisma ?? new PrismaClient({ log: [...] });
if (process.env.NODE_ENV !== 'production') {
globalThis.__prisma = db;
}
```
Acceptance criteria met:
Issue #884 — `updateMarketStatus()` (
src/services/market.service.ts)Problem: The service file had several bugs that would cause runtime crashes:
Fix:
```ts
export async function updateMarketStatus(
market_id: string,
status: MarketStatus,
outcome?: Outcome
): Promise {
return db.market.update({
where: { id: market_id },
data: {
status,
...(outcome !== undefined && { outcome }),
...(status === MarketStatus.Resolved && { resolvedAt: new Date() }),
},
});
}
```
Acceptance criteria met:
Files Changed
closes #880
closes #884