Skip to main content

TypeOrmCrudService

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

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
Features:
  • Automatic query builder creation
  • Field selection
  • Filtering and search
  • Relation joins
  • Sorting
  • Pagination
  • Caching support
Example:

getOne()

Retrieves a single entity by ID or search criteria.
CrudRequest
required
Request object with search criteria in parsed.search
Promise<T>
The found entity or throws NotFoundException
Example:

createOne()

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)
Example:
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 create
Promise<T[]>
Array of created entities
Example:
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
Example:
Behavior:
  • Fetches the existing entity
  • Merges with new data
  • Applies parameter filters if allowParamsOverride is false
  • Returns shallow or fully loaded entity based on returnShallow option

replaceOne()

Fully replaces an entity.
CrudRequest
required
Request object with entity identifier
T | Partial<T>
required
Complete entity data
Promise<T>
The replaced entity
Example:
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
Example:
Behavior:
  • Uses soft delete if options.query.softDelete is 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
Example:
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
Example:

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

Helper Methods

getParamFilters()

Extracts parameter filters from parsed request.
CrudRequest['parsed']
required
Parsed request data
ObjectLiteral
Object with filter field-value pairs
Example:

Repository Accessors

The service provides direct access to TypeORM repository methods:

findOne

Access TypeORM’s findOne method directly. Example:

find

Access TypeORM’s find method directly. Example:

count

Access TypeORM’s count method directly. Example:

Advanced Features

SQL Injection Protection

The service includes built-in SQL injection protection:
All field names in queries are automatically checked against these patterns.

Soft Delete Support

If your entity has a @DeleteDateColumn(), the service automatically:
  • Filters out soft-deleted records by default
  • Supports includeDeleted query parameter
  • Provides deleteOne() for soft deletion
  • Provides recoverOne() for recovery
Example Entity:
Usage:

Relation Handling

The service automatically handles entity relations:
Relation Configuration:

Caching

Enable query result caching:

Complete Usage Example

Query Examples