I'm currently creating a NodeJS website using PostgreSQL via pg-promise.
I have a page with an HTML form with checkboxes to select variables to search the database for using various fields. These are then fed into a SQL query with pg-promise
and the intended behaviour is the results are passed back to the user in JSON format.
A very minimal working example would be as follows.
HTML form:
<form action="/search" method="get">
<fieldset>
<legend>Variable A</legend>
<div>
<input type="checkbox" name="variable_a" value="apple">
<label for="variable_a">
Apple
</label>
</div>
<div>
<input type="checkbox" name="variable_a" value="orange">
<label for="variable_a">
Orange
</label>
</div>
</fieldset>
<fieldset>
<legend>Variable B</legend>
<div>
<input type="checkbox" name="variable_b" value="pear">
<label for="variable_b">
Pear
</label>
</div>
<div>
<input type="checkbox" name="variable_b" value="banana">
<label for="variable_b">
Banana
</label>
</div>
</fieldset>
<button type="submit">Search</button>
</form>
From this an URL like the following is created /search?variable_b=pear&variable_b=banana
The problem I have is when trying to create a 'catch all' SQL SELECT query to handle this search.
This is the SQL query I have created in pg-promise
:
router.get('/search', function(req, res, next) {
db.any(`SELECT * FROM food
WHERE variable_a IN ($1:csv)
AND variable_b IN ($2:csv)`, [req.query.variable_a, req.query.variable_b])
.then(result=>res.send(result))
.catch();
});
This fails given the /search?variable_b=pear&variable_b=banana
URL, but works with say the following URL /search?variable_a=apple&variable_b=banana
.
This is undoubtedly because in the example above req.query.variable_a
is undefined as no checkboxes were selected and the SQL query falls over with IN ()
. I should perhaps add, if variable_a
or variable_b
isn't defined by a checkbox, in this case the intended behaviour is there is no filter on said columns.
My question is what is the best way of handling this?
I feel like I could probably create a lot of if/else logic to handle potential undefined req.query
variables and resulting SQL queries but this seems messy and not elegant.