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.8] Add support for cursor pagination #2358

Merged
merged 6 commits 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

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

## [3.8.4] - 2021-05-27

### Fixed
Expand Down
27 changes: 26 additions & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Builder extends EloquentBuilder

/**
* The methods that should be returned from query builder.
*
* @var array
*/
protected $passthru = [
Expand Down Expand Up @@ -191,7 +192,8 @@ public function raw($expression = null)
* TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e
* wiil be reverted
* Issue in laravel frawework https://github.com/laravel/framework/issues/27791.
* @param array $values
*
* @param array $values
* @return array
*/
protected function addUpdatedAtColumn(array $values)
Expand All @@ -216,4 +218,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