feathers-sequelize icon indicating copy to clipboard operation
feathers-sequelize copied to clipboard

Use consistent $select in all methods, optimize select function

Open DaddyWarbucks opened this issue 3 years ago • 2 comments

The _find method uses Sequelize's attributes option to select properties. https://github.com/feathersjs-ecosystem/feathers-sequelize/blob/db4b5ff9b89df9b163f9cd5d8c32de8c915ff71c/lib/index.js#L135 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);

DaddyWarbucks avatar Jan 09 '22 20:01 DaddyWarbucks

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.

DaddyWarbucks avatar Jan 09 '22 21:01 DaddyWarbucks

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

DaddyWarbucks avatar Jan 09 '22 21:01 DaddyWarbucks