Overview
TheCrudService 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.
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”.
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 arraycount: Number of entities in the current pagetotal: Total number of entitiespage: Current page number (1-based)pageCount: Total number of pages
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.- Returns
trueifalwaysPaginateis enabled in options - Returns
trueifpageoroffsetis specified in the request - Returns
falseotherwise
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.
- Request
limitparameter (capped bymaxLimitif set) - Configured
limitoption (capped bymaxLimitif set) - Configured
maxLimitoption null(no limit)
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.
- If
pageis specified: returnstake * (page - 1) - If
offsetis specified: returns theoffsetvalue - Otherwise: returns
null
getPrimaryParams
Extracts primary parameter field names from the CRUD options.CrudRequestOptions
required
The CRUD request options.
string[]
An array of primary parameter field names.
Abstract Methods
These methods must be implemented by concrete service classes:getMany
getOne
createOne
createMany
updateOne
replaceOne
deleteOne
recoverOne
Usage in Custom Implementations
When creating a custom CRUD service for a different ORM or data source, extend theCrudService 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.See Also
- TypeOrmCrudService - TypeORM implementation
- Custom Service - Creating custom implementations