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

Add a cursor to go to the first page #42

Merged
merged 6 commits into from
Sep 4, 2024

Conversation

GCorbel
Copy link
Contributor

@GCorbel GCorbel commented Sep 3, 2024

I added first_cursor to Mongoid::Scroll::Scrollable::Iterator.

first_cursor = nil

cursor = cursor_type.from_record Feed::Item.last, field_name: field_name, field_type: field_type

Feed::Item.asc(field_name).limit(2).scroll(cursor) do |_, iterator|
  first_cursor = iterator.first_cursor
end

Feed::Item.asc(field_name).limit(2).scroll(first_cursor) do |record|
  # loop over the 2 first records.
end

This is honestly kind of the same than Feed::Item.asc(field_name).limit(2) but allows to keep sort options encrypted the same way it is for other kind of cursors.

We use it to have this kind of payload for an API response :

GET {{baseUrl}}/attachments?pageToken=eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlRpbWUiLCJmaWVsZF9uYW1lIjoiY3JlYXRlZF9hdCIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOm51bGwsInR5cGUiOiJuZXh0In0=
{
    "records": [
        // ....
    ],
    "paging": {
        "pageToken": "eyJ2YWx1ZSI6MTM0OTM2MTI4MS4wMjcsImZpZWxkX3R5cGUiOiJUaW1lIiwiZmllbGRfbmFtZSI6ImNyZWF0ZWRfYXQiLCJkaXJlY3Rpb24iOjEsImluY2x1ZGVfY3VycmVudCI6dHJ1ZSwidGllYnJlYWtfaWQiOiI1MDZkOWU4MTdhYTU4ZDEyNTkwMDBmMTIiLCJ0eXBlIjoibmV4dCJ9",
        "perPage": 1,
        "firstPageToken": "eyJ2YWx1ZSI6bnVsbCwiZmllbGRfdHlwZSI6IlRpbWUiLCJmaWVsZF9uYW1lIjoiY3JlYXRlZF9hdCIsImRpcmVjdGlvbiI6MSwiaW5jbHVkZV9jdXJyZW50IjpmYWxzZSwidGllYnJlYWtfaWQiOm51bGwsInR5cGUiOiJuZXh0In0=",
        "nextPageToken": "eyJ2YWx1ZSI6MTM0OTM2MTI4MS4wMjcsImZpZWxkX3R5cGUiOiJUaW1lIiwiZmllbGRfbmFtZSI6ImNyZWF0ZWRfYXQiLCJkaXJlY3Rpb24iOjEsImluY2x1ZGVfY3VycmVudCI6ZmFsc2UsInRpZWJyZWFrX2lkIjoiNTA2ZDllODE3YWE1OGQxMjU5MDAwZjEyIiwidHlwZSI6Im5leHQifQ=="
    }
}

Given that, we can go to the first page at any time.

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Add to README please.

CHANGELOG.md Outdated
@@ -1,6 +1,7 @@
### 2.0.0 (Next)

* [#38](https://github.com/mongoid/mongoid-scroll/pull/38): Allow to reverse the scroll - [@GCorbel](https://github.com/GCorbel).
* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a cursor to go to the first page - [@GCorbel](https://github.com/GCorbel).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a cursor to go to the first page - [@GCorbel](https://github.com/GCorbel).
* [#42](https://github.com/mongoid/mongoid-scroll/pull/42): Add a `first` cursor - [@GCorbel](https://github.com/GCorbel).

I think for #38 above we should say "Add a previous cursor" too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed.

lib/mongoid/criteria/scrollable/iterator.rb Outdated Show resolved Hide resolved
@dblock dblock merged commit a725ea5 into mongoid:master Sep 4, 2024
12 checks passed
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

Successfully merging this pull request may close these issues.

2 participants