Dynamic filtering is a must have feature in an app that comes to replace our company use in Google sheets.
A user can select values from multiple multi select and when he clicks on 'Apply' button the following PostgreSQL query gets triggered:
SELECT *
FROM ports
WHERE
(
{{ !portsPhoneNumberInput.value }} OR phone_number ILIKE {{ '%' + portsPhoneNumberInput.value + '%' }}
)
AND ( carrier_id = ANY( {{ portsCarriersSelect.value.length === 0 ? portsCarriersSelect.data.map(val => val.id).concat([null]) : portsCarriersSelect.value}})
)
AND... (more multi selects)
;
So, if the user didn't select any value, I will pass all the possible values to the ANY
statement. that would work, except that we also have NULL
values in there, and ANY
cannot valuate NULL
therefor the result is not as expected. (PostgreSQL calculate NULL
as UNKNOWN
while ANY
only returns TRUE
or FALSE
)
One thing we can do is to eliminate NULL
values by having a DEFAULT
set to an id that is treated like null - that would work but may cause bugs in the future if we forget about it, and in general it's not good practice.
Do you have a better solution?
Any help would be much appreciated!
P.S. while writing this I started wondering if !portsCarriersSelect.value
can replace portsCarriersSelect.value.length === 0
.