Skip to content

Use consistent $select in all methods, optimize select function #384

Closed

Description

@DaddyWarbucks

The _find method uses Sequelize's attributes option to select properties.

if (filters.$select) {

But it does not use the select function from adapters-commons on its results. All other methods do use this select function but not the attributes option. Is there a reason _get does not also use the attributes option? I don't see why it wouldn't work. The _find method should also use the select function.

If we use attributes in _get, then that should cover most other methods as they call _findOrGet under the hood. But _create uses Model.create and Model.bulkCreate and does not then call _findOrGet. I am curious if these Sequelize methods also support attributes.

If using attributes on all methods, is the select function even needed at that point? Even if not, it should likely be left in place for consistency across all adapter types. On the other hand, should we be using attributes at all and solely relying on the select function?

I am making an assumption that using atrributes on all methods would give us a performance boost. But, it generally makes sense that only selecting the columns we want returned at the SQL level would be better.

Furthermore, we should only use select function when there is a $select filter. Otherwise we are wasting loops. Even though select just returns result => result when there is no $select, with some simple conditionals we could check filters.$select then call select if needed. This is arguably a micro optimization when considering small results, but could make a difference with larger results. See: https://github.com/feathersjs/feathers/blob/dove/packages/adapter-commons/src/index.ts

For example,

return Model.findAll(q)
  .then((results) => {
    if (!filters.$select) {
      return results;
    }
    return select(params, this.id)(results);
  })
  .catch(utils.errorHandler);

Activity

  1. DaddyWarbucks commented on Jan 9, 2022

    @DaddyWarbucks
    ContributorAuthor

    My comments above about "only use select function when there is a $select filter" are incorrect. The function returned from select does not run its results.map function if there are no $select. This is already "optimized".

    But, I don't think we have to call select on methods that call _findOrGet under the hood because they have already been selected. For Example, in the _remove method we are selecting twice

    return (
      this._getOrFind(id, opts)
        .then((data) => {
          return Model.destroy(options).then(() => data);
        })
        // .then(select(params, this.id)) no need for this, _getOrFind handled it
        .catch(utils.errorHandler)
    );

    I also missed that update calls instance.update and could also use potentially use attributes.

  2. DaddyWarbucks commented on Jan 9, 2022

    @DaddyWarbucks
    ContributorAuthor

    Checklist of things to accomplish for this card

    • [] Use the select function on the _find method
    • [] Use the attributes option on the _get method
    • [] Use the attributes option on the create method, provided Modal.create and Model.bulkCreate support it.
    • [] Use the attributes option on the update method, provided instance.update supports it.
    • [] Remove select from the _patch method
    • [] Remove select from the _remove method
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