From 16e8f9161857ad1be412e45fd7cb8f360caea784 Mon Sep 17 00:00:00 2001 From: Jeroen van de Weerd Date: Wed, 23 Feb 2022 16:11:51 +0100 Subject: [PATCH 1/6] Add support for cursor pagination This commit adds support for Laravel cursor pagination. Fixes #2314 --- src/Eloquent/Builder.php | 31 +++++++++++++++++++++++++++++++ tests/QueryTest.php | 23 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index f77e87c2d..ed3333f5e 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -216,4 +216,35 @@ 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 $this->mapMongodbOrdersToEloquentOrders($this->query->orders); + } + + private function mapMongodbOrdersToEloquentOrders($orders) { + $eloquentOrders = []; + + foreach($orders as $column => $direction) { + $eloquentOrders[] = [ + 'column' => $column, + 'direction' => $direction === 1 ? 'asc' : 'desc', + ]; + } + + return collect($eloquentOrders); + } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index cc22df587..b2716e178 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -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'])); From d6652de1f2e8d9757f123f0aef9d3a99d04a0d6b Mon Sep 17 00:00:00 2001 From: Jeroen van de Weerd Date: Wed, 23 Feb 2022 16:28:20 +0100 Subject: [PATCH 2/6] Fix CS issues This commit fixes CS issues --- src/Eloquent/Builder.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index ed3333f5e..dde05ff42 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -13,6 +13,7 @@ class Builder extends EloquentBuilder /** * The methods that should be returned from query builder. + * * @var array */ protected $passthru = [ @@ -191,6 +192,7 @@ 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 * @return array */ @@ -235,10 +237,11 @@ protected function ensureOrderForCursorPagination($shouldReverse = false) return $this->mapMongodbOrdersToEloquentOrders($this->query->orders); } - private function mapMongodbOrdersToEloquentOrders($orders) { + private function mapMongodbOrdersToEloquentOrders($orders) + { $eloquentOrders = []; - foreach($orders as $column => $direction) { + foreach ($orders as $column => $direction) { $eloquentOrders[] = [ 'column' => $column, 'direction' => $direction === 1 ? 'asc' : 'desc', From 464af94dbfef0fc45d0e694767fa10b7aeae09bd Mon Sep 17 00:00:00 2001 From: Jeroen van de Weerd Date: Wed, 23 Feb 2022 16:34:42 +0100 Subject: [PATCH 3/6] Fix CS issues This commit fixes CS issues --- src/Eloquent/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index dde05ff42..4ec0e3cb5 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -193,7 +193,7 @@ public function raw($expression = null) * 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) From 05677940142ace388054c43b4e70eb45ce4b934b Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Wed, 2 Mar 2022 01:02:23 +0300 Subject: [PATCH 4/6] feat: improve logic --- src/Eloquent/Builder.php | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 4ec0e3cb5..01c39be23 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -234,20 +234,11 @@ protected function ensureOrderForCursorPagination($shouldReverse = false) })->toArray(); } - return $this->mapMongodbOrdersToEloquentOrders($this->query->orders); - } - - private function mapMongodbOrdersToEloquentOrders($orders) - { - $eloquentOrders = []; - - foreach ($orders as $column => $direction) { - $eloquentOrders[] = [ + return collect($this->query->orders)->map(function ($direction, $column) { + return [ 'column' => $column, 'direction' => $direction === 1 ? 'asc' : 'desc', ]; - } - - return collect($eloquentOrders); + })->values(); } } From cbfaa8e2a783e1e44f3b304e7b966d0224e3a930 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Mon, 7 Mar 2022 05:24:23 +0300 Subject: [PATCH 5/6] chore: update changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a43b9acd7..365f53f60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. -## [Unreleased] +## [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 From f2a9644e5260dd9fa828fc7a263c3b54f552a055 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Mon, 7 Mar 2022 05:25:06 +0300 Subject: [PATCH 6/6] chore: fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 365f53f60..1e6c2fb30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changelog All notable changes to this project will be documented in this file. -## [Unreleased +## [Unreleased] ### Added - Support for cursor pagination [#2358](https://github.com/jenssegers/laravel-mongodb/pull/2358) by [@Jeroenwv](https://github.com/Jeroenwv).