@nestjsx/crud-typeorm package provides TypeOrmCrudService with built-in methods for all CRUD operations.
Basic service
A minimal CRUD service extendsTypeOrmCrudService and injects a TypeORM repository:
/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
TheTypeOrmCrudService provides eight main methods that correspond to CRUD operations:
Read operations
getMany()
Retrieve multiple entities with filtering, pagination, sorting, and joins:/home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:94-98:
getOne()
Retrieve a single entity by ID: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: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:/home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:144-158:
Update operations
updateOne()
Partially update an entity (PATCH):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):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):- If
softDelete: trueis set in query options, performs a soft delete - If
returnDeleted: trueis set in route options, returns the deleted entity - Otherwise, performs a hard delete and returns
void
/home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:237-248:
recoverOne()
Recover a soft-deleted entity: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:Query builder
The service uses TypeORM’s query builder internally. You can access it through thecreateBuilder() method:
/home/daytona/workspace/source/packages/crud-typeorm/src/typeorm-crud.service.ts:269-348:
Repository access
The service exposes common repository methods for convenience:/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:DELETE /companies/:idsetsdeletedAtinstead of removing the row- Soft-deleted entities are excluded from queries by default
PATCH /companies/:id/recoverrestores soft-deleted entities- Use
?include_deleted=1to include soft-deleted entities in results
/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:/home/daytona/workspace/source/integration/crud-typeorm/companies/company.entity.ts:17-32:
The service applies:
CREATEgroup forcreateOne()andcreateMany()UPDATEgroup forupdateOne()andreplaceOne()
Validation happens automatically through NestJS pipes. No additional configuration is needed in the service.
Security features
The service includes built-in SQL injection protection:/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:/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