Home > Software design >  Firestore Pagination: how to define 'startAt'-cursor for REST?
Firestore Pagination: how to define 'startAt'-cursor for REST?

Time:02-24

I am trying to use a cursor and 'startAt', to paginate REST requests to Firestore. According to the Paginate-documentation, the cursor should equal to the last document of the previous query. As the REST-documentation is without an example, I tried to run it by inserting an entire document as cursor in the startAt-object; like this:

POST https://firestore.googleapis.com/v1/PROJECT-NAME/databases/(default)/documents/organizations/testManyInstructions:runQuery
{
    "structuredQuery": {
        "from": [
            {
                "collectionId": "instructions"
            }
        ],
        "where": {
            "fieldFilter": {
                "field": {
                    "fieldPath": "belongsToDepartementID"
                },
                "op": "EQUAL",
                "value": {
                    "stringValue": "toplevel-document-id"
                }
            }
        },
        "orderBy": [ 
          { 
              "field": { 
                  "fieldPath": "instructionNumber" 
              }, 
              "direction": "ASCENDING" 
          } 
        ], 
        "startAt": {
            "values": [{
              "document": {
                  "name": "projects/PROJECT-NAME/databases/(default)/documents/organizations/testManyInstructions/instructions/i0",
                  "fields": {
                      "checkbox": {
                          "booleanValue": false
                      },
                      "retrainTimespanDays": {
                          "integerValue": "365000"
                      },
                      "approvedByName": {
                          "stringValue": ""
                      },
                      "instructionNumber": {
                          "stringValue": "instr. 0"
                      },
                      "instructionCurrentRevision": {
                          "stringValue": "A"
                      },
                      "instructionCurrentRevisionPublishingDate": {
                          "timestampValue": "1999-01-01T00:00:00Z"
                      },
                      "instructionFileURL": {
                          "stringValue": ""
                      },
                      "instructionTitle": {
                          "stringValue": "dummy Title0"
                      },
                      "instructionFileUploadDate": {
                          "timestampValue": "1999-01-01T00:00:00Z"
                      },
                      "belongsToDepartementID": {
                          "stringValue": "toplevel-document-id"
                      },
                      "approvedByEmailAdress": {
                          "stringValue": ""
                      }
                  },
                  "createTime": "2022-02-18T13:55:42.807103Z",
                  "updateTime": "2022-02-18T13:55:42.807103Z"
              }
            }
          ]
        },
        "limit": 5
    }
}
  • without the "startAt"-Object, the following code works fine and returns 5 documents.
  • with the "startAt"-Object, this error is returned:
    {
        "error": {
            "code": 400,
            "message": "Invalid JSON payload received. Unknown name \"document\" at 'structured_query.start_at.values[0]': Cannot find field.",
            "status": "INVALID_ARGUMENT",
            "details": [
                {
                    "@type": "type.googleapis.com/google.rpc.BadRequest",
                    "fieldViolations": [
                        {
                            "field": "structured_query.start_at.values[0]",
                            "description": "Invalid JSON payload received. Unknown name \"document\" at 'structured_query.start_at.values[0]': Cannot find field."
                        }
                    ]
                }
            ]
        }
    }
]

Please advise, how to set the cursor in the startAt-object correctly.

CodePudding user response:

I've run a similar query using offset instead of startAt, so I tried modifying and got it to work. This is the rest api documentation I used. startAt requires a Cursor object which is an array of Values. https://firebase.google.com/docs/firestore/reference/rest/v1/StructuredQuery https://firebase.google.com/docs/firestore/reference/rest/v1/Cursor https://firebase.google.com/docs/firestore/reference/rest/Shared.Types/ArrayValue#Value

I would have preferred an example as well!

    "startAt": {
        "values": [{
            "stringValue": "Cr"
        }]
    },
    "orderBy": [{
        "field": {
            "fieldPath": "Summary"
        }
    }],

Good luck!

  • Related