CrudService
TheCrudService 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
getOne()
Retrieves a single entity.CrudRequest
required
The CRUD request object with search criteria
Promise<T>
Returns the requested entity
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
createMany()
Creates multiple entities in bulk.CrudRequest
required
The CRUD request object
CreateManyDto
required
Object containing a
bulk array of entities to createPromise<T[]>
Returns an array of created entities
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
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.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
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.
Utility Methods
throwBadRequestException()
Throws aBadRequestException with an optional message.
unknown
Optional error message
throwNotFoundException()
Throws aNotFoundException with a resource name.
string
required
The name of the resource that was not found
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 entitiescount: Number of entities in current pagetotal: Total number of entitiespage: Current page number (1-indexed)pageCount: Total number of pages
Override this method to customize the pagination response format.
decidePagination()
Determines whether pagination should be applied.ParsedRequestParams
required
Parsed request parameters
CrudRequestOptions
required
CRUD configuration options
boolean
Returns
true if pagination should be appliedgetTake()
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
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
getPrimaryParams()
Extracts primary parameter field names from options.CrudRequestOptions
required
CRUD configuration options
string[]
Array of primary parameter field names
Implementation Example
Here’s a complete example of implementing a custom CRUD service:Related Documentation
- TypeOrmCrudService - TypeORM implementation
- CRUD Controllers
- Request Parsing