Sort search and group for fields on included models #57
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes 3 Issues for
list.js
I've run into while dealing with included/associated models in a resource. I've tried to isolate these 3 problems in tests but the behavior changes based on if the resource hasoptions.subQuery = true/false
set and if the associations between models are one-to-many or many-to-many. Ideally I'm looking to be able to sort, search, and group by fields on included models at the same time.1. Group By breaks pagination
Current Version
If you use a group command (my test example is
context.options.group = ['users.id']
) then the sequelizefindAndCountAll()
method returns a list of grouped counts instead of one count number.Change
If
options.group
is set then split thefind
andcount
into two queries.2. Sort by field on associated model
Current Version
Sorting by a field on an included model doesn't work. Sequelize uses the wrong table name for the nested field when generating SQL
Change
I use a Sequelize.literal() in the sort if the field path has a period in it.
3. Search with "LIKE" on a associated resource
Current Version
If you want to search for a field on an associated resource you have the following options
?profile.nickname=name
Sequelize.Op.eq
operator, otherwise the search is picked up as astringOperator
inlist.js:115
and throws an error because the path to the included model isn't a sequelize.STRINGChange
I exclude search keys with a period in the serach for stringOperators to allow using
Sequelize.Op.like
in the search definition in the resourceNote: the field name in the search definition still requires the dollars around the field e.g.
$profile.nickname$