ts-safeql / safeql

Validate and auto-generate TypeScript types from raw SQL queries in PostgreSQL.
https://safeql.dev
MIT License
1.35k stars 22 forks source link

Apply `connections.targets.fieldTransform` to JSON / JSONB data #278

Open karlhorky opened 1 month ago

karlhorky commented 1 month ago

Is your feature request related to a problem? Please describe.

The option connections.targets.fieldTransform: 'camel' is not applying to JSON fields like it does with Postgres.js

Code below assumes config of connections.targets.fieldTransform: 'camel'

// 💥 Query has incorrect type annotation.
//  Expected: { contact: { userId: number } }[]
//    Actual: { contact: { user_id: number } }[]
await sql<{ contact: { userId: number } }[]>`
  SELECT
    jsonb_build_object('user_id', users.id) AS contact
  FROM
    users
`;

Describe the solution you'd like

Apply connections.targets.fieldTransform to nested JSON / JSONB fields, either as:

  1. Default behavior
  2. An option (default on?)

Describe alternatives you've considered

Provide a different option (eg. jsonFieldTransform) to allow for additional customization.

Additional context

--

karlhorky commented 1 month ago

Workaround

Manually specify camel case field names in the JSON / JSONB:

// ✅
await sql<{ contact: { userId: number } }[]>`
  SELECT
    jsonb_build_object('userId', users.id) AS contact
  FROM
    users
`;