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

New find select object api should support false values as expected #8796

Closed
cduff opened this issue Mar 24, 2022 · 1 comment · Fixed by #8807
Closed

New find select object api should support false values as expected #8796

cduff opened this issue Mar 24, 2022 · 1 comment · Fixed by #8807

Comments

@cduff
Copy link
Contributor

cduff commented Mar 24, 2022

Feature Description

The Problem

As per https://typeorm.io/#/find-options/basic-options, typeorm now supports syntax like:

userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
})

However, if you change one of the values to false, like:

userRepository.find({
    select: {
        firstName: true,
        lastName: false, // note false
    },
})

Then it makes no difference. lastName is still selected which does not seem intuitive.

However, if the value is specified as undefined then the field will not be selected:

userRepository.find({
    select: {
        firstName: true,
        lastName: undefined, // note undefined
    },
})

It would be great if false was handled intuitively so that simple boolean conditions can be specified for conditional selection.

Note that the relations option has a similar interface and it does handle false values as expected.

The Solution

Could be simple as changing the following line:

if (select[key] === undefined) continue

to:

if (!select[key]) continue;

???

Considered Alternatives

undefined can be specified instead of false but this complicates code for conditional selects.

The select implementation is also inconsistent with the relations implementation which does handle false values as expected.

Additional Context

The new select and relations API was recently released in https://github.com/typeorm/typeorm/releases/tag/0.3.0.

Relevant Database Driver(s)

n/a

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@pleerock
Copy link
Member

I suggest to simply do it as:

 if (select[key] === undefined || select[key] === false) continue 

and add test for this change. Simply to contribute.

@chfleury chfleury mentioned this issue Mar 26, 2022
7 tasks
pleerock pushed a commit that referenced this issue Mar 26, 2022
* fix: select supports false value instead of only undefined

* test: issue-8796

* refactor: add test description

* refactor: format code
M-TGH pushed a commit to TradeCast/typeorm that referenced this issue Mar 29, 2022
…ypeorm#8807)

* fix: select supports false value instead of only undefined

* test: issue-8796

* refactor: add test description

* refactor: format code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants