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 proper $and in patch and remove methods #383

Closed
DaddyWarbucks opened this issue Jan 9, 2022 · 3 comments
Closed

Use proper $and in patch and remove methods #383

DaddyWarbucks opened this issue Jan 9, 2022 · 3 comments

Comments

@DaddyWarbucks
Copy link
Contributor

@fratzinger Made an excellent PR recently that ensures the user's $and query is not overwritten. See 8de0144

We should similarly update the _patch and _remove methods to do so. Note that this issue should probably be accomplished first: #382. Right now the _patch does not use and $and query, but if we update it to work more similarly to _remove then it will use an $and query I believe.

@DaddyWarbucks
Copy link
Contributor Author

Also, we likely need to do some more type checking on the $and. Note in this line that we assume the $and is an array. But the code also potentially returns an object instead of an array.

[and]: where[and] ? [...where[and], { [this.id]: id }] : { [this.id]: id }

So if $and works with an object and not just an array, when we need to check that. I think it is most common that its an array so we should also return an array.

Something like

applyAndQuery(where, id) {
  const { and } = this.Op;
  const IdQuery = { [this.id]: id };
  const newWhere = Object.assign({}, where);

  if (!newWhere[and]) {
    newWhere[and] = [IdQuery]
    return newWhere;
  }

  if (Array.isArray(newWhere[and])) {
    newWhere[and] = [...newWhere[and], idQuery];
    return newWhere;
  }

  newWhere[and] = [newWhere[and], idQuery];
  return newWhere;
}

@fratzinger
Copy link
Contributor

Good catch! We should add a test for that. Maybe even on @feathersjs/adapter-tests and we could add $and to the official supported operators list at https://docs.feathersjs.com/api/databases/querying.html.

Op.and and Op.or can be used as an object, indeed (see https://sequelize.org/v6/manual/model-querying-basics.html#examples-with--code-op-and--code--and--code-op-or--code-). But I believe at the root level they're always meant to be arrays. It makes sense if they're nested within a column query like:

where: {
  rank: {
    [Op.and]: {
      [Op.lt]: 1000,
      [Op.eq]: null
    }
  },
}

@fratzinger
Copy link
Contributor

resolved by #429

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

2 participants