TypeOrmCrudService
TheTypeOrmCrudService is a concrete implementation of the CrudService abstract class that provides full CRUD functionality using TypeORM. This is the primary service class you’ll use with TypeORM repositories.
Overview
This service provides:- Complete CRUD operations implementation
- Advanced query building with TypeORM
- Automatic relation handling
- Search and filter support
- Pagination and sorting
- SQL injection protection
- Soft delete support
Constructor
Repository<T>
required
TypeORM repository instance for the entity
CRUD Methods
getMany()
Retrieves multiple entities with advanced query support.CrudRequest
required
The CRUD request containing:
parsed: Parsed query parameters (filter, sort, pagination, joins)options: CRUD configuration options
Promise<GetManyDefaultResponse<T> | T[]>
Returns:
- Paginated response with metadata if pagination is applied
- Simple array of entities otherwise
- Automatic query builder creation
- Field selection
- Filtering and search
- Relation joins
- Sorting
- Pagination
- Caching support
getOne()
Retrieves a single entity by ID or search criteria.CrudRequest
required
Request object with search criteria in
parsed.searchPromise<T>
The found entity or throws
NotFoundExceptioncreateOne()
Creates a single entity.CrudRequest
required
Request object with configuration options
T | Partial<T>
required
Entity data to create
Promise<T>
The created entity (shallow or fully loaded based on
returnShallow option)The service automatically applies parameter filters and auth persist data from the request.
createMany()
Creates multiple entities in bulk.CrudRequest
required
Request object
CreateManyDto<T | Partial<T>>
required
Object with
bulk array containing entities to createPromise<T[]>
Array of created entities
Entities are saved in chunks of 50 for optimal performance.
updateOne()
Partially updates an existing entity.CrudRequest
required
Request object with entity identifier
T | Partial<T>
required
Partial entity data to update
Promise<T>
The updated entity
- Fetches the existing entity
- Merges with new data
- Applies parameter filters if
allowParamsOverrideis false - Returns shallow or fully loaded entity based on
returnShallowoption
replaceOne()
Fully replaces an entity.CrudRequest
required
Request object with entity identifier
T | Partial<T>
required
Complete entity data
Promise<T>
The replaced entity
If the entity doesn’t exist, it will be created with the provided data.
deleteOne()
Deletes a single entity (soft or hard delete).CrudRequest
required
Request object with entity identifier
Promise<void | T>
Void or the deleted entity if
returnDeleted option is enabled- Uses soft delete if
options.query.softDeleteis true - Uses hard delete (permanent) otherwise
- Can return deleted entity based on configuration
recoverOne()
Recovers a soft-deleted entity.CrudRequest
required
Request object with entity identifier
Promise<T>
The recovered entity
Only works with entities that have a delete date column (e.g.,
@DeleteDateColumn()).Query Builder Methods
createBuilder()
Creates a TypeORM SelectQueryBuilder with all query parameters applied.ParsedRequestParams
required
Parsed request parameters (filters, joins, sort, pagination)
CrudRequestOptions
required
CRUD configuration options
boolean
default:"true"
Whether to apply pagination and sorting (true for getMany, false for getOne)
boolean
default:"false"
Include soft-deleted entities
Promise<SelectQueryBuilder<T>>
Configured TypeORM query builder
doGetMany()
Executes the query builder and returns results with or without pagination.SelectQueryBuilder<T>
required
Configured query builder
ParsedRequestParams
required
Parsed query parameters
CrudRequestOptions
required
CRUD options
Promise<GetManyDefaultResponse<T> | T[]>
Paginated response or array of entities
Helper Methods
getParamFilters()
Extracts parameter filters from parsed request.CrudRequest['parsed']
required
Parsed request data
ObjectLiteral
Object with filter field-value pairs
Repository Accessors
The service provides direct access to TypeORM repository methods:findOne
findOne method directly.
Example:
find
find method directly.
Example:
count
count method directly.
Example:
Advanced Features
SQL Injection Protection
The service includes built-in SQL injection protection:Soft Delete Support
If your entity has a@DeleteDateColumn(), the service automatically:
- Filters out soft-deleted records by default
- Supports
includeDeletedquery parameter - Provides
deleteOne()for soft deletion - Provides
recoverOne()for recovery
Relation Handling
The service automatically handles entity relations:Caching
Enable query result caching:Complete Usage Example
Query Examples
Related Documentation
- CrudService - Abstract base class
- CRUD Controllers
- Query Parameters
- Relations