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
I'm experiencing an issue with dynamic columns in queries when using postgres.js. I have a structure where functions dynamically build the query, as shown below:
functionsendConsult(query){returndbpg` SELECT COUNT(_id) FROM positions WHERE status = 'active' AND company_id = ${companyId}${addFilters('position',companyId,query)} `;}constaddFilters=(source,companyId,query)=>{if(source==='position'){constdate={from: moment(query.from).format('YYYY-MM-DD'),to: moment(query.to).format('YYYY-MM-DD 23:59:59')}constcolumns={one: 'positions."archivedAt"',two: 'positions."releasedAt"'};returndbpg`${filterDateBy(columns,'$case',date)}`;}};constfilterDateBy=(field,operator,date)=>{const{ from, to }=date;constclause={'$case': from&&to ? dbpg`AND CASE WHEN positions.status = 'closed' THEN DATE_TRUNC('day', ${field.one}) > ${from} AND DATE_TRUNC('day', ${field.two}) <= ${to} ELSE DATE_TRUNC('day', ${field.two}) <= ${to} END` : dbpg``,};returnclause[operator];};
When executing the query, I get an error stating that the dynamically passed columns (field.one or field.two) do not exist. However, if I print the generated query (without using the dbpg instance) and execute it directly in DBeaver, it works as expected.
Question:
Is there a native way in postgres.js to handle dynamic columns so the query works correctly? If not, are there any recommendations to address this limitation?
Thank you in advance for the support!
The text was updated successfully, but these errors were encountered:
I'm experiencing an issue with dynamic columns in queries when using postgres.js. I have a structure where functions dynamically build the query, as shown below:
When executing the query, I get an error stating that the dynamically passed columns (field.one or field.two) do not exist. However, if I print the generated query (without using the dbpg instance) and execute it directly in DBeaver, it works as expected.
Question:
Is there a native way in postgres.js to handle dynamic columns so the query works correctly? If not, are there any recommendations to address this limitation?
Thank you in advance for the support!
The text was updated successfully, but these errors were encountered: