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

Use attributes exclude #361

Closed
DaddyWarbucks opened this issue Feb 5, 2021 · 1 comment
Closed

Use attributes exclude #361

DaddyWarbucks opened this issue Feb 5, 2021 · 1 comment

Comments

@DaddyWarbucks
Copy link
Contributor

Sequelize allows you to exclude attributes by providing an object to attributes instead of an array.
See: https://sequelize.org/v5/manual/querying.html

But the following line overwrites q.attributes with the filter.$select

if (filters.$select) {

This means that even if the user passed the following object

context.params.sequelize = {
  attributes: {
    exclude: ['myProp']
  }
}

The exclude will be totally overwritten with the filter.$select

Instead the code should be updated to something like

if (filters.$select) {
  if (!q.attributes) {
    q.attributes = filters.$select
  } else if (Array.isArray(q.attributes)) {
    q.attributes = [...q.attributes, ...filters.$select];
  } else {
    q.attributes = {
      ...q.attributes,
      include: filters.$select
    }
  }
}

NOTE: I have not actually tested this but stumbled across it while working on something. I will write a test to confirm and make a PR shortly

@DaddyWarbucks
Copy link
Contributor Author

Closing this issue. The comments made above are true. When using $select it does totally overwrite the attributes property. But, that makes sense because you would never really use both select and exclude at the same time.

As a note to future readers. If you need to use sequelize.attributes, do not also use query.$select.

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