シリーズ: next.js
typescript
31 行
· 更新日 2026-02-03
prisma.ts
next.js/backend/src/services/prisma.ts
// Prisma client service for database operations
// This service provides a singleton instance of PrismaClient
// and handles database connections and disconnections
import { PrismaClient } from '@prisma/client';
// Create a global variable to store the PrismaClient instance
// This prevents multiple instances during hot reloads in development
declare global {
var __prisma: PrismaClient | undefined;
}
// Initialize PrismaClient
// Use existing instance if available (for hot reloads), otherwise create new one
export const prisma = globalThis.__prisma || new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
});
// Store the instance globally in development
if (process.env.NODE_ENV === 'development') {
globalThis.__prisma = prisma;
}
// Graceful shutdown handler
// Ensures database connections are properly closed when the application shuts down
process.on('beforeExit', async () => {
await prisma.$disconnect();
});
// Export the PrismaClient instance for use in other parts of the application
export default prisma;
関連記事
next.js
javascript
更新日 2026-02-03
jest.config.js
jest.config.js — javascript source code from the next.js learning materials (next.js/backend/jest.config.js).
記事を読む →
next.js
typescript
更新日 2026-02-03
setup.ts
setup.ts — typescript source code from the next.js learning materials (next.js/backend/src/__tests__/setup.ts).
記事を読む →
next.js
typescript
更新日 2026-02-03
index.ts
index.ts — typescript source code from the next.js learning materials (next.js/backend/src/index.ts).
記事を読む →
next.js
typescript
更新日 2026-02-03
responses.ts
responses.ts — typescript source code from the next.js learning materials (next.js/backend/src/routes/responses.ts).
記事を読む →
next.js
typescript
更新日 2026-02-03
validation.ts
validation.ts — typescript source code from the next.js learning materials (next.js/backend/src/schemas/validation.ts).
記事を読む →
next.js
typescript
更新日 2026-02-03
seed.ts
seed.ts — typescript source code from the next.js learning materials (next.js/backend/src/scripts/seed.ts).
記事を読む →