Skip to content

Commit c1b6d04

Browse files
Park JuhyungPark Juhyung
authored andcommitted
Calculate firstEvaluatedKey and lastEvaluatedKey correctly
The indexer queries DB one more row than a user requested. And the indexer uses the additional row to let the user know whether there is the next page or not. The indexer should calculate firstEvaluatedKey and lastEvaluatedKey without the additional row. Before this commit, the indexer calculates evaluated keys before removing the additional row. After this commit, the indexer calculates evaluated keys after removing the additional row.
1 parent db40978 commit c1b6d04

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

src/routers/asset.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,15 @@ export function handle(context: IndexerContext, router: Router) {
172172
confirmThreshold
173173
});
174174
const utxo = utxoInsts.map(inst => inst.get({ plain: true }));
175-
const firstItem = utxo[0];
176-
const lastItem = utxo[utxo.length - 1];
177175

178176
res.json(
179177
createPaginationResult({
180178
query: {
181179
firstEvaluatedKey,
182180
lastEvaluatedKey
183181
},
184-
result: {
185-
data: utxo,
186-
firstEvaluatedKey: firstItem
187-
? UTXOModel.createUTXOEvaluatedKey(firstItem)
188-
: null,
189-
lastEvaluatedKey: lastItem
190-
? UTXOModel.createUTXOEvaluatedKey(lastItem)
191-
: null
192-
},
182+
rows: utxo,
183+
getEvaluatedKey: UTXOModel.createUTXOEvaluatedKey,
193184
itemsPerPage
194185
})
195186
);

src/routers/pagination.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
import * as Sequelize from "sequelize";
22

3-
export function createPaginationResult(params: {
3+
export function createPaginationResult<Row>(params: {
44
query: {
55
firstEvaluatedKey?: any[] | null;
66
lastEvaluatedKey?: any[] | null;
77
};
8-
result: {
9-
data: any[];
10-
firstEvaluatedKey: string | null;
11-
lastEvaluatedKey: string | null;
12-
};
8+
rows: Row[];
9+
getEvaluatedKey: (row: Row) => string;
1310
itemsPerPage: number;
1411
}) {
15-
const { query, result, itemsPerPage } = params;
12+
const { query, itemsPerPage, getEvaluatedKey } = params;
13+
let { rows } = params;
1614

1715
const firstQuery = !query.firstEvaluatedKey && !query.lastEvaluatedKey;
1816
const order: "forward" | "reverse" = queryOrder(query);
1917

2018
if (order === "reverse") {
21-
result.data = result.data.reverse();
19+
rows = rows.reverse();
2220
}
2321

24-
const hasMorePage = result.data.length > itemsPerPage;
22+
const hasMorePage = rows.length > itemsPerPage;
2523
if (hasMorePage) {
2624
if (order === "forward") {
27-
result.data.pop();
25+
rows.pop();
2826
} else if (order === "reverse") {
29-
result.data.unshift();
27+
rows.unshift();
3028
}
3129
}
3230

@@ -44,12 +42,15 @@ export function createPaginationResult(params: {
4442
} else {
4543
throw new Error("Unreachable");
4644
}
45+
46+
const firstRow = rows[0];
47+
const lastRow = rows[rows.length - 1];
4748
return {
48-
data: result.data,
49+
data: rows,
4950
hasNextPage,
5051
hasPreviousPage,
51-
firstEvaluatedKey: result.firstEvaluatedKey,
52-
lastEvaluatedKey: result.lastEvaluatedKey
52+
firstEvaluatedKey: firstRow ? getEvaluatedKey(firstRow) : null,
53+
lastEvaluatedKey: lastRow ? getEvaluatedKey(lastRow) : null
5354
};
5455
}
5556

0 commit comments

Comments
 (0)