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 cursor pagination desc order #2037

Merged
merged 11 commits into from
Jan 14, 2024
Merged

Add cursor pagination desc order #2037

merged 11 commits into from
Jan 14, 2024

Conversation

ziimakc
Copy link
Contributor

@ziimakc ziimakc commented Dec 29, 2023

New Features

Add cursor pagination desc order support

Usage:

cursor_by().asc() or cursor_by().desc()


Moved query builder logic inside all because it depends on sort order asc/desc

@ziimakc
Copy link
Contributor Author

ziimakc commented Dec 30, 2023

It seems more complicated then I expected, for example for the following cases

first=2,after=5,asc 6,7 is id>5 ASC
last=2,after=5,asc 9,10 is id>5 DESC + reverse result
last=2,before=5,asc 3,4 is id<5 DESC + reverse result
first=2,before=5,asc 1,2 is id<5 ASC 

first=2,after=5,desc 4,3 is id<5 DESC
last=2,after=5,desc 2,1 is id<5 ASC + reverse result
last=2,before=5,desc 7,6 is id>5 ASC + reverse result
first=2,before=5,desc 10,9 is id>5 DESC

first=2,after=5,before=9,asc 6,7 is id>5 and id<9 ASC
last=2,after=5,before=9,asc 7,8 is id>5 and id<9 DESC + reverse result
last=2,after=5,before=1,desc 3,2 is id<5 and id>1 ASC + reverse result
first=2,after=5,before=1,desc 4,3 is id<5 and id>1 DESC

We can say rules is that:

  • reverse sort order in sql query when last is used
  • if order reversed, reverse results
  • for after cursor use > when asc is used
  • for before cursor use > when desc is used

Will test it out and see how it works

@ziimakc
Copy link
Contributor Author

ziimakc commented Dec 30, 2023

Will add mock query tests a bit later Done.

@ziimakc ziimakc changed the title WIP: Add cursor pagination desc order Add cursor pagination desc order Dec 31, 2023
Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

Happy new year. Thank you! This is sure a lot of effort, and it seems to be working. I am not too concerned as long as our test coverage increases! (which leads to some comments below)

),])]
);

Ok(())
}

#[smol_potat::test]
async fn composite_keys_5() -> Result<(), DbErr> {
async fn composite_keys_5_desc() -> Result<(), DbErr> {
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid changing existing test cases, since it shrinks our test coverage. Can you extract the common parts and make it a new test case?

Copy link
Contributor Author

@ziimakc ziimakc Jan 12, 2024

Choose a reason for hiding this comment

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

I didn't change any existing tests, but duplicated all existing ones with desc order, it shows a bit incorrectly by github, I assume because names composite_keys_5 and composite_keys_5_desc and the rest of the test body except after and first replaced by before, last and desc which produces same query, but reversed results

Copy link
Member

@tyt2y3 tyt2y3 Jan 14, 2024

Choose a reason for hiding this comment

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

Oh thanks! Got it now

].join(" "))
);

