data-provider/core

More homogeneous Selector interface

javierbrea opened this issue · 0 comments

Currently, the selector interface is different for defining dependencies than for defining the last function, which can be considered the "selector" itself, because it receives all of the results of the dependencies. But dependencies receive also the results from the previous dependencies, but in a different format (in a single argument, as an array of results, while the last function receives them as different arguments).

Well, as the selector function is also able to return another dependency, there is no a big difference between the rest of dependencies, and it may be hard to understand why the last function receives the dependencies results in one format, and the other ones in another. This was not intentional in a first moment, but the evolution of the library, allowing selectors to return another selectors, have taking us to this point.

In order to simplify the interface, and for a better comprehension (I hope), here is a proposal in which there are no distinction between the arguments that all functions in a Selector receive. All functions in a Selector will receive previous results as different arguments, ordered by the order of the dependencies itself.

Note also that the query argument have been moved to the first place to avoid making its position dependant of the number of previous dependencies.

const booksOfAuthor = new Selector(
  (query) => authorsModel.query(query),
  (query, authorData) => books.queries.byAuthorId(authorData.id),
  (query, authorData, booksData) => {
    return booksData.map(book => {
      return {
         ...bookData,
         authorName: authorData.name
      };
    });
  }
);