Custom Filter
In this lesson, we’ll explore how to simplify and reuse filtering logic in your application using Custom Filters.
Consider the following scenario where we need to filter Order
records by status and a specific year:
If you need to apply this same filter in multiple places throughout your application, this can lead to repetitive code. Rewriting this filter over and over not only increases the likelihood of errors but also makes the code harder to maintain and less readable.
This is where Custom Filters come in handy. Custom Filters allow you to encapsulate and reuse filtering logic, simplifying your code and improving maintainability.
Benefits of Custom Filters
Custom Filters offer several advantages:
- Executes on the Server: The filter is processed on the server, meaning it has the full power of the backend. This allows for more complex operations, such as database queries, and offloads the filtering from the client, improving performance.
- Reusability: You can reuse the same filtering logic in different parts of your application without rewriting it each time.
- Maintainability: If you need to update your filtering criteria, you can do it in one place, ensuring consistency throughout your app.
- Readability: By encapsulating the filter logic, your code becomes cleaner and easier to understand.
- Flexibility: Custom Filters allow you to pass dynamic parameters, making them adaptable to various scenarios.
Defining a Custom Filter in the Order Entity
In the Order
entity, we’ll define a custom filter to encapsulate the filtering logic for active orders within a given year.
Breakdown of the Code
-
Custom Filter Definition:
Filter.createCustom<Order, { year: number }>()
defines a custom filter for theOrder
entity.- The filter accepts an argument object with a
year
property and returns an object representing the filter criteria. - In this case, the filter checks for
Order
records with specific statuses and filters orders within the specified year.
-
Static Method:
- The
activeOrdersFor
is a static method, meaning it belongs to the class itself and can be used without creating an instance of theOrder
class. - This method dynamically generates the filter based on the
year
parameter passed in by the user.
- The
-
Status and Order Date Filters:
- The filter checks if the order’s status is one of the following:
'created', 'confirmed', 'pending', 'blocked', 'delayed'
. - It also filters the orders by their
orderDate
, ensuring only orders from the specified year are included in the results.
- The filter checks if the order’s status is one of the following:
Using the Custom Filter
Once the custom filter is defined, you can use it in your code as follows:
Explanation of Usage
- Simplified Code: By using
Order.activeOrdersFor({ year })
, you’re applying the filtering logic in a clean and reusable manner. You no longer need to duplicate the filtering conditions wherever this logic is required. - Dynamic Parameters: The
{ year }
argument allows the filter to be used with different years, making it adaptable to different contexts. - Backend Evaluation: The filtering is handled on the backend, meaning that you avoid sending large datasets to the client and applying the filters there, which optimizes performance.
Composability: Combining Filters
One of the powerful features of custom filters is their composability. You can combine a custom filter with other filters to create more complex query logic. This is useful when you want to add additional filtering conditions on top of your custom filter.
Example: Combining with an amount
Filter
Let’s say you want to find active orders for a given year, but you also want to filter the orders based on their total amount
. You can easily combine filters using the $and
operator:
Explanation
- Custom Filter (
activeOrdersFor
): Filters the orders by their status and order date for the specified year. - Additional Filter (Amount): The second filter adds an extra condition that only includes orders where the total amount is greater than 100.
- Combining with
$and
: The$and
operator combines both filters, ensuring that only orders that satisfy both theactiveOrdersFor
filter and theamount
filter are included in the results.
Recap
With composable custom filters, you can build modular, reusable filters that combine seamlessly with other conditions, making your code more flexible and maintainable. Whether you’re filtering by status, date, or custom logic like order amount, custom filters allow you to easily manage complex queries with less effort.
- Installing dependencies
- Starting http server