Skip to main content

Overview

The TypeOrmCrudService 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 use TypeOrmCrudService 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

Repository<T>
required
The TypeORM repository instance for your entity. This is typically injected using @InjectRepository(Entity).

Properties

Protected Properties

DataSourceOptions['type']
The database type (e.g., ‘postgres’, ‘mysql’, ‘mariadb’). Automatically detected from the repository connection.
string[]
An array of all column names in the entity. Populated during initialization.
string[]
An array of primary key column names. Used for entity identification.
boolean
Indicates whether the entity has a soft delete column. Enables soft delete functionality.
ObjectLiteral
A hash map of entity columns for quick lookup and SQL injection prevention.
Map<string, IAllowedRelation>
A cache of entity relations metadata for efficient join operations.

Public Methods

getMany

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

getOne

Retrieves a single entity based on the request parameters.
CrudRequest
required
The CRUD request object with search criteria.
T
Returns the found entity or throws a NotFoundException.
Example:

createOne

Creates a single entity.
CrudRequest
required
The CRUD request object with options.
T | Partial<T>
required
The data transfer object containing the entity data to create.
T
Returns the created entity, either as a shallow object or fully populated based on the returnShallow option.
Example:

createMany

Creates multiple entities in bulk.
CrudRequest
required
The CRUD request object.
CreateManyDto<T | Partial<T>>
required
An object with a bulk property containing an array of entities to create.
T[]
Returns an array of created entities.
Example:
Bulk creates are processed in chunks of 50 entities for optimal performance.

updateOne

Updates a single entity.
CrudRequest
required
The CRUD request object with entity identification.
T | Partial<T>
required
Partial entity data to update.
T
Returns the updated entity.
Example:
By default, path parameters cannot be overridden. Set allowParamsOverride: true in route options to change this behavior.

replaceOne

Replaces an entire entity (PUT operation).
CrudRequest
required
The CRUD request object.
T | Partial<T>
required
Complete entity data to replace.
T
Returns the replaced entity.
Example:

deleteOne

Deletes a single entity.
CrudRequest
required
The CRUD request object with entity identification.
void | T
Returns the deleted entity if returnDeleted is true, otherwise returns void.
Example:
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.
CrudRequest
required
The CRUD request object identifying the soft-deleted entity.
T
Returns the recovered entity.
Example:

createBuilder

Creates a TypeORM QueryBuilder with all request parameters applied.
ParsedRequestParams
required
Parsed request parameters including filters, joins, sort, and pagination.
CrudRequestOptions
required
CRUD request options from the controller configuration.
boolean
default:"true"
Whether to apply pagination and sorting (for getMany) or not (for getOne).
boolean
default:"false"
Whether to include soft-deleted entities in the query.
SelectQueryBuilder<T>
A fully configured TypeORM SelectQueryBuilder instance.
Example:

Advanced Features

SQL Injection Protection

The service includes built-in SQL injection protection using regular expressions:
Field names and values are automatically checked for SQL injection patterns. Invalid queries throw BadRequestException.

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
String Operators:
  • $cont - Contains
  • $excl - Excludes
  • $starts - Starts with
  • $ends - Ends with
Case-Insensitive Operators:
  • $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)
Array Operators:
  • $in - In array
  • $notin - Not in array
  • $inL - In array (case-insensitive)
  • $notinL - Not in array (case-insensitive)
Null Operators:
  • $isnull - Is null
  • $notnull - Is not null
Range Operators:
  • $between - Between two values

Database-Specific Optimizations

The service automatically detects the database type and applies appropriate optimizations:
For MySQL and MariaDB, backticks are used for identifiers, while PostgreSQL uses double quotes.

Repository Access

Direct access to TypeORM repository methods:
Example:

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
Example:

See Also