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

Cursor pagination #822

Merged
merged 9 commits into from
Jul 10, 2022
Merged

Cursor pagination #822

merged 9 commits into from
Jul 10, 2022

Conversation

tyt2y3
Copy link
Member

@tyt2y3 tyt2y3 commented Jun 26, 2022

Continue #754

Things to do:

I released the clear_order_by in sea-query 0.25

So the dependency is to upgrade sea-orm to sea-query 0.25.1

@billy1624 billy1624 linked an issue Jun 27, 2022 that may be closed by this pull request
@tyt2y3
Copy link
Member Author

tyt2y3 commented Jun 30, 2022

Is there a PR for upgrading sea-query to 0.25?

@billy1624
Copy link
Member

Is there a PR for upgrading sea-query to 0.25?

Hey @tyt2y3, check #834

@tyt2y3 tyt2y3 merged commit bfe6eb3 into master Jul 10, 2022
@tyt2y3 tyt2y3 deleted the cursor-pagination branch July 10, 2022 07:08
billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Jul 13, 2022
tyt2y3 added a commit to SeaQL/seaql.github.io that referenced this pull request Jul 17, 2022
* Custom join condition (SeaQL/sea-orm#793)

* Migration does not depend on entity crate

* Define integer enum with repr[x] syntax

* Document datatype mappings (SeaQL/sea-orm#772)

* Cursor pagination (SeaQL/sea-orm#754, SeaQL/sea-orm#822)

* (de)serialize custom JSON types (SeaQL/sea-orm#794)

* Generate new migration file (SeaQL/sea-orm#656)

* Skip generating entity file for specific tables (SeaQL/sea-orm#837)

* Generate entity with date time crate option (SeaQL/sea-orm#724)

* Drop `SelectTwoMany::one()` method (SeaQL/sea-orm#813)

* Datatype mappings of primitives (SeaQL/sea-orm#850, SeaQL/sea-schema#75)

* Join with table alias (SeaQL/sea-orm#852)

* SQLx logging level (SeaQL/sea-orm#800)

* Insert with on conflict (SeaQL/sea-orm#791)

* Migrate generate should take file name as argument instead of option (SeaQL/sea-orm#870)

* Upgrade docusaurus to 2.0.0-beta.22

* What's new in SeaORM 0.9.0

* Move migration section forward

* Rename "Generating Database Schema" section to "Generating SeaQuery Statement"

* Fix broken links

* Edit

* Edit

* Edit

* Edit

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
@penso
Copy link

penso commented Aug 12, 2022

Is there any plan to have cursor in other directions? Like if we want to have created_at DESC to list most recent first?

@billy1624
Copy link
Member

Hey @penso, thanks for asking!!

This is exactly what .last(N) method is for.

use sea_orm::{entity::*, query::*, tests_cfg::cake};

// Create a cursor that order by `cake`.`id`
let mut cursor = cake::Entity::find().cursor_by(cake::Column::Id);

// Filter paginated result by `cake`.`id` > 1 AND `cake`.`id` < 100
cursor.after(1).before(100);

// Get last 10 rows (order by `cake`.`id` DESC but rows are returned in ascending order)
for cake in cursor.last(10).all(db).await? {
    // Do something on cake: cake::Model
}

Docs: https://www.sea-ql.org/SeaORM/docs/basic-crud/select/#cursor-pagination

@penso
Copy link

penso commented Aug 12, 2022

Wouldn't it be easier to have:

let mut cursor = cake::Entity::find().cursor_by(cake::Column::Id, .desc)
for cake in cursor.first(10).all(db).await? {
}

so we don't need to convert/reverse the resulted array if we actually want to deliver the last ID first and in descending order?

@billy1624
Copy link
Member

Hey @penso, I think we have add two additional method - first_desc and last_desc - to the cursor:

// Get first 10 rows (order by `cake`.`id` ASC but rows are returned in descending order)
for cake in cursor.first_desc(10).all(db).await? {
    // Do something on cake: cake::Model
}

// Get last 10 rows (order by `cake`.`id` ASC but rows are returned in descending order)
for cake in cursor.last_desc(10).all(db).await? {
    // Do something on cake: cake::Model
}

@tyt2y3
Copy link
Member Author

tyt2y3 commented Aug 16, 2022

Is there any drawback to just rev the Vec?

@billy1624
Copy link
Member

billy1624 commented Aug 16, 2022

Is there any drawback to just rev the Vec?

Good question! @penso?
Note that first_desc and last_desc are simply performing Vec:::rev under-the-hood loll

@penso
Copy link

penso commented Aug 16, 2022

No drawback than I can think of, except the mental thinking. I've done a "manual" cursor this week-end and will try to move to cursor_by and see if it's easier or not.

@billy1624
Copy link
Member

Please! And let us know your experience after using it :)

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

Successfully merging this pull request may close these issues.

Cursor Based Pagination
3 participants