【发布时间】:2022-12-08 22:05:53
【问题描述】:
客户端库:“@aws-sdk/client-dynamodb”:“3.188.0”
我有一个 DynamoDB 分页实现。
我的用户数是 98,页面大小是 20。因此我期望有 5 个页面,每个页面有 20、20、20、20 和 18 个用户。
但实际上我得到了超过 5 个页面,每个页面都有可变数量的用户,如 10、12、11 ..等。
如何让用户获得适当的页面限制,如 20、20、20、20 和 18?
public async pagedList(usersPerPage: number, lastEvaluatedKey?: string): Promise<PagedUser> {
const params = {
TableName: tableName,
Limit: usersPerPage,
FilterExpression: '#type = :type',
ExpressionAttributeValues: {
':type': { S: type },
},
ExpressionAttributeNames: {
'#type': 'type',
},
} as ScanCommandInput;
if (lastEvaluatedKey) {
params.ExclusiveStartKey = { 'oid': { S: lastEvaluatedKey } };
}
const command = new ScanCommand(params);
const data = await client.send(command);
const users: User[] = [];
if (data.Items !== undefined) {
data.Items.forEach((item) => {
if (item !== undefined) {
users.push(this.makeUser(item));
}
});
}
let lastKey;
if (data.LastEvaluatedKey !== undefined) {
lastKey = data.LastEvaluatedKey.oid.S?.valueOf();
}
return {
users: users,
lastEvaluatedKey: lastKey
};
}
【问题讨论】:
标签: node.js amazon-dynamodb dynamodb-queries