-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(QueryBuilder): Add pagination collectors to qb
Pagination collectors allow qb to help with pagination by abstracting `limit` and `offset` to a concept of a page and by collecting relevant pagination data into a struct returned with the results of the query: ``` { "totalRecords": 45, "maxRows": 25, "totalPages": 2, "page": 2, "offset": 25, "results": [ /* ... */ ] } ``` BREAKING CHANGE: Changes to argument names of `forPage` to `page` and `maxRows`.
- Loading branch information
Showing
3 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
component extends="testbox.system.BaseSpec" { | ||
|
||
function run() { | ||
describe( "pagination", function() { | ||
it( "returns the default pagination object", function() { | ||
var builder = getBuilder(); | ||
var expectedResults = []; | ||
for ( var i = 1; i <= 25; i++ ) { | ||
expectedResults.append( { "id" = i } ); | ||
} | ||
var expectedQuery = queryNew( "id", "integer", expectedResults ); | ||
builder.$( "count", 45 ); | ||
builder.$( "runQuery", expectedQuery ); | ||
|
||
var results = builder.from( "users" ).paginate(); | ||
|
||
expect( results ).toBe( { | ||
"pagination": { | ||
"maxRows": 25, | ||
"offset": 0, | ||
"page": 1, | ||
"totalPages": 2, | ||
"totalRecords": 45 | ||
}, | ||
"results": expectedResults | ||
} ); | ||
} ); | ||
|
||
it( "can get results for subsequent pages", function() { | ||
var builder = getBuilder(); | ||
var expectedResults = []; | ||
for ( var i = 26; i <= 45; i++ ) { | ||
expectedResults.append( { "id" = i } ); | ||
} | ||
var expectedQuery = queryNew( "id", "integer", expectedResults ); | ||
builder.$( "count", 45 ); | ||
builder.$( "runQuery", expectedQuery ); | ||
|
||
var results = builder.from( "users" ).paginate( page = 2 ); | ||
|
||
expect( results ).toBe( { | ||
"pagination": { | ||
"maxRows": 25, | ||
"offset": 25, | ||
"page": 2, | ||
"totalPages": 2, | ||
"totalRecords": 45 | ||
}, | ||
"results": expectedResults | ||
} ); | ||
} ); | ||
|
||
it( "can provide a custom amount per page", function() { | ||
var builder = getBuilder(); | ||
var expectedResults = []; | ||
for ( var i = 1; i <= 10; i++ ) { | ||
expectedResults.append( { "id" = i } ); | ||
} | ||
var expectedQuery = queryNew( "id", "integer", expectedResults ); | ||
builder.$( "count", 45 ); | ||
builder.$( "runQuery", expectedQuery ); | ||
|
||
var results = builder.from( "users" ).paginate( page = 1, maxRows = 10); | ||
|
||
expect( results ).toBe( { | ||
"pagination": { | ||
"maxRows": 10, | ||
"offset": 0, | ||
"page": 1, | ||
"totalPages": 5, | ||
"totalRecords": 45 | ||
}, | ||
"results": expectedResults | ||
} ); | ||
} ); | ||
|
||
it( "can provide a custom paginator shell", function() { | ||
var builder = getBuilder(); | ||
builder.setPaginationCollector( { | ||
"generateWithResults" = function( totalRecords, results, page, maxRows ) { | ||
return { | ||
"total": totalRecords, | ||
"pageNumber": page, | ||
"limit": maxRows, | ||
"data": results | ||
}; | ||
} | ||
} ); | ||
var expectedResults = []; | ||
for ( var i = 1; i <= 25; i++ ) { | ||
expectedResults.append( { "id" = i } ); | ||
} | ||
var expectedQuery = queryNew( "id", "integer", expectedResults ); | ||
builder.$( "count", 45 ); | ||
builder.$( "runQuery", expectedQuery ); | ||
|
||
var results = builder.from( "users" ).paginate(); | ||
|
||
expect( results ).toBe( { | ||
"total": 45, | ||
"pageNumber": 1, | ||
"limit": 25, | ||
"data": expectedResults | ||
} ); | ||
} ); | ||
} ); | ||
} | ||
|
||
private function getBuilder() { | ||
var grammar = getMockBox() | ||
.createMock( "qb.models.Grammars.BaseGrammar" ) | ||
.init(); | ||
var builder = getMockBox().createMock( "qb.models.Query.QueryBuilder" ) | ||
.init( grammar ); | ||
return builder; | ||
} | ||
|
||
private array function getTestBindings( builder ) { | ||
return builder.getBindings().map( function( binding ) { | ||
return binding.value; | ||
} ); | ||
} | ||
|
||
} |