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

[3.9] feat: backport support for cursor pagination #2362

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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