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

orderBy Query Parameter #320

Closed
edgar971 opened this issue Jan 28, 2017 · 3 comments
Closed

orderBy Query Parameter #320

edgar971 opened this issue Jan 28, 2017 · 3 comments

Comments

@edgar971
Copy link

Hi,

I was searching through the documentation and example tutorials and I couldn't figure out how to use sortBy with query parameters. I want to be able to make API calls like http://localhost:8080/v1/sites?__orderBy=created_at,DESC to have a custom sort. Is there a way to do this natively with Nodal? I also tried the following versions:

http://localhost:8080/v1/sites?__orderBy=created_at|DESC
http://localhost:8080/v1/sites?orderBy=created_at,DESC
http://localhost:8080/v1/sites?orderBy=created_at|DESC

The controller looks like this:

Site.query()
            .where(this.params.query)
            .end((err, models) => {


                this.respond(err || models);

            });

Thanks!

@nogsantos
Copy link

By Query composer you have the .orderBy() method, something like...

...
Customer.query()
   .where(this.params.query)
    .join('phones')
    .limit(this.params.query.offset, process.env.QUERY_LIMIT)
    .orderBy("id", "DESC") // here you put your params
    .end((err, cliente) => {
...

Just send the values what you want in body or, like you said, in you query string.

You have another options, see http://nsipplswezey.github.io/nodal/docs/graphql.html

@edgar971
Copy link
Author

@nogsantos Thanks for the help.

I ended up doing the following:

Created a function to parse the orderBy query.

parseOrderBy(query) {

        let orderBy = [];

        if (!query.__orderBy && query.__orderBy.length) return false;

        orderBy = query.__orderBy.split('|');

        return orderBy;

    }

Used the function inside my index method.

let order = this.parseOrderBy(this.params.query);
        console.log(order);

        Site.query()
            .where(this.params.query)
            .orderBy(...order)
            .end((err, models) => {


                this.respond(err || models);

            });

That worked for me.

@maxism
Copy link
Contributor

maxism commented Feb 1, 2017

@edgar971 you should use __order instead of __orderBy param in your HTTP request. So in your case it will be looks like http:///localhost:8080/v1/sites?__order=created_at desc.

This issue was closed.
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