creating-client-singletons

Centralizes PrismaClient as a global singleton to prevent multiple database connection pools.

Updated Nov 21, 2025
One-click install
npx skills add https://github.com/djankies/claude-configs --skill creating-client-singletons
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: creating-client-singletons
Source: https://github.com/djankies/claude-configs/tree/main/prisma-6/skills/creating-client-singletons
Command: npx skills add https://github.com/djankies/claude-configs --skill creating-client-singletons

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill teaches a global or module-level singleton pattern for PrismaClient to prevent multiple connection pools and P1017 errors, especially in serverless environments.

Core Features & Use Cases

  • Singleton Pattern: One PrismaClient instance shared across modules.
  • Global vs Module Singleton: Techniques for serverless and hot-reload scenarios.
  • Migration Guidance: Replacing per-file clients with centralized access.

Quick Start

Create a central Prisma client export and replace local instantiations with the singleton import.

Frequently Asked Questions about creating-client-singletons

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does my serverless application get P1017 connection pool errors with Prisma?

P1017 errors occur when multiple PrismaClient instances exhaust the database connection pool. In serverless environments, each function invocation can create a new client. A singleton pattern ensures one shared PrismaClient across all invocations, preventing pool exhaustion.

How do I prevent multiple PrismaClient instances in a Node.js application?

Create a centralized module that exports a single PrismaClient instance using a global singleton pattern with globalThis or global. Replace all local client instantiations with imports from this module, ensuring every part of your application shares one connection pool.

Can I use a singleton PrismaClient pattern in hot-reload development environments?

Yes. Use globalThis to persist the PrismaClient across hot reloads, or implement module-level singletons that survive restart cycles. This prevents connection leaks and duplicate pools during development iteration.

What's the difference between a global singleton and module-level singleton for Prisma?

A global singleton (globalThis) persists across module reloads in serverless and development, ideal for ephemeral environments. Module-level singletons work for traditional servers where modules load once; global approach provides stronger isolation in hot-reload scenarios.

Do I need to configure connection limits when using a singleton PrismaClient?

Connection limits are optional but recommended. Configure connectionLimit in Prisma's datasource to cap pool size, preventing resource exhaustion. Logging can also help debug connection behavior in development and production.

How do I migrate existing code with per-file PrismaClient instances to a singleton?

Create a central client export file, update all imports to reference the singleton instead of creating new instances, and remove instantiation calls from route handlers and utility functions. This consolidates pool management into one entry point.