Encapsulating text filter

A method for checking if a text contains a fragment.

Before finishing this section, let's just make two improvements. First, we are finding products whose name contains a received text. Well, this behavior is quite common, and is probably going to appear elsewhere at some moment. So, it would be interesting to encapsulate this logic. Let's then begin by creating a service for filtering.

nest g s querying/filtering --flat

Already export this service, as it will be used afterwards.

In it, we may create a method called contains(), a simple and intuitive name. If we also check there if the text exists, we avoid having to manually return undefined.

contains(text: string) {
  if (!text) return;

  return ILike(`%${text}%`);
}

Now, we just need to inject the filteringService and use it.

name: this.filteringService.contains(name),

Commit - Method to check if field contains text

Last updated