Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort not working #34

Open
arthurquerou opened this issue Jan 25, 2017 · 7 comments
Open

Sort not working #34

arthurquerou opened this issue Jan 25, 2017 · 7 comments

Comments

@arthurquerou
Copy link

arthurquerou commented Jan 25, 2017

It seems that sort conditions are not taken into account.

In index.js in the Sort section you have this code:

var sort = _.keys(options.sort)[0];
      if (sort) {
          query.ascending();
        }
        else if (sort == -1) {
          query.descending();
        }

It can't even work as sort equals the key and not the value.

Are there any plan to update this behaviour ?

@ferrants
Copy link
Collaborator

ferrants commented Feb 9, 2017

Trying to work this out myself as well.
According this library: https://github.com/ryanfitz/vogels,
For non-scanning queries, sort requires indicating an Index to use if you're not using the primary index of the table. You can't or shouldn't be able to sort on an arbitrary field using dynamo unless you're using scan. The ascending/descending should respect the range field of the index you're using.

Looks like the index is set around line 546: https://github.com/gadelkareem/sails-dynamodb/blob/master/index.js#L546

          if (indexName && indexName != 'primary') {
            query.usingIndex(indexName);
          }

Regardless of this, the lines you posted don't make sense for the handling of the sort. It gets the first key in the sort dictionary and expects that to be -1 for descending or 1 for ascending. The number -1 can not be the key of a dictionary, so you can never have descending.
sort: {-1: something} doesn't make sense. I think it should be:

sort: {indexName: -1}

to sort that index in a descending way.

I made #35 to see if that works

@ferrants
Copy link
Collaborator

ferrants commented Feb 9, 2017

You can use sort: "-1" to descendingly sort based on whatever index it picks up. The sort variable ends up being { '-1': 1 }, which seems to work. My Pr didn't work.

@ferrants
Copy link
Collaborator

I made #36 for a documentation update to show how this feature works for this adapter

@jkeczan
Copy link

jkeczan commented Mar 13, 2017

When I sort by a -1, I am getting query.descending not a function defined on line 897.

Any thoughts?

TypeError: query.descending is not a function
    at Object._searchCondition (/Users/jkeczan/Projects/web/external/straingauge-web/node_modules/sails-dynamodb/index.js:897:17)
   

@ferrants
Copy link
Collaborator

My thoughts are that you may not have an index on what you are searching by or ordering by so it's trying to do it in scan mode instead of query mode.

@ggarcia92
Copy link

Hi, I'm trying to sort by updatedAt but when I define an index to that attribute on model and lunch local dynamodb throws this error "Cannot read property 'dynamoType' of undefined". Is this a bug?

@ferrants
Copy link
Collaborator

ferrants commented Apr 4, 2018

Doesn't sound like a bug or this open issue. Sounds like a configuration issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants