Skip to content

Commit a424f47

Browse files
authored
feat(data-access): fetch all records on demand (#659)
Please ensure your pull request adheres to the following guidelines: - [ ] make sure to link the related issues in this description - [ ] when merging / squashing, make sure the fixed issue references are visible in the commits, for easy compilation of release notes ## Related Issues Thanks for contributing!
1 parent b511118 commit a424f47

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

packages/spacecat-shared-data-access/src/models/base/base.collection.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff 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);

packages/spacecat-shared-data-access/test/unit/models/base/base.collection.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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', () => {

0 commit comments

Comments
 (0)