top | item 47055362

(no title)

netghost | 12 days ago

Thank you. This looks like a nice improvement on pgtyped, and another good option.

I'm curious if there are any good patterns for dealing with dynamic query building or composing queries?

discuss

order

stephen|12 days ago

Per "how to handle dynamic queries", it's admittedly pretty different b/c we're an ORM (https://joist-orm.io/) that "fetches entities" instead of adhoc SQL queries, but our pattern for "variable number of filters/joins" looks like:

const { date, name, status } = args.filter;

await em.find(Employee, { date, name, employer: { status } });

Where the "shape" of the query is static, but `em.find` will drop/prune any filters/joins that are set to `undefined`.

So you get this nice "declarative / static structure" that gets "dynamically pruned to only what's applicable for the current query", instead of trying to jump through "how do I string together knex .orWhere clauses for this?" hoops.

n_e|12 days ago

I haven’t found a good way to handle dynamic queries in pg-typesafe yet.

For now, I type these manually, which is acceptable for my usage as they are pretty rare compared to static queries.

netghost|11 days ago

That seems like a reasonable tradeoff, thanks.