Skip to content

Use proper $and in patch and remove methods #383

Closed

Description

@DaddyWarbucks

@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.

Activity

  1. DaddyWarbucks commented on Jan 9, 2022

    @DaddyWarbucks
    ContributorAuthor

    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;
    }
  2. fratzinger commented on Jan 9, 2022

    @fratzinger
    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
        }
      },
    }
  3. fratzinger commented on May 12, 2024

    @fratzinger
    Contributor

    resolved by #429

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions