Custom Handler Guide

Custom Handler Guide - Override Default Operations

Overview

When you need to customize the behavior of CRUD operations beyond what the default implementation provides, you can use inject and handler to create custom logic. This allows you to:

  • Override default pagination behavior

  • Add custom business logic

  • Use custom services instead of direct Prisma queries

  • Implement complex filtering or data transformation

  • Return custom response formats

When to Use Custom Handlers

Use inject and handler when:

  1. Default behavior doesn't meet your requirements - You need custom pagination, filtering, or data processing

  2. Business logic is required - You need to call services, perform calculations, or apply complex rules

  3. Custom response format - You need to return data in a different structure than the default

  4. Performance optimization - You need to optimize queries or add caching

Syntax

Handler Parameters by Operation Type

LIST Operation

GET Operation

CREATE Operation

UPDATE Operation

DELETE_BY_ID Operation

Examples

Example 1: Custom LIST with Flexible Pagination

Use Case: Allow pagination to be optional, and support returning all data when pagination=false.

Key Points:

  • Uses inject: [PrismaClient] to inject Prisma client

  • Custom handler receives query: CategoryFilterDTO as first parameter

  • Handler implements custom pagination logic based on pagination flag

  • Returns different response formats based on pagination mode

Example 2: Using Custom Service Instead of Direct Prisma

Use Case: Use a service layer for business logic instead of direct database access.

Key Points:

  • Uses inject: [CredentialService] to inject a custom service

  • Handler delegates all logic to the service

  • Service can contain business logic, validation, or additional processing

Example 3: Multiple Injected Services

Use Case: Use multiple services in a single handler.

Key Points:

  • Multiple services can be injected in order

  • Handler parameters match the order of inject array

  • Each service is available as a typed parameter

Combining with Other Options

You can combine handler with other operation options:

Note: When using a custom handler, the filter and order mappers are still available for documentation and type safety, but the handler is responsible for applying them.

Default Behavior vs Custom Handler

Default Behavior (No Handler)

  • Uses default pagination (page-based)

  • Applies filter and order mappers automatically

  • Returns standard response format

Custom Handler

  • Full control over query execution

  • Can implement custom pagination (cursor-based, offset-based, etc.)

  • Can add custom business logic

  • Can return custom response format

  • Must manually apply filtering and ordering if needed

Best Practices

  1. Use Services for Business Logic: If you have complex business logic, create a service and inject it rather than putting all logic in the handler.

  2. Keep Handlers Focused: Handlers should be focused on orchestrating the operation, not containing all business logic.

  3. Type Safety: Always type your handler parameters correctly for better IDE support and type safety.

  4. Error Handling: Consider error handling in your handlers, especially for async operations.

  5. Performance: Use Promise.all for parallel operations when possible (like fetching data and count simultaneously).

  6. Select Validators: Always use selectCategoryValidator() or similar validators to ensure consistent field selection.

Common Patterns

Pattern 1: Conditional Pagination

Pattern 2: Service Delegation

Pattern 3: Data Transformation

Pattern 4: Complex Filtering

Summary

  • Use inject to provide services or dependencies to your handler

  • Use handler to override default operation behavior

  • Handler parameters depend on the operation type

  • You can inject multiple services in order

  • Custom handlers give you full control but require more code

  • Combine with other options like filter and order for type safety

Last updated