File tree Expand file tree Collapse file tree 2 files changed +41
-6
lines changed
packages/spacecat-shared-data-access Expand file tree Collapse file tree 2 files changed +41
-6
lines changed Original file line number Diff line number Diff line change @@ -248,15 +248,26 @@ class BaseCollection {
248248 ) ;
249249 }
250250
251- const records = await query . go ( queryOptions ) ;
251+ // execute the initial query
252+ let result = await query . go ( queryOptions ) ;
253+ let allData = result . data ;
254+
255+ // if the caller requests ALL pages and we're not using limit: 1,
256+ // continue to fetch until there is no LastEvaluatedKey.
257+ if ( options . fetchAllPages && options . limit !== 1 ) {
258+ while ( result . lastEvaluatedKey ) {
259+ // update queryOptions with the start key to fetch the next page
260+ queryOptions . ExclusiveStartKey = result . lastEvaluatedKey ;
261+ // eslint-disable-next-line no-await-in-loop
262+ result = await query . go ( queryOptions ) ;
263+ allData = allData . concat ( result . data ) ;
264+ }
265+ }
252266
253267 if ( options . limit === 1 ) {
254- if ( records . data ?. length === 0 ) {
255- return null ;
256- }
257- return this . #createInstance( records . data [ 0 ] ) ;
268+ return allData . length ? this . #createInstance( allData [ 0 ] ) : null ;
258269 } else {
259- return this . #createInstances( records . data ) ;
270+ return this . #createInstances( allData ) ;
260271 }
261272 } catch ( error ) {
262273 return this . #logAndThrowError( 'Failed to query' , error ) ;
Original file line number Diff line number Diff line change @@ -675,6 +675,30 @@ describe('BaseCollection', () => {
675675 . to . have . been . calledOnceWithExactly ( { pk : 'ALL_MOCKENTITYMODELS' } ) ;
676676 expect ( mockGo ) . to . have . been . calledOnceWithExactly ( { order : 'desc' , attributes : [ 'test' ] } ) ;
677677 } ) ;
678+
679+ it ( 'handles pagination with fetchAllPages option' , async ( ) => {
680+ const firstResult = { data : [ mockRecord ] , lastEvaluatedKey : 'key1' } ;
681+ const secondRecord = { id : '2' , foo : 'bar' } ;
682+ const secondResult = { data : [ secondRecord ] } ;
683+
684+ const goStub = stub ( ) ;
685+ goStub . onFirstCall ( ) . resolves ( firstResult ) ;
686+ goStub . onSecondCall ( ) . resolves ( secondResult ) ;
687+
688+ mockElectroService . entities . mockEntityModel . query . all . returns ( {
689+ go : goStub ,
690+ } ) ;
691+
692+ const result = await baseCollectionInstance . all ( { } , { fetchAllPages : true } ) ;
693+ expect ( result ) . to . be . an ( 'array' ) . that . has . length ( 2 ) ;
694+ expect ( result [ 0 ] . record ) . to . deep . include ( mockRecord ) ;
695+ expect ( result [ 1 ] . record ) . to . deep . include ( secondRecord ) ;
696+
697+ expect ( goStub . callCount ) . to . equal ( 2 ) ;
698+
699+ const secondCallArgs = goStub . secondCall . args [ 0 ] ;
700+ expect ( secondCallArgs ) . to . deep . include ( { order : 'desc' , ExclusiveStartKey : 'key1' } ) ;
701+ } ) ;
678702 } ) ;
679703
680704 describe ( 'allByIndexKeys' , ( ) => {
You can’t perform that action at this time.
0 commit comments