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
{{ message }}
Repository navigation
Interpolated named parameters for function calls #1040
Glad you think so! I'm really happy using Postgres.js in a project (thank you!) and am finding myself abstracting more and more on the DB side in stored functions.
I might have a few cycles to have a go at it - unless it is particularly easy for some Javascript veteran familiar with the codebase :)
This will be, of course, best answered by the developers, but from looking into the implementation, my guess is that the raw string parser currently isn't set up to recognize that we're translating stored function arguments and just works in the default "SELECT mode". My approach would be to switch the context of the parser when we are translating arguments to functions and generate a list of arguments from an array or a sequence of named arguments from an object.
Good news !
After some more testing, I can confirm both options do work:
// ordered args// func_params: (string | number | boolean)[]constparams=func_params.map((e,i)=>i<func_params.length-1 ? sql`${e}, ` : sql`${e}`);constquery=sql`SELECT * FROM my_func( ${params} )`;// named params// func_params: {[x: string]: string | number | boolean;}const_params=Object.entries(func_params).map(([k,v])=>sql`${sql(k)} => ${v}`);constparams=_params.map((e,i)=>i<_params.length-1 ? sql`${e}, ` : sql`${e}`);constquery=sql`SELECT * FROM my_func( ${params} )`;
Now of course if a new helper is created to help build named params from a dictionary of key-values, all the better.
In which case I suspect it would look like the below - and seemingly easy to implement from the above (but I have no idea how the parser would detect this case and apply the above transformation):
// named params helper// func_params: {[x: string]: string | number | boolean;}constquery=sql`SELECT * FROM my_func( ${sql(func_params)} )`;
In v4 an object in the parentheses of a function call becomes named arguments: make_interval(${ sql({ days: 1, hours: 2 }) }) gives make_interval("days" => $1,"hours" => $2).
Activity
porsager commentedon Mar 3, 2025
Yeah, that's really neat :) Good idea!
ferdv commentedon Mar 4, 2025
Glad you think so! I'm really happy using Postgres.js in a project (thank you!) and am finding myself abstracting more and more on the DB side in stored functions.
I might have a few cycles to have a go at it - unless it is particularly easy for some Javascript veteran familiar with the codebase :)
oscar6echo commentedon Mar 27, 2025
Would it work "manually" like this ?
I'll have to test.
I have the same issue and in my case the following does not work.
It is expected ? or I made a mistake somehow ?
Any hint ?
ferdv commentedon Mar 27, 2025
This will be, of course, best answered by the developers, but from looking into the implementation, my guess is that the raw string parser currently isn't set up to recognize that we're translating stored function arguments and just works in the default "SELECT mode". My approach would be to switch the context of the parser when we are translating arguments to functions and generate a list of arguments from an array or a sequence of named arguments from an object.
oscar6echo commentedon Mar 27, 2025
Ok but in this issue #1026 it seems they succeed in passing args to an SQL function - after fixing a type problem. I'll continue testing...
oscar6echo commentedon Mar 28, 2025
Good news !
After some more testing, I can confirm both options do work:
Now of course if a new helper is created to help build named params from a dictionary of key-values, all the better.
In which case I suspect it would look like the below - and seemingly easy to implement from the above (but I have no idea how the parser would detect this case and apply the above transformation):
porsager commentedon Oct 29, 2026
In v4 an object in the parentheses of a function call becomes named arguments:
make_interval(${ sql({ days: 1, hours: 2 }) })givesmake_interval("days" => $1,"hours" => $2).