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 for FILTER Clauses #211

Open
karlhorky opened this issue Apr 28, 2024 · 0 comments
Open

Support for FILTER Clauses #211

karlhorky opened this issue Apr 28, 2024 · 0 comments

Comments

@karlhorky
Copy link
Collaborator

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

PostgreSQL FILTER clauses don't currently change the type of the resulting rows

Eg. animalFoods.id can also be null here, even though the FILTER clause makes this impossible:

const [animal] = await sql<{ animalFoods: { id: number | null }[]; }[]>`
  SELECT
    json_agg(foods.*) FILTER (WHERE foods.id IS NOT NULL) AS animal_foods
  FROM
    animals
    LEFT JOIN animal_foods ON animals.id = animal_foods.animal_id
    LEFT JOIN foods ON foods.id = animal_foods.food_id
  WHERE
    animals.id = ${id}
`;

Describe the solution you'd like

FILTER clauses should be respected and the type should be altered / narrowed based on the logic in them

Eg. animalFoods.id is only number here:

const [animal] = await sql<{ animalFoods: { id: number }[]; }[]>`
  SELECT
    json_agg(foods.*) FILTER (WHERE foods.id IS NOT NULL) AS animal_foods
  FROM
    animals
    LEFT JOIN animal_foods ON animals.id = animal_foods.animal_id
    LEFT JOIN foods ON foods.id = animal_foods.food_id
  WHERE
    animals.id = ${id}
`;

Describe alternatives you've considered

Other constructs for filtering

Additional context

Original issue with more context:

@karlhorky karlhorky mentioned this issue Apr 28, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant