Overview
TheTypeOrmCrudService is a powerful service class that extends the abstract CrudService and provides complete CRUD functionality for TypeORM entities. It handles complex querying, filtering, sorting, pagination, and relation management out of the box.
Basic Usage
The simplest way to useTypeOrmCrudService is to extend it in your service class:
The
TypeOrmCrudService automatically inherits all CRUD methods and doesn’t require any additional configuration for basic operations.Constructor
The TypeORM repository instance for your entity. This is typically injected using
@InjectRepository(Entity).Properties
Protected Properties
The database type (e.g., ‘postgres’, ‘mysql’, ‘mariadb’). Automatically detected from the repository connection.
An array of all column names in the entity. Populated during initialization.
An array of primary key column names. Used for entity identification.
Indicates whether the entity has a soft delete column. Enables soft delete functionality.
A hash map of entity columns for quick lookup and SQL injection prevention.
A cache of entity relations metadata for efficient join operations.
Public Methods
getMany
Retrieves multiple entities based on the request parameters.The CRUD request object containing parsed query parameters and options.
Returns either a paginated response with metadata or a plain array of entities.
getOne
Retrieves a single entity based on the request parameters.The CRUD request object with search criteria.
Returns the found entity or throws a NotFoundException.
createOne
Creates a single entity.The CRUD request object with options.
The data transfer object containing the entity data to create.
Returns the created entity, either as a shallow object or fully populated based on the
returnShallow option.createMany
Creates multiple entities in bulk.The CRUD request object.
An object with a
bulk property containing an array of entities to create.Returns an array of created entities.
Bulk creates are processed in chunks of 50 entities for optimal performance.
updateOne
Updates a single entity.The CRUD request object with entity identification.
Partial entity data to update.
Returns the updated entity.
replaceOne
Replaces an entire entity (PUT operation).The CRUD request object.
Complete entity data to replace.
Returns the replaced entity.
deleteOne
Deletes a single entity.The CRUD request object with entity identification.
Returns the deleted entity if
returnDeleted is true, otherwise returns void.Supports both hard delete and soft delete. When soft delete is enabled, the entity is marked as deleted but not removed from the database.
recoverOne
Recovers a soft-deleted entity.The CRUD request object identifying the soft-deleted entity.
Returns the recovered entity.
createBuilder
Creates a TypeORM QueryBuilder with all request parameters applied.Parsed request parameters including filters, joins, sort, and pagination.
CRUD request options from the controller configuration.
Whether to apply pagination and sorting (for getMany) or not (for getOne).
Whether to include soft-deleted entities in the query.
A fully configured TypeORM SelectQueryBuilder instance.
Advanced Features
SQL Injection Protection
The service includes built-in SQL injection protection using regular expressions:Query Operators
The service supports a comprehensive set of query operators: Basic Operators:$eq- Equal to$ne- Not equal to$gt- Greater than$lt- Less than$gte- Greater than or equal to$lte- Less than or equal to
$cont- Contains$excl- Excludes$starts- Starts with$ends- Ends with
$eqL- Equal (case-insensitive)$neL- Not equal (case-insensitive)$contL- Contains (case-insensitive)$startsL- Starts with (case-insensitive)$endsL- Ends with (case-insensitive)$exclL- Excludes (case-insensitive)
$in- In array$notin- Not in array$inL- In array (case-insensitive)$notinL- Not in array (case-insensitive)
$isnull- Is null$notnull- Is not null
$between- Between two values
Database-Specific Optimizations
The service automatically detects the database type and applies appropriate optimizations:Repository Access
Direct access to TypeORM repository methods:Error Handling
The service provides consistent error handling:- NotFoundException: Thrown when an entity is not found
- BadRequestException: Thrown for invalid data or SQL injection attempts
See Also
- CrudService - Abstract base class
- Custom Service - Creating custom implementations