Skip to main content

Overview

The CrudService is an abstract class that defines the interface for all CRUD service implementations. It provides utility methods for pagination, error handling, and common operations that are shared across different ORM implementations.

Abstract Class Definition

Utility Methods

throwBadRequestException

Throws a standardized BadRequestException with an optional message.
unknown
Optional error message to include in the exception.
BadRequestException
Always throws a BadRequestException.
Example:

throwNotFoundException

Throws a standardized NotFoundException with the entity name.
string
required
The name of the entity that was not found.
NotFoundException
Always throws a NotFoundException with the format: ” not found”.
Example:

createPageInfo

Wraps entity data in a paginated response object with metadata.
T[]
required
The array of entities for the current page.
number
required
The total number of entities across all pages.
number
required
The maximum number of entities per page.
number
required
The number of entities to skip (for pagination).
GetManyDefaultResponse<T>
An object containing:
  • data: The entity array
  • count: Number of entities in the current page
  • total: Total number of entities
  • page: Current page number (1-based)
  • pageCount: Total number of pages
Example:
You can override this method to customize the pagination response format for your application.

decidePagination

Determines whether pagination should be applied to a query.
ParsedRequestParams
required
The parsed request parameters from the query string.
CrudRequestOptions
required
The CRUD options configured for the controller.
boolean
Returns true if pagination should be applied, false otherwise.
Logic:
  • Returns true if alwaysPaginate is enabled in options
  • Returns true if page or offset is specified in the request
  • Returns false otherwise
Example:

getTake

Calculates the number of entities to fetch based on request parameters and configured limits.
ParsedRequestParams
required
The parsed request parameters.
QueryOptions
required
The query options from the controller configuration.
number | null
The number of entities to fetch, or null if no limit should be applied.
Priority Order:
  1. Request limit parameter (capped by maxLimit if set)
  2. Configured limit option (capped by maxLimit if set)
  3. Configured maxLimit option
  4. null (no limit)
Example:

getSkip

Calculates the number of entities to skip for pagination.
ParsedRequestParams
required
The parsed request parameters.
number
required
The number of entities per page (from getTake).
number | null
The number of entities to skip, or null if no offset should be applied.
Logic:
  • If page is specified: returns take * (page - 1)
  • If offset is specified: returns the offset value
  • Otherwise: returns null
Example:

getPrimaryParams

Extracts primary parameter field names from the CRUD options.
CrudRequestOptions
required
The CRUD request options.
string[]
An array of primary parameter field names.
Example:

Abstract Methods

These methods must be implemented by concrete service classes:

getMany

Retrieves multiple entities based on request parameters.

getOne

Retrieves a single entity.

createOne

Creates a single entity.

createMany

Creates multiple entities.

updateOne

Updates a single entity (PATCH operation).

replaceOne

Replaces a single entity (PUT operation).

deleteOne

Deletes a single entity.

recoverOne

Recovers a soft-deleted entity.

Usage in Custom Implementations

When creating a custom CRUD service for a different ORM or data source, extend the CrudService class:

Type Parameters

generic
required
The entity type that this service manages. Should be your entity/model class.

Interfaces

GetManyDefaultResponse

CrudRequest

Best Practices

Consistent Error Handling: Always use the provided throwBadRequestException and throwNotFoundException methods for consistent error messages across your application.
Override createPageInfo: If your application uses a different pagination format (e.g., cursor-based pagination), override the createPageInfo method to match your needs.
When implementing abstract methods, ensure you handle all edge cases including:
  • Empty results
  • Invalid input data
  • Missing required fields
  • Validation errors

See Also