Sorting API on latest data in bubble DB

How do I sort the API response to get the 10 latest db values from the bubble data api?

What values should go where for the API to pull in the latest rows from the DB?


This is not working… :neutral_face:

Please send a bug report to Bubble and report back what they tell you.

I’ve been trying to get the sort by created date to work for 4 years now…I’ve asked which way the parameter value needs to be (this is because in Bubble they are not consistent with naming conventions and in some places it is CreatedDate and in others it is CreationDate - or some variation of them). I have never heard back despite two bug reports on the topic as to what is the proper format for sorting by created date

Support ticket sent :+1:

Looking at the image, am I using the parameters correctly - are they in the correct place (the parameters section)?

Read that additional_sort_fields should be used if multiple parameters are used, but how should that be added to the api plugin?

Or perhaps there is another way of getting the 10 latest entries in the db?

That is implying if you want to sort based off more than one parameter I believe.

Below is an example of how mine is setup and it works.

Screen Shot 2023-11-16 at 2.39.56 PM

The constraints portion is for filtering, not sorting, so for your use case that can be ignored.

Yeah it never did work. Are you locked in on the Data API and won’t consider a regular API Workflow?

Well my reason behind using the data API was to speed up showing a list of the 100 latest entries in the DB, the sorting using the DB directly is a bit slow, and it seems to need loads of data I have seen. The result seems to be alot faster over the API, the only thing I dont get working is the sorting using date. :-/

Eureka moment here, got it working using the key additional_sort_fields
:partying_face:
Now I get the latest DB entries first, now to get some sort of pagination working :smiley:

2 Likes

Not just create date. I had to do this to get sorting descending working on another date field as well.

Did NOT work:

    {
        constraints: [
          {
            key: "Team Member",
            constraint_type: "equals",
            value: user._id,
          },
        ],
        sort_field: "Cyclet Start",
        descending: true,
        limit: 1,
    }

DID work:

    {
        constraints: [
          {
            key: "Team Member",
            constraint_type: "equals",
            value: user._id,
          },
        ],
        additional_sort_fields: [{
          sort_field: "Cyclet Start",
          descending: true,
        }],
        limit: 1,
    }

This does not align with the docs. I’m not sure if the bug is specific to date fields or if it applies more broadly to sorting descending, but @svensson.adam I owe you a huge thank you for sharing this!!