Skip to content

Commit f4c448f

Browse files
divineJeroenwv
andauthored
feat: backport support for cursor pagination (#2362)
Backport #2358 to L9 Co-Authored-By: Jeroen van de Weerd <[email protected]>
1 parent 8d601b2 commit f4c448f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
55

6+
### Added
7+
- Backport support for cursor pagination [#2358](https://github.com/jenssegers/laravel-mongodb/pull/2358) by [@Jeroenwv](https://github.com/Jeroenwv).
8+
9+
## [3.9.0] - 2022-02-17
10+
611
### Added
712
- Compatibility with Laravel 9.x [#2344](https://github.com/jenssegers/laravel-mongodb/pull/2344) by [@divine](https://github.com/divine).
813

Diff for: src/Eloquent/Builder.php

+23
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,27 @@ public function getConnection()
217217
{
218218
return $this->query->getConnection();
219219
}
220+
221+
/**
222+
* @inheritdoc
223+
*/
224+
protected function ensureOrderForCursorPagination($shouldReverse = false)
225+
{
226+
if (empty($this->query->orders)) {
227+
$this->enforceOrderBy();
228+
}
229+
230+
if ($shouldReverse) {
231+
$this->query->orders = collect($this->query->orders)->map(function ($direction) {
232+
return $direction === 1 ? -1 : 1;
233+
})->toArray();
234+
}
235+
236+
return collect($this->query->orders)->map(function ($direction, $column) {
237+
return [
238+
'column' => $column,
239+
'direction' => $direction === 1 ? 'asc' : 'desc',
240+
];
241+
})->values();
242+
}
220243
}

Diff for: tests/QueryTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,29 @@ public function testPaginate(): void
383383
$this->assertEquals(1, $results->currentPage());
384384
}
385385

386+
public function testCursorPaginate(): void
387+
{
388+
$results = User::cursorPaginate(2);
389+
$this->assertEquals(2, $results->count());
390+
$this->assertNotNull($results->first()->title);
391+
$this->assertNotNull($results->nextCursor());
392+
$this->assertTrue($results->onFirstPage());
393+
394+
$results = User::cursorPaginate(2, ['name', 'age']);
395+
$this->assertEquals(2, $results->count());
396+
$this->assertNull($results->first()->title);
397+
398+
$results = User::orderBy('age', 'desc')->cursorPaginate(2, ['name', 'age']);
399+
$this->assertEquals(2, $results->count());
400+
$this->assertEquals(37, $results->first()->age);
401+
$this->assertNull($results->first()->title);
402+
403+
$results = User::whereNotNull('age')->orderBy('age', 'asc')->cursorPaginate(2, ['name', 'age']);
404+
$this->assertEquals(2, $results->count());
405+
$this->assertEquals(13, $results->first()->age);
406+
$this->assertNull($results->first()->title);
407+
}
408+
386409
public function testUpdate(): void
387410
{
388411
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));

0 commit comments

Comments
 (0)