Skip to main content

Overview

While the framework provides TypeOrmCrudService out of the box, you can create custom service implementations for other ORMs (like Mongoose, Prisma, Sequelize) or even non-database data sources (like REST APIs, GraphQL, etc.).

Creating a Custom Service

To create a custom CRUD service, extend the abstract CrudService class and implement all required methods.

Basic Structure

Mongoose Example

Here’s a complete example of a CRUD service for Mongoose:

Prisma Example

Here’s an example for Prisma:

REST API Backend Example

You can even create a CRUD service that communicates with a REST API:

Using Your Custom Service

Once you’ve created your custom service, use it in your controller:

Best Practices

Reuse Base Utilities: Always use the inherited utility methods like decidePagination, getTake, getSkip, and createPageInfo for consistent behavior.
Error Handling: Use throwBadRequestException and throwNotFoundException for consistent error responses across your application.
Validation: Make sure to validate input data before processing. The framework doesn’t automatically validate DTOs in custom services.
Transaction Support: If your data source supports transactions, consider implementing transaction handling in your custom service for data consistency.

Advanced Customization

Override Pagination Format

You can override createPageInfo to customize the pagination response:

Add Custom Methods

Extend your service with custom methods for specific use cases:

See Also