porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.03k stars 255 forks source link

Unable to query structured data #900

Open mnunezdm opened 2 weeks ago

mnunezdm commented 2 weeks ago

Hello!

I'm having problems when accessing / filtering on / ordering by I'm able to query with a hardcoded query from a file or using the unsafe method, but... when trying to use it dynamically, I'm not able to write the correct code, I have looked into the documentation but I did not find anything

select
  identityMap['contactid'].id,
  segmentmembership['ups']['06e0ac99-d525-492b-8a34-c6a4f09d6b6e'].lastQualificationTime,
  segmentmembership['ups']['06e0ac99-d525-492b-8a34-c6a4f09d6b6e'].status
from
  profile_snapshot_export_fdcfa371_e04e_4206_bcf0_a07ce88ed172
where
  segmentmembership['ups']['06e0ac99-d525-492b-8a34-c6a4f09d6b6e'].status != ''
order by segmentmembership['ups']['06e0ac99-d525-492b-8a34-c6a4f09d6b6e'].lastQualificationTime desc

This is running code but its using the unsafe method...

const response: IdentityMap[] = await sql`
  select
    ${sql.unsafe(
      [
        `identityMap['contactid'].id`,
        `segmentmembership['ups']['${segmentId}'].lastQualificationTime`,
        `segmentmembership['ups']['${segmentId}'].status`,
      ].join(','),
    )}
  from
    ${sql(TABLE_BY_ENV[argv.entorno])}
  where
    ${sql.unsafe(`segmentmembership['ups']['${segmentId}'].status`)} != ''
  order by ${sql.unsafe(
    `segmentmembership['ups']['${segmentId}'].lastQualificationTime`,
  )} desc
  `