Dynamic queries
pgTyped doesn't support query composition or concatenation, but this doesn't mean you can't create dynamic queries. Instead of providing non-typesafe query composition, pgTyped forces you to move the dynamic logic into the SQL layer.
WHERE
filters#
Dynamic A frequently used pattern is a query with an optional filter that selects all rows by default.
This can be achieved using a IS NULL
construct.
Here is an example of a query with optional age
and name
filters:
ORDER BY
sorting#
Dynamic Sorting by a dynamic column is another widely used dynamic query:
Next, if we want to include a dynamic sort order as well:
#
Advanced dynamic queriesMore complicated dynamic queries can be built similarly to the above two.
Note that highly dynamic SQL queries can lead to worse DB execution times, so sometimes it is worth to split a complex query into multiple independent ones.