Skip to main content
CRUD services extend the base service class to provide database operations. The @nestjsx/crud-typeorm package provides TypeOrmCrudService with built-in methods for all CRUD operations.

Basic service

A minimal CRUD service extends TypeOrmCrudService and injects a TypeORM repository:
From /home/daytona/workspace/source/integration/crud-typeorm/companies/companies.service.ts:1-13:
You must pass the injected repository to the super() constructor to enable all CRUD operations.

Service methods

The TypeOrmCrudService provides eight main methods that correspond to CRUD operations:

Read operations

getMany()

Retrieve multiple entities with filtering, pagination, sorting, and joins:
Returns either an array of entities or a paginated response object:
From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:94-98:

getOne()

Retrieve a single entity by ID:
Throws NotFoundException if the entity doesn’t exist. From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:104-106:

Create operations

createOne()

Create a single entity:
By default, returns the full entity after creation. Set returnShallow: true in route options to return only the saved data without additional queries. From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:113-137:

createMany()

Create multiple entities in bulk:
Entities are saved in chunks of 50 for optimal performance. From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:144-158:

Update operations

updateOne()

Partially update an entity (PATCH):
Merges the DTO with the existing entity and saves. The allowParamsOverride option controls whether path parameters can be overwritten by the request body. From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:165-185:

replaceOne()

Fully replace an entity (PUT):
Replaces the entire entity with the DTO. Unlike updateOne(), this doesn’t merge with existing data. From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:202-231:

Delete operations

deleteOne()

Delete an entity (hard or soft delete):
Behavior depends on configuration:
  • If softDelete: true is set in query options, performs a soft delete
  • If returnDeleted: true is set in route options, returns the deleted entity
  • Otherwise, performs a hard delete and returns void
From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:237-248:

recoverOne()

Recover a soft-deleted entity:
Only available when softDelete: true is enabled. From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:192-195:

Custom business logic

Extend the service to add custom methods or override existing ones:
Access the underlying TypeORM repository through this.repo for custom queries that go beyond CRUD operations.

Query builder

The service uses TypeORM’s query builder internally. You can access it through the createBuilder() method:
This is useful for complex custom queries:
From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:269-348:

Repository access

The service exposes common repository methods for convenience:
From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:70-80:

Soft deletes

Enable soft delete support in your entity and controller:
With soft delete enabled:
  • DELETE /companies/:id sets deletedAt instead of removing the row
  • Soft-deleted entities are excluded from queries by default
  • PATCH /companies/:id/recover restores soft-deleted entities
  • Use ?include_deleted=1 to include soft-deleted entities in results
From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:314-318:

Validation groups

The service automatically applies validation groups based on the operation:
From /home/daytona/workspace/source/integration/crud-typeorm/companies/company.entity.ts:17-32: The service applies:
  • CREATE group for createOne() and createMany()
  • UPDATE group for updateOne() and replaceOne()
Validation happens automatically through NestJS pipes. No additional configuration is needed in the service.

Security features

The service includes built-in SQL injection protection:
From /home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:56-61: All query parameters are validated against these patterns before being executed.

Complete example

Here’s a complete service with custom logic:
From /home/daytona/workspace/source/integration/crud-typeorm/users/users.service.ts:1-13:

Next steps

Controllers

Learn how to configure CRUD controllers

Requests

Understand request parsing and filtering