kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.22k stars 259 forks source link

Add support for json_agg_strict (PostgreSQL only) #892

Open leibale opened 6 months ago

leibale commented 6 months ago

Add support for json_agg_strict (PostgreSQL only) - this function is just like json_agg, but skips null (see https://www.postgresql.org/docs/current/functions-aggregate.html)

A few notes:

  1. I've duplicated all json_agg tests and changed them to use json_agg_strict, but I'm not sure if:
    1. Should we test all cases for both json_agg and json_agg_strict?
    2. If we want to fully test both, do you prefer duplicating the code, using a loop, or using a function?
  2. json_agg might return null (for example, when using left join and there is nothing to join with), but the types ignore that.. I guess fixing it is a breaking change, so I did not include it in this PR

Thanks for this awesome package! :)

vercel[bot] commented 6 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 21, 2024 10:28am
leibale commented 6 months ago

When using it with left join every field becomes nullable (I think that left join changes DB[table] to a "nullable version"), which is not true for json_agg (which will reply with [null] for "empty join", or json_agg_strict (which will reply with [] for "empty join"). I'm not too sure if this will be easy to solve.. any help will be appreciated :)