Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for json_agg_strict (PostgreSQL only) #892

Open
wants to merge 2 commits into
base: v0.28
Choose a base branch
from

Conversation

leibale
Copy link

@leibale leibale commented Feb 29, 2024

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! :)

Copy link

vercel bot commented Feb 29, 2024

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
Copy link
Author

leibale commented Mar 1, 2024

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 :)

@igalklebanov igalklebanov added enhancement New feature or request postgres Related to PostgreSQL api Related to library's API labels Mar 1, 2024
@igalklebanov
Copy link
Member

Hey 👋

Thanks! 🙏

I'm on the fence on this one for now:

  • This only collected 2 reactions in almost a year.
  • It is supported just in PostgreSQL.
  • It can be achieved in other ways:
const results = await db
  .selectFrom("person")
  .leftJoin("pet", "pet.owner_id", "person.id")
  .groupBy("person.first_name")
  .select((eb) => [
    "first_name",
    eb.fn
      .coalesce( // <-- ensures defaults to empty array instead of null
        eb.fn
          .jsonAgg("pet")
          .filterWhere("pet.owner_id", "is not", null), // <-- removes null rows
        sql.lit("[]"),
      )
      .as("pets"),
  ])
  .$narrowType<{ pets: Selectable<PetTable>[] }>() // <-- this makes all pet fields not nullable in a type-safe way.
  .execute();

https://kyse.link/ZIU7y

@igalklebanov igalklebanov changed the base branch from master to v0.28 January 5, 2025 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request postgres Related to PostgreSQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants