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

Support json_agg(column_ref). #1280

Open
igalklebanov opened this issue Nov 24, 2024 · 1 comment
Open

Support json_agg(column_ref). #1280

igalklebanov opened this issue Nov 24, 2024 · 1 comment
Labels
api Related to library's API enhancement New feature or request postgres Related to PostgreSQL typescript Related to Typescript

Comments

@igalklebanov
Copy link
Member

Hey 👋

Currently eb.fn.jsonAgg supports table names and expressions as inputs. In such cases, the return type is a JSON object array.

json_agg also supports passing a column reference. In such cases, the return type is a literal array.
We should support this.

const result = await db
  .selectFrom('person')
  .innerJoin('pet', 'pet.owner_id', 'person.id')
  .select((eb) =>
    eb.fn.jsonAgg('pet.name').as('person_pets')
  )
  .executeTakeFirstOrThrow()

The return type would be { person_pets: string[] }.

@igalklebanov igalklebanov added enhancement New feature or request postgres Related to PostgreSQL api Related to library's API typescript Related to Typescript labels Nov 24, 2024
@koskimas
Copy link
Member

koskimas commented Dec 2, 2024

Good idea! jsonAgg is also quite broken with left joins, and there's nothing we can do about it easily. Not related to this issue though.

We express left joined tables in the DB generic as tables where each column is nullable. While this works in most cases, in case of jsonAgg it produces objects with nullable properties when it should produce nullable objects with normal property types 😬

We have no way to tell if a column is nullable in DB because it's actually nullable or if it was put there using a left join.

We could express left joined tables in DB like this

{
  person: Person,
  leftJoined: Pet | null
}

But that's a big type overhaul and most likely a breaking change.

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 typescript Related to Typescript
Projects
None yet
Development

No branches or pull requests

2 participants