singleton-patterns

Implement singleton patterns for module-level state in serverless and edge environments.

8|Updated Mar 8, 2026
One-click install
npx skills add https://github.com/SufficientDaikon/omniskill --skill singleton-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: singleton-patterns
Source: https://github.com/SufficientDaikon/omniskill/tree/main/skills/singleton-patterns
Command: npx skills add https://github.com/SufficientDaikon/omniskill --skill singleton-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses the challenge of managing module-level state reliably in serverless and edge environments, preventing issues like connection exhaustion and ensuring expensive resources are initialized only once.

Core Features & Use Cases

  • GlobalThis Attachment: Persists state across warm serverless invocations.
  • Initialization Guards: Guarantees setup code runs exactly once, even under concurrent access.
  • Connection Pooling: Reuses database clients, HTTP agents, and SDK instances to prevent resource leaks.
  • Use Case: Creating a reusable Prisma database client for a serverless API that needs to handle multiple concurrent requests without re-initializing the client for each one.

Quick Start

Use the singleton-patterns skill to create a reusable PrismaClient instance attached to globalThis.

Frequently Asked Questions about singleton-patterns

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

FAQPage Schema
How do I prevent database connection exhaustion in serverless functions?

To prevent database connection exhaustion in serverless functions, use connection pooling and globalThis attachment to initialize database clients once and share them safely across warm invocations.

How do I manage module-level state safely in edge environments?

Module-level state in edge environments is managed safely by attaching resources to globalThis and implementing initialization guards to ensure setup code runs exactly once under concurrent access.

How do I create a reusable PrismaClient instance for a serverless API?

Create a reusable PrismaClient instance for a serverless API by attaching the client to globalThis with an initialization guard, preventing re-initialization during concurrent requests.

Does this singleton pattern approach work with HTTP agents and SDK instances?

Yes, this singleton pattern approach works with HTTP agents and SDK instances by applying connection pooling to reuse them across application lifecycles, preventing resource leaks.

Why does my serverless function re-initialize expensive resources on every invocation?

Serverless functions re-initialize expensive resources when module-level state is not persisted; attaching state to globalThis ensures resources are created once and shared across warm invocations.

What's the best way to ensure idempotency for setup code in serverless environments?

The best way to ensure idempotency for setup code in serverless environments is using initialization guards that guarantee execution runs exactly once, even under concurrent access.