Skip to content

Commit

Permalink
feat: backport support for cursor pagination (#2362)
Browse files Browse the repository at this point in the history
Backport #2358 to L9

Co-Authored-By: Jeroen van de Weerd <info@jeroenvdweerd.nl>
  • Loading branch information
divine and Jeroenwv committed Mar 7, 2022
1 parent 8d601b2 commit f4c448f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Added
- Backport support for cursor pagination [#2358](https://github.com/jenssegers/laravel-mongodb/pull/2358) by [@Jeroenwv](https://github.com/Jeroenwv).

## [3.9.0] - 2022-02-17

### Added
- Compatibility with Laravel 9.x [#2344](https://github.com/jenssegers/laravel-mongodb/pull/2344) by [@divine](https://github.com/divine).

Expand Down
23 changes: 23 additions & 0 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,27 @@ public function getConnection()
{
return $this->query->getConnection();
}

/**
* @inheritdoc
*/
protected function ensureOrderForCursorPagination($shouldReverse = false)
{
if (empty($this->query->orders)) {
$this->enforceOrderBy();
}

if ($shouldReverse) {
$this->query->orders = collect($this->query->orders)->map(function ($direction) {
return $direction === 1 ? -1 : 1;
})->toArray();
}

return collect($this->query->orders)->map(function ($direction, $column) {
return [
'column' => $column,
'direction' => $direction === 1 ? 'asc' : 'desc',
];
})->values();
}
}
23 changes: 23 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,29 @@ public function testPaginate(): void
$this->assertEquals(1, $results->currentPage());
}

public function testCursorPaginate(): void
{
$results = User::cursorPaginate(2);
$this->assertEquals(2, $results->count());
$this->assertNotNull($results->first()->title);
$this->assertNotNull($results->nextCursor());
$this->assertTrue($results->onFirstPage());

$results = User::cursorPaginate(2, ['name', 'age']);
$this->assertEquals(2, $results->count());
$this->assertNull($results->first()->title);

$results = User::orderBy('age', 'desc')->cursorPaginate(2, ['name', 'age']);
$this->assertEquals(2, $results->count());
$this->assertEquals(37, $results->first()->age);
$this->assertNull($results->first()->title);

$results = User::whereNotNull('age')->orderBy('age', 'asc')->cursorPaginate(2, ['name', 'age']);
$this->assertEquals(2, $results->count());
$this->assertEquals(13, $results->first()->age);
$this->assertNull($results->first()->title);
}

public function testUpdate(): void
{
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));
Expand Down

0 comments on commit f4c448f

Please sign in to comment.