assert_eq!(
DbBackend::Postgres.build(&
Entity::find()
.cursor_by((Column::Col1, Column::Col2, Column::Col3, Column::Col4, Column::Col5, Column::Col6))
.after(("val_1", "val_2", "val_3", "val_4", "val_5", "val_6"))
.before(("val_1", "val_2", "val_3", "val_4", "val_5", "val_6")).desc().apply_limit().apply_order_by().apply_filters()
Copy link
Member

@tyt2y3 tyt2y3 Jan 12, 2024

Choose a reason for hiding this comment

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

Likewise, it's better to test both before and after separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as my previous comment, it's a copy of cursor_by_many named as cursor_by_many_desc, no tests were removed or changed except additions like .apply_limit().apply_order_by().apply_filters() because filters applied when we call all()

@tyt2y3 tyt2y3 merged commit c5adb9d into SeaQL:master Jan 14, 2024
30 of 31 checks passed
Copy link

🎉 Released In 0.12.11 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

maxdeviant referenced this pull request in zed-industries/zed Jul 27, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [sea-orm](https://www.sea-ql.org/SeaORM)
([source](https://github.com/SeaQL/sea-orm)) | dev-dependencies |
patch | `0.12.3` -> `0.12.15` |
| [sea-orm](https://www.sea-ql.org/SeaORM)
([source](https://github.com/SeaQL/sea-orm)) | dependencies | patch |
`0.12.3` -> `0.12.15` |

---

### Release Notes

<details>
<summary>SeaQL/sea-orm (sea-orm)</summary>

###
[`v0.12.15`](https://github.com/SeaQL/sea-orm/releases/tag/0.12.15)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.14...0.12.15)

##### Enhancements

- `DerivePartialModel` macro attribute `entity` now supports `syn::Type`
[https://github.com/SeaQL/sea-orm/pull/2137](https://github.com/SeaQL/sea-orm/pull/2137)

```rust

#[derive(DerivePartialModel)]
#[sea_orm(entity = "<entity::Model as ModelTrait>::Entity")]
struct EntityNameNotAIdent {
    #[sea_orm(from_col = "foo2")]
    _foo: i32,
    #[sea_orm(from_col = "bar2")]
    _bar: String,
}
```

- Added `RelationDef::from_alias()`
[https://github.com/SeaQL/sea-orm/pull/2146](https://github.com/SeaQL/sea-orm/pull/2146)

```rust
assert_eq!(
    cake::Entity::find()
        .join_as(
            JoinType::LeftJoin,
            cake_filling::Relation::Cake.def().rev(),
            cf.clone()
        )
        .join(
            JoinType::LeftJoin,
            cake_filling::Relation::Filling.def().from_alias(cf)
        )
        .build(DbBackend::MySql)
        .to_string(),
    [
        "SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
        "LEFT JOIN `cake_filling` AS `cf` ON `cake`.`id` = `cf`.`cake_id`",
        "LEFT JOIN `filling` ON `cf`.`filling_id` = `filling`.`id`",
    ]
    .join(" ")
);
```

###
[`v0.12.14`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#01214---2024-02-05)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.12...0.12.14)

- Added feature flag `sqlite-use-returning-for-3_35` to use SQLite's
returning
[https://github.com/SeaQL/sea-orm/pull/2070](https://github.com/SeaQL/sea-orm/pull/2070)
- Added Loco example
[https://github.com/SeaQL/sea-orm/pull/2092](https://github.com/SeaQL/sea-orm/pull/2092)

###
[`v0.12.12`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#01212---2024-01-22)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.11...0.12.12)

##### Bug Fixes

- \[sea-orm-cli] Fix entity generation for non-alphanumeric enum
variants[https://github.com/SeaQL/sea-orm/pull/1821](https://github.com/SeaQL/sea-orm/pull/1821)1
- \[sea-orm-cli] Fix entity generation for relations with composite
keys[https://github.com/SeaQL/sea-orm/pull/2071](https://github.com/SeaQL/sea-orm/pull/2071)1

##### Enhancements

-   Added `ConnectOptions::test_before_acquire`

###
[`v0.12.11`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#01211---2024-01-14)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.10...0.12.11)

##### New Features

- Added `desc` to `Cursor` paginator
[https://github.com/SeaQL/sea-orm/pull/2037](https://github.com/SeaQL/sea-orm/pull/2037)

##### Enhancements

- Improve query performance of `Paginator`'s `COUNT` query
[https://github.com/SeaQL/sea-orm/pull/2030](https://github.com/SeaQL/sea-orm/pull/2030)
- Added SQLx slow statements logging to `ConnectOptions`
[https://github.com/SeaQL/sea-orm/pull/2055](https://github.com/SeaQL/sea-orm/pull/2055)
- Added `QuerySelect::lock_with_behavior`
[https://github.com/SeaQL/sea-orm/pull/1867](https://github.com/SeaQL/sea-orm/pull/1867)

##### Bug Fixes

- \[sea-orm-macro] Qualify types in `DeriveValueType` macro
[https://github.com/SeaQL/sea-orm/pull/2054](https://github.com/SeaQL/sea-orm/pull/2054)

##### House keeping

- Fix clippy warnings on 1.75
[https://github.com/SeaQL/sea-orm/pull/2057](https://github.com/SeaQL/sea-orm/pull/2057)

###
[`v0.12.10`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#01210---2023-12-14)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.9...0.12.10)

##### New Features

- \[sea-orm-macro] Comment attribute for Entity (`#[sea_orm(comment =
"action")]`); `create_table_from_entity` supports comment
[https://github.com/SeaQL/sea-orm/pull/2009](https://github.com/SeaQL/sea-orm/pull/2009)
- Added "proxy" (feature flag `proxy`) to database backend
[https://github.com/SeaQL/sea-orm/pull/1881](https://github.com/SeaQL/sea-orm/pull/1881),
[https://github.com/SeaQL/sea-orm/pull/2000](https://github.com/SeaQL/sea-orm/pull/2000)

##### Enhancements

- Cast enums in `is_in` and `is_not_in`
[https://github.com/SeaQL/sea-orm/pull/2002](https://github.com/SeaQL/sea-orm/pull/2002)

##### Upgrades

- Updated `sea-query` to `0.30.5`
https://github.com/SeaQL/sea-query/releases/tag/0.30.5

###
[`v0.12.9`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#0129---2023-12-08)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.8...0.12.9)

##### Enhancements

- Add source annotations to errors
[https://github.com/SeaQL/sea-orm/pull/1999](https://github.com/SeaQL/sea-orm/pull/1999)

##### Upgrades

- Updated `sea-query` to `0.30.4`
https://github.com/SeaQL/sea-query/releases/tag/0.30.4

###
[`v0.12.8`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#0128---2023-12-04)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.7...0.12.8)

##### Enhancements

- Implement `StatementBuilder` for `sea_query::WithQuery`
[https://github.com/SeaQL/sea-orm/issues/1960](https://github.com/SeaQL/sea-orm/issues/1960)

##### Upgrades

- Upgrade `axum` example to `0.7`
[https://github.com/SeaQL/sea-orm/pull/1984](https://github.com/SeaQL/sea-orm/pull/1984)

###
[`v0.12.7`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#0127---2023-11-22)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.6...0.12.7)

##### Enhancements

- Added method `expr_as_` that accepts `self`
[https://github.com/SeaQL/sea-orm/pull/1979](https://github.com/SeaQL/sea-orm/pull/1979)

##### Upgrades

- Updated `sea-query` to `0.30.3`
https://github.com/SeaQL/sea-query/releases/tag/0.30.3

###
[`v0.12.6`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#0126---2023-11-13)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.5...0.12.6)

##### New Features

- Added `#[sea_orm(skip)]` for `FromQueryResult` derive macro
[https://github.com/SeaQL/sea-orm/pull/1954](https://github.com/SeaQL/sea-orm/pull/1954)

###
[`v0.12.5`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#0125---2023-11-12)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.4...0.12.5)

##### Bug Fixes

- \[sea-orm-cli] Fix duplicated active enum use statements on generated
entities[https://github.com/SeaQL/sea-orm/pull/1953](https://github.com/SeaQL/sea-orm/pull/1953)3
- \[sea-orm-cli] Added `--enum-extra-derives`
[https://github.com/SeaQL/sea-orm/pull/1934](https://github.com/SeaQL/sea-orm/pull/1934)
- \[sea-orm-cli] Added `--enum-extra-attributes`
[https://github.com/SeaQL/sea-orm/pull/1952](https://github.com/SeaQL/sea-orm/pull/1952)

###
[`v0.12.4`](https://github.com/SeaQL/sea-orm/blob/HEAD/CHANGELOG.md#0124---2023-10-19)

[Compare
Source](https://github.com/SeaQL/sea-orm/compare/0.12.3...0.12.4)

##### New Features

- Add support for root JSON arrays
[https://github.com/SeaQL/sea-orm/pull/1898](https://github.com/SeaQL/sea-orm/pull/1898)
Now the following works (requires the `json-array` / `postgres-array`
feature)!

```rust

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_struct_vec")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(column_type = "Json")]
    pub struct_vec: Vec<JsonColumn>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct JsonColumn {
    pub value: String,
}
```

##### Enhancements

- Loader: use `ValueTuple` as hash key
[https://github.com/SeaQL/sea-orm/pull/1868](https://github.com/SeaQL/sea-orm/pull/1868)

##### Upgrades

- Updated `sea-query` to `0.30.2`
https://github.com/SeaQL/sea-query/releases/tag/0.30.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 3pm on Wednesday" in timezone
America/New_York, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these
updates again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

Release Notes:

- N/A

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MzguMCIsInVwZGF0ZWRJblZlciI6IjM3LjQzOC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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