Skip to content

Commit

Permalink
Merge pull request #3 from emdgroup/fix/gsi-pagination
Browse files Browse the repository at this point in the history
fix pagination on index
  • Loading branch information
monken authored Apr 23, 2023
2 parents 7cd7892 + bd8188c commit 7e1906f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@emdgroup/dynamodb-paginator",
"version": "2.1.1",
"version": "2.1.2",
"main": "dist/cjs/index.js",
"exports": {
"import": "./dist/esm/index.js",
Expand Down
16 changes: 12 additions & 4 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ describe('aws', () => {
assert.equal(paginator.scannedCount, 20);
});

it('nextToken', async () => {
[null, 'GSI1.1'].map((idx) => it(idx ? 'nextToken with index' : 'nextToken', async () => {
// give index some time to update
if (idx) await new Promise((resolve) => setTimeout(resolve, 1000));
const paginator = ({ from, context }: { from?: string, context?: string } = {}) => paginateQuery({
TableName: TABLE_NAME,
KeyConditionExpression: 'PK = :pk',
FilterExpression: 'GSI1SK >= :sk',
...idx ? {
IndexName: 'GSI1.1',
KeyConditionExpression: 'GSI1PK = :pk',
FilterExpression: 'SK >= :sk',
} : {
KeyConditionExpression: 'PK = :pk',
FilterExpression: 'GSI1SK >= :sk',
},
ExpressionAttributeValues: {
':pk': 'p:',
':sk': items[4].SK,
Expand Down Expand Up @@ -184,7 +192,7 @@ describe('aws', () => {

const pageCtx = paginator({ from: page1.nextToken, context: 'foobar' });
await assert.rejects(() => pageCtx.all(), { name: 'TokenError' });
});
}).retries(10));

it('predicate', async () => {
type Foo = { PK: string; SK: string };
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ export class PaginationResponse<T extends AttributeMap = AttributeMap> {
while (items.length) {
const item = items.shift();
if (!item) continue;
this._nextKey = this.buildLastEvaluatedKey(item, query.IndexName);
this._nextKey = this.buildLastEvaluatedKey(item);
if (query.IndexName) this._nextKey = {
...this._nextKey,
...this.buildLastEvaluatedKey(item, query.IndexName),
};
if (filter && !filter(item)) continue;
this.count += 1;
yield item as T;
Expand Down

0 comments on commit 7e1906f

Please sign in to comment.