You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
postgres.js gives us parameterized tagged templates and the ability to nest sql partials, and this is fantastic, but I think it is missing a piece of the puzzle:
I want to be able concat partials together (that may contain query parameters), usually with some delimiter (eg whitespace, or a comma, or maybe an sql operator)
would be amazing if this were a first-class feature in postgres.js
this should allow things like
letdynamicFilters=[sql`somecol = ${somevalue}`,sql`col2 = ${v2}`];constdynamicColumns=[sql('somecol'),sql('col2')]awaitsql` select ${sql.join(dynamicColumns,sql`, `)} from t where ${sql.join(dynamicFilters,sql` and `)};`;
This was insanely helpful, I have a really nested structure so my version is slightly different, and I am monkey patching the sql/database variable.
// Dumb placeholder to shorten codeexporttypePendingQuery<Textendspostgres.Row=postgres.Row>=postgres.PendingQuery<T[]>;// Hack to add the "join" method.exporttypesql=ReturnType<typeofpostgres>&{join: (a: Array<PendingQuery|null>,b: PendingQuery)=>PendingQuery;};constdb=postgres(config)assql;db["join"]=(fragments: (PendingQuery|null)[],joiner: PendingQuery)=>fragments.flatMap((x,i)=>{if(!i)returnx;if(Array.isArray(x)){// already an array, means we're nested, so lets spread itreturn[joiner, ...x];}return[joiner,x];})asunknownasPendingQuery;
It's typescript, but you can strip the types and it should work, usage is:
changed the title [-]postgres.js needs something like slonik's sql.join() for building dynamic queries[/-][+]sql.join() for building dynamic queries[/+]on Jun 27, 2024
It's literally a one line function, which doesn't annoy me at all. Personally, I think it just needs to be documented. Here's my typescript version in case it's useful to anybody (I decided not to mess with the sql variable):
import type { PendingQuery, Row } from 'postgres';
type SqlClause = PendingQuery<Row[]>;
const sqlJoin = (arr: SqlClause[], joiner = sql`, `) => arr.flatMap((x, i) => (i ? [joiner, x] : x));
For a where clause, use it like this:
const whereClauseArray = [sql`id = 'foo'`, sql`name = 'bar'`];
const result = sql`SELECT ... FROM ... WHERE ${sqlJoin(whereClauseArray, sql` AND `)}`;
Activity
porsager commentedon Feb 17, 2024
Yeah, I think it makes sense to add, but I've just been doing this when needed
You can fairly quickly make a function to use instead if you feel like it - eg 😋
I'll keep the issue open for now.
DanielFGray commentedon Feb 27, 2024
oh interesting! i would never have come up with that on my own
do you have any reservations about adding this as a core feature?
DanielFGray commentedon May 15, 2024
and if no reservations, would you accept a PR adding this as a core feature?
davepar commentedon Jun 7, 2024
It would be great to at least add the work-around to the docs. It wasn't obvious to me how to join sql fragments with a comma. Glad I found this.
DanielFGray commentedon Jun 7, 2024
i'm working on a PR
anyone want to bike-shed over
joinvsconcatto avoid confusion with sql joins?or
concat_wsto use actual postgres api 😁Destreyf commentedon Jun 26, 2024
This was insanely helpful, I have a really nested structure so my version is slightly different, and I am monkey patching the sql/database variable.
It's typescript, but you can strip the types and it should work, usage is:
Hopefully this can make it into somewhere in the codebase as it's super useful for report building.
@DanielFGray hopefully this code can help if you haven't figured out this part.
[-]postgres.js needs something like slonik's sql.join() for building dynamic queries[/-][+]sql.join() for building dynamic queries[/+]awildeep commentedon Mar 12, 2025
Any word on making this happen without hacks?
FrancescoDiMuro commentedon Jun 6, 2025
It's crazy annoying to join where clauses.
Any suggestions?
DanielFGray commentedon Jun 10, 2025
this will likely have a better chance of landing if we can fix tests
if you're interested in helping, you can send a PR to my PR on the appropriate branch
my favorite api for this, which is mentioned in my PR but not implemented:
i do have a branch where this is implemented but it also needs types and tests
davepar commentedon Jun 10, 2025
It's literally a one line function, which doesn't annoy me at all. Personally, I think it just needs to be documented. Here's my typescript version in case it's useful to anybody (I decided not to mess with the sql variable):
For a where clause, use it like this:
porsager commentedon Oct 29, 2026
v4 has it:
sql\,`.join(items)`.