| 52 | } |
| 53 | |
| 54 | pub async fn cursor_pagination(db: &DatabaseConnection) -> Result<(), DbErr> { |
| 55 | use insert_default::*; |
| 56 | |
| 57 | // Before 5, i.e. id < 5 |
| 58 | |
| 59 | let mut cursor = Entity::find().cursor_by(Column::Id); |
| 60 | |
| 61 | cursor.before(5); |
| 62 | |
| 63 | assert_eq!( |
| 64 | cursor.first(4).all(db).await?, |
| 65 | [ |
| 66 | Model { id: 1 }, |
| 67 | Model { id: 2 }, |
| 68 | Model { id: 3 }, |
| 69 | Model { id: 4 }, |
| 70 | ] |
| 71 | ); |
| 72 | |
| 73 | assert_eq!( |
| 74 | cursor.first(5).all(db).await?, |
| 75 | [ |
| 76 | Model { id: 1 }, |
| 77 | Model { id: 2 }, |
| 78 | Model { id: 3 }, |
| 79 | Model { id: 4 }, |
| 80 | ] |
| 81 | ); |
| 82 | |
| 83 | assert_eq!( |
| 84 | cursor.last(4).all(db).await?, |
| 85 | [ |
| 86 | Model { id: 1 }, |
| 87 | Model { id: 2 }, |
| 88 | Model { id: 3 }, |
| 89 | Model { id: 4 }, |
| 90 | ] |
| 91 | ); |
| 92 | |
| 93 | assert_eq!( |
| 94 | cursor.last(5).all(db).await?, |
| 95 | [ |
| 96 | Model { id: 1 }, |
| 97 | Model { id: 2 }, |
| 98 | Model { id: 3 }, |
| 99 | Model { id: 4 }, |
| 100 | ] |
| 101 | ); |
| 102 | |
| 103 | // Before 5 DESC, i.e. id > 5 |
| 104 | |
| 105 | let mut cursor = Entity::find().cursor_by(Column::Id); |
| 106 | |
| 107 | cursor.before(5).desc(); |
| 108 | |
| 109 | assert_eq!( |
| 110 | cursor.first(4).all(db).await?, |
| 111 | [ |