在typescript中使用aws-sdk/lib-dynamodb



我的pokemon数据库有这样的schema:

id: pikachu (pokemon的名字),content: pokemon(对象),height: 3

我想获得高度最大的口袋妖怪,为了做到这一点,我想获得我的数据库中所有的口袋妖怪,然后"reduce">

我正在使用这个aws库:@aws-sdk/lib-dynamodb

如何创建QueryCommand来获取所有值?我不需要任何KeyConditions,但是AWS要求我必须包含它们。

new QueryCommand({
TableName: this.tableName,
}),
);
const array =
response?.Items?.map(
item =>
new PokemonDB({
pokemon: item.content,
height: item.height,
}),
) ?? [];
const max = array.reduce(function (prev, current) {
return prev.height > current.height ? prev : current;
});
return max.height;

错误是:

ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.

DynamoDB基本上是一个键/值数据库。所有操作都必须提供散列/分区键(PK),以及可选的排序键。在不提供PK的情况下,不可能有效地查询所有项,因为您必须借助于全表扫描来实现这一点。

通常,Dynamo中的数据建模使用通常被称为"单表设计"的东西。所以,你可以有一个像这样的表:

<表类> PK SK myVal tbody><<tr>user_123_friendsfriend_456xuser_123_friendsfriend_789yuser_456_friendsfriend_123z

最新更新