ExpressWebJs, a popular NodeJs framework offers powerful features to simplify application development. One essential aspect of ExpressWebJs is its support for data persistence through repositories. Repositories in ExpressWebJs enable efficient data access and manipulation, promoting clean and maintainable code.
In this article, we'll delve into ExpressWebJs repositories and shed light on three captivating methods: orElseThrow
, isPresent
, and orElse
. These methods are available when fetching single data and they play a crucial role in handling data gracefully within the context of ExpressWebJs repositories.
What is ExpressWebJs Repository?
ExpressWebJs repository is an abstraction built on top of ExpressWebJs DataSource which provides a streamlined way to interact with the underlying data storage. It enables developers to define a set of common operations, reducing the need for boilerplate code. This promotes code reusability, enhances maintainability, and accelerates development.
Also as of the time of writing this, ExpressWebJs currently supports ObjectionJs ORM, TypeORM, and Mongoose. Which means one Repository code for the supported ORMs. No code change when switching from one ORM to the other.
Exploring the Cool Methods
1. orElseThrow
The orElseThrow
method is a game-changer when it comes to handling absent values elegantly. In the context of repositories, it allows us to fetch an entity or throw a specific exception if the entity is not found. This method is particularly useful when dealing with unique entities that should always exist.
public async getEmployeeById(employeeId: string) {
return await this.employeeRepository.findById(employeeId))
.orElseThrow(() => {
new ResourceNotFoundException("Employee not found");
});
}
2. isPresent
The isPresent
method returns true
if a value is present and false
if not. It is a fundamental method to check the existence of a value before performing any actions on it. This method is useful in conditional operations within repository methods.
public async doesEmployeeExist(employeeId: string):Promise<boolean> {
return await this.employeeRepository.findById(employeeId))
.isPresent();
}
3. orElse
The orElse
method provides a fallback value in case the record is empty. It returns the value if present, or the specified default value if the record is empty. This method is handy for setting default values or providing alternative actions.
public async findUserByIdOrFallBack(employeeId: string){
return await this.employeeRepository.findById(employeeId))
.orElse(getDefaultUser());
}
private getDefaultUser(){
// Provide default user logic
}
Conclusion
ExpressWebJs repositories, accompanied by its versatile methods like orElseThrow
, isPresent
, and orElse
, offer a powerful toolkit for efficient data management. These methods enhance error handling, provide seamless operations, and contribute to the overall robustness of your application.
Leverage the strength of ExpressWebJs repositories and the flexibility of its methods to create resilient and feature-rich applications. Happy coding!
Kindly:
Star to support ExpressWebJs on GitHub