Skip to main content

CrudService

The CrudService is an abstract base class that defines the contract for implementing CRUD operations in your NestJS applications. All concrete service implementations should extend this class.

Overview

This abstract class provides:
  • Standard CRUD method signatures
  • Error handling utilities
  • Pagination helpers
  • Query parameter processing

Abstract Methods

These methods must be implemented by concrete service classes:

getMany()

Retrieves multiple entities based on query parameters.
CrudRequest
required
The CRUD request object containing parsed query parameters and options
Promise<GetManyDefaultResponse<T> | T[]>
Returns either an array of entities or a paginated response with metadata
Example:

getOne()

Retrieves a single entity.
CrudRequest
required
The CRUD request object with search criteria
Promise<T>
Returns the requested entity
Example:

createOne()

Creates a single entity.
CrudRequest
required
The CRUD request object
T | Partial<T>
required
The data transfer object containing entity data to create
Promise<T>
Returns the created entity
Example:

createMany()

Creates multiple entities in bulk.
CrudRequest
required
The CRUD request object
CreateManyDto
required
Object containing a bulk array of entities to create
Promise<T[]>
Returns an array of created entities
Example:

updateOne()

Updates an existing entity (partial update).
CrudRequest
required
The CRUD request object with entity identifier
T | Partial<T>
required
The data to update (partial entity data)
Promise<T>
Returns the updated entity
Example:

replaceOne()

Replaces an entire entity (full update).
CrudRequest
required
The CRUD request object with entity identifier
T | Partial<T>
required
The complete entity data to replace
Promise<T>
Returns the replaced entity
Unlike updateOne(), replaceOne() replaces the entire entity rather than merging properties.
Example:

deleteOne()

Deletes a single entity.
CrudRequest
required
The CRUD request object with entity identifier
Promise<void | T>
Returns void or the deleted entity if configured to return deleted data
Example:

recoverOne()

Recovers a soft-deleted entity.
CrudRequest
required
The CRUD request object with entity identifier
Promise<void | T>
Returns void or the recovered entity
This method is only applicable when soft delete is enabled on your entity.
Example:

Utility Methods

throwBadRequestException()

Throws a BadRequestException with an optional message.
unknown
Optional error message
Example:

throwNotFoundException()

Throws a NotFoundException with a resource name.
string
required
The name of the resource that was not found
Example:

createPageInfo()

Creates a paginated response wrapper.
T[]
required
Array of entities for the current page
number
required
Total number of entities matching the query
number
required
Maximum number of entities per page
number
required
Number of entities to skip
GetManyDefaultResponse<T>
Object containing:
  • data: Array of entities
  • count: Number of entities in current page
  • total: Total number of entities
  • page: Current page number (1-indexed)
  • pageCount: Total number of pages
Override this method to customize the pagination response format.
Example:

decidePagination()

Determines whether pagination should be applied.
ParsedRequestParams
required
Parsed request parameters
CrudRequestOptions
required
CRUD configuration options
boolean
Returns true if pagination should be applied
Example:

getTake()

Calculates the number of entities to fetch.
ParsedRequestParams
required
Parsed query parameters
QueryOptions
required
Query configuration options
number | null
Number of entities to fetch, or null if no limit
Example:

getSkip()

Calculates the number of entities to skip for pagination.
ParsedRequestParams
required
Parsed query parameters
number
required
Number of entities per page
number | null
Number of entities to skip, or null if no offset
Example:

getPrimaryParams()

Extracts primary parameter field names from options.
CrudRequestOptions
required
CRUD configuration options
string[]
Array of primary parameter field names
Example:

Implementation Example

Here’s a complete example of implementing a custom CRUD service